Commit 63b39c78 authored by Matthew Holt's avatar Matthew Holt

Better default template; other fixes

parent 13d9bcc0
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/dustin/go-humanize"
"github.com/mholt/caddy/middleware" "github.com/mholt/caddy/middleware"
) )
...@@ -55,6 +56,14 @@ type FileInfo struct { ...@@ -55,6 +56,14 @@ type FileInfo struct {
Mode os.FileMode Mode os.FileMode
} }
func (fi FileInfo) HumanSize() string {
return humanize.Bytes(uint64(fi.Size))
}
func (fi FileInfo) HumanModTime(format string) string {
return fi.ModTime.Format(format)
}
var IndexPages = []string{ var IndexPages = []string{
"index.html", "index.html",
"index.htm", "index.htm",
...@@ -205,30 +214,17 @@ func parse(c middleware.Controller) ([]BrowseConfig, error) { ...@@ -205,30 +214,17 @@ func parse(c middleware.Controller) ([]BrowseConfig, error) {
for c.Next() { for c.Next() {
var bc BrowseConfig var bc BrowseConfig
if !c.NextArg() { // First argument is directory to allow browsing; default is site root
if c.NextArg() {
bc.PathScope = c.Val()
} else {
bc.PathScope = "/" bc.PathScope = "/"
err := appendCfg(bc)
if err != nil {
return configs, err
}
continue
} }
bc.PathScope = c.Val() // Second argument would be the template file to use
if !c.NextArg() {
err := appendCfg(bc)
if err != nil {
return configs, err
}
continue
}
tplFile := c.Val()
var tplText string var tplText string
if c.NextArg() {
if tplFile != "" { tplBytes, err := ioutil.ReadFile(c.Val())
tplBytes, err := ioutil.ReadFile(tplFile)
if err != nil { if err != nil {
return configs, err return configs, err
} }
...@@ -237,12 +233,14 @@ func parse(c middleware.Controller) ([]BrowseConfig, error) { ...@@ -237,12 +233,14 @@ func parse(c middleware.Controller) ([]BrowseConfig, error) {
tplText = defaultTemplate tplText = defaultTemplate
} }
// Build the template
tpl, err := template.New("listing").Parse(tplText) tpl, err := template.New("listing").Parse(tplText)
if err != nil { if err != nil {
return configs, err return configs, err
} }
bc.Template = tpl bc.Template = tpl
// Save configuration
err = appendCfg(bc) err = appendCfg(bc)
if err != nil { if err != nil {
return configs, err return configs, err
...@@ -251,9 +249,3 @@ func parse(c middleware.Controller) ([]BrowseConfig, error) { ...@@ -251,9 +249,3 @@ func parse(c middleware.Controller) ([]BrowseConfig, error) {
return configs, nil return configs, nil
} }
const defaultTemplate = `
{{range .}}
{{.Name}}<br>
{{end}}
`
package browse
// The default template to use when serving up directory listings
const defaultTemplate = `<!DOCTYPE html>
<html>
<head>
<title>{{.Name}}</title>
<meta charset="utf-8">
<style>
* { padding: 0; margin: 0; }
body {
padding: 1% 2%;
font: 16px sans-serif;
}
header {
font-size: 45px;
padding: 25px;
}
header a {
text-decoration: none;
color: inherit;
}
header .up {
display: inline-block;
height: 50px;
width: 50px;
text-align: center;
margin-right: 20px;
}
header a.up:hover {
background: #000;
color: #FFF;
}
h1 {
font-size: 30px;
display: inline;
}
table {
border: 0;
border-collapse: collapse;
max-width: 750px;
margin: 0 auto;
}
th,
td {
padding: 4px 20px;
vertical-align: middle;
line-height: 1.5em; /* emoji are kind of odd heights */
}
th {
text-align: left;
}
@media (max-width: 650px) {
.hideable {
display: none;
}
body {
padding: 0;
}
header,
header h1 {
font-size: 14px;
}
header {
position: fixed;
top: 0;
width: 100%;
background: #333;
color: #FFF;
padding: 10px;
text-align: center;
}
header .up {
height: auto;
width: auto;
display: none;
}
header a.up {
display: inline-block;
position: absolute;
left: 0;
top: 0;
width: 35px;
height: 28px;
font-size: 35px;
}
header h1 {
font-weight: normal;
}
main {
margin-top: 50px;
}
}
</style>
</head>
<body>
<header>
{{if .CanGoUp}}
<a href=".." class="up" title="Up one level">&#11025;</a>
{{else}}
<div class="up">&nbsp;</div>
{{end}}
<h1>{{.Path}}</h1>
</header>
<main>
<table>
<tr>
<th>Name</th>
<th>Size</th>
<th class="hideable">Modified</th>
</tr>
{{range .Items}}
<tr>
<td>
{{if .IsDir}}&#128194;{{else}}&#128196;{{end}}
<a href="{{.URL}}">{{.Name}}</a>
</td>
<td>{{.HumanSize}}</td>
<td class="hideable">{{.HumanModTime "01/02/2006 3:04:05 PM -0700"}}</td>
</tr>
{{end}}
</table>
</main>
</body>
</html>`
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment