add browse functionality

pull/20/head
Henrique Dias 2015-09-17 17:41:31 +01:00
parent 686a909e23
commit edf0a453a3
10 changed files with 258 additions and 139 deletions

File diff suppressed because one or more lines are too long

View File

@ -2467,7 +2467,7 @@ h2 {
margin: .83em 0;
}
header {
nav {
position: fixed;
top: 0;
left: 0;
@ -2480,7 +2480,7 @@ header {
color: #eee;
}
header nav ul {
nav ul {
margin: 0;
padding: 0;
display: -webkit-box;
@ -2494,21 +2494,21 @@ header nav ul {
display: flex;
}
header nav ul li {
nav ul li {
list-style-type: none;
display: inline-block;
vertical-align: middle;
}
header nav ul li:last-child {
nav ul li:last-child {
margin-left: auto;
}
header nav img {
nav img {
height: 2em;
}
header nav ul li a {
nav ul li a {
padding: .5em;
line-height: 2em;
display: block;
@ -2517,11 +2517,11 @@ header nav ul li a {
transition: .5s ease background-color;
}
header nav ul li a:hover {
nav ul li a:hover {
background-color: rgba(255, 255, 255, 0.57);
}
main {
.main {
position: fixed;
top: 3em;
left: 0;
@ -2530,6 +2530,16 @@ main {
overflow: auto;
}
header {
color: #fff;
background-color: #37474f;
padding: .67em 0;
}
header h1 {
margin: 0;
}
.content {
margin: 1.5em auto;
width: 80%;

View File

@ -9,7 +9,7 @@ h2 {
margin: .83em 0;
}
header {
nav {
position : fixed;
top : 0;
left : 0;
@ -22,7 +22,7 @@ header {
color : #eee;
}
header nav ul {
nav ul {
margin : 0;
padding: 0;
display: -webkit-box;
@ -36,21 +36,21 @@ header nav ul {
display: flex;
}
header nav ul li {
nav ul li {
list-style-type: none;
display : inline-block;
vertical-align : middle;
}
header nav ul li:last-child {
nav ul li:last-child {
margin-left: auto;
}
header nav img {
nav img {
height: 2em;
}
header nav ul li a {
nav ul li a {
padding : .5em;
line-height : 2em;
display : block;
@ -59,11 +59,11 @@ header nav ul li a {
transition : .5s ease background-color;
}
header nav ul li a:hover {
nav ul li a:hover {
background-color: rgba(255, 255, 255, 0.57);
}
main {
.main {
position: fixed;
top : 3em;
left : 0;
@ -72,6 +72,16 @@ main {
overflow: auto;
}
header {
color : #fff;
background-color: #37474f;
padding : .67em 0;
}
header h1 {
margin: 0;
}
.content {
margin : 1.5em auto;
width : 80%;

View File

@ -1,14 +1,47 @@
package browse
import (
"log"
"net/http"
"strings"
"github.com/hacdias/caddy-hugo/page"
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware/browse"
)
// Execute sth
func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
page := new(page.Page)
page.Title = "Browse"
return page.Render(w, r, "browse")
if r.URL.Path[len(r.URL.Path)-1] != '/' {
http.Redirect(w, r, r.URL.Path+"/", http.StatusTemporaryRedirect)
return 0, nil
}
r.URL.Path = strings.Replace(r.URL.Path, "/admin/browse", "", 1)
if r.URL.Path == "" {
r.URL.Path = "/"
}
tpl, err := page.GetTemplate(r, "browse")
if err != nil {
log.Print(err)
return 500, err
}
b := browse.Browse{
Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
return 404, nil
}),
Root: "./",
Configs: []browse.Config{
browse.Config{
PathScope: "/",
Template: tpl,
},
},
}
return b.ServeHTTP(w, r)
}

View File

@ -91,7 +91,7 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
}
page := new(page.Page)
page.Title = "Edit"
page.Name = "Edit"
page.Body = inf
return page.Render(w, r, "edit", "frontmatter")
}

View File

@ -85,6 +85,7 @@ func sortByTitle(config []*frontmatter) {
}
sort.Strings(keys)
// TODO: http://golang.org/pkg/sort/#Interface
cnf := make([]*frontmatter, len(config))
for index, title := range keys {

View File

@ -1,9 +1,9 @@
package page
import (
"html/template"
"log"
"net/http"
"text/template"
"github.com/hacdias/caddy-hugo/assets"
"github.com/hacdias/caddy-hugo/utils"
@ -19,12 +19,24 @@ var funcMap = template.FuncMap{
// Page type
type Page struct {
Title string
Name string
Body interface{}
}
// Render the page
func (p *Page) Render(w http.ResponseWriter, r *http.Request, templates ...string) (int, error) {
tpl, err := GetTemplate(r, templates...)
if err != nil {
log.Print(err)
return 500, err
}
tpl.Execute(w, p)
return 200, nil
}
func GetTemplate(r *http.Request, templates ...string) (*template.Template, error) {
if r.Header.Get("X-PJAX") == "true" {
templates = append(templates, "base_minimal")
} else {
@ -38,7 +50,7 @@ func (p *Page) Render(w http.ResponseWriter, r *http.Request, templates ...strin
if err != nil {
log.Print(err)
return 500, err
return new(template.Template), err
}
if i == 0 {
@ -49,10 +61,9 @@ func (p *Page) Render(w http.ResponseWriter, r *http.Request, templates ...strin
if err != nil {
log.Print(err)
return 500, err
return new(template.Template), err
}
}
tpl.Execute(w, p)
return 200, nil
return tpl, nil
}

View File

@ -61,7 +61,7 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
}
page := new(page.Page)
page.Title = "Settings"
page.Name = "Settings"
page.Body = f
return page.Render(w, r, "settings", "frontmatter")
}

View File

@ -6,7 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#fff">
<title>{{ .Title }}</title>
<title>{{ .Name }}</title>
<link href='https://fonts.googleapis.com/css?family=Roboto:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="/admin/assets/css/main.min.css">
@ -14,7 +14,6 @@
</head>
<body>
<header>
<nav>
<ul>
<li><a href="/"><i class="fa fa-home fa-lg"></i> Home</a></li>
@ -24,8 +23,7 @@
<li><a id="logout" href="#logout"><i class="fa fa-sign-out"></i> Logout</a></li>
</ul>
</nav>
</header>
<div id="container">
<div class="main" id="container">
{{ template "content" . }}
</div>
<footer>

View File

@ -1,5 +1,61 @@
{{ define "content" }}
<header>
<div class="content">
<h1>{{.Path}}</h1>
{{ $path := .Path }}
</div>
</header>
<main>
<a href="/admin/settings">Settings</a>
<div class="content">
{{if .CanGoUp}}
<a href=".." class="up" title="Up one level">&#11025;</a>
{{else}}
<div class="up">&nbsp;</div>
{{end}}
<table>
<tr>
<th>
{{if and (eq .Sort "name") (ne .Order "desc")}}
<a href="?sort=name&order=desc">Name &#9650;</a>
{{else if and (eq .Sort "name") (ne .Order "asc")}}
<a href="?sort=name&order=asc">Name &#9660;</a>
{{else}}
<a href="?sort=name&order=asc">Name</a>
{{end}}
</th>
<th>
{{if and (eq .Sort "size") (ne .Order "desc")}}
<a href="?sort=size&order=desc">Size &#9650;</a>
{{else if and (eq .Sort "size") (ne .Order "asc")}}
<a href="?sort=size&order=asc">Size &#9660;</a>
{{else}}
<a href="?sort=size&order=asc">Size</a>
{{end}}
</th>
<th class="hideable">
{{if and (eq .Sort "time") (ne .Order "desc")}}
<a href="?sort=time&order=desc">Modified &#9650;</a>
{{else if and (eq .Sort "time") (ne .Order "asc")}}
<a href="?sort=time&order=asc">Modified &#9660;</a>
{{else}}
<a href="?sort=time&order=asc">Modified</a>
{{end}}
</th>
</tr>
{{range .Items}}
<tr>
<td>
{{if .IsDir}}&#128194;<a href="{{.URL}}">{{.Name}}</a>{{else}}
&#128196;<a href="/admin/edit{{ $path }}{{.URL}}">{{.Name}}</a>{{end}}
</td>
<td>{{.HumanSize}}</td>
<td class="hideable">{{.HumanModTime "01/02/2006 3:04:05 PM -0700"}}</td>
</tr>
{{end}}
</table>
</div>
</main>
{{ end }}