From 6f07c75f2ae1c3b164b66c2d7ed5fde242f8a744 Mon Sep 17 00:00:00 2001 From: Feuerfuchs Date: Thu, 20 Jun 2019 18:16:12 +0200 Subject: Add navigation header --- assets/style.css | 2 +- css/main.scss | 50 ++++++++++++++++++++++++++++++++++---------------- go.sum | 3 +++ gopherproxy.go | 17 +++++++++++++---- template.go | 22 ++++++++++++++++++++++ 5 files changed, 73 insertions(+), 21 deletions(-) diff --git a/assets/style.css b/assets/style.css index 665be39..8841dae 100644 --- a/assets/style.css +++ b/assets/style.css @@ -1 +1 @@ -@font-face{font-family:'Iosevka Term SS03';font-style:normal;font-weight:normal;src:url("/iosevka-term-ss03-regular.woff2") format("woff2"),url("/iosevka-term-ss03-regular.woff") format("woff")}body{margin:0;padding:0;background-color:#14171a;color:#cad1d8}.wrap{text-align:center}.content{display:inline-block;min-width:50em;min-width:85ch;margin:0;padding:2em 1em;text-align:left;font-family:'Iosevka Term SS03', 'IBM Plex Mono', 'Fira Code', 'Fira Mono', 'Roboto Mono', 'Droid Sans Mono', Monaco, Consolas, Courier, monospace;font-size:1.0625em;line-height:1.5}::selection{color:#000;background-color:rgba(239,198,138,0.996)}:link{color:#fff}:visited{color:#cad1d8}.link-type{color:#929ba3} +@font-face{font-family:'Iosevka Term SS03';font-style:normal;font-weight:normal;src:url("/iosevka-term-ss03-regular.woff2") format("woff2"),url("/iosevka-term-ss03-regular.woff") format("woff")}body{margin:0;padding:0;background-color:#14171a;color:#cad1d8}::selection{color:#000;background-color:rgba(239,198,138,0.996)}:link{color:#fff}:visited{color:#cad1d8}.header{padding:1em;border-bottom:1px solid #353a3f;font-family:'Iosevka Term SS03', 'IBM Plex Mono', 'Fira Code', 'Fira Mono', 'Roboto Mono', 'Droid Sans Mono', Monaco, Consolas, Courier, monospace;font-size:1.0625em;line-height:1.1;color:#686f76}.header__uripart{color:#929ba3}.header__uripart--last{color:#fff}.wrap{text-align:center}.content{display:inline-block;min-width:50em;min-width:85ch;margin:0;padding:2em 1em;text-align:left;font-family:'Iosevka Term SS03', 'IBM Plex Mono', 'Fira Code', 'Fira Mono', 'Roboto Mono', 'Droid Sans Mono', Monaco, Consolas, Courier, monospace;font-size:1.0625em;line-height:1.5}.link-type{color:#929ba3} diff --git a/css/main.scss b/css/main.scss index 367f2d7..61787b5 100644 --- a/css/main.scss +++ b/css/main.scss @@ -6,6 +6,7 @@ $text-faint: mix(hsl(210, 100%, 95%), $background, 40%); $text-plus: #fff; $link-idle: $text-plus; $link-visited: $text; +$border: mix(hsl(210, 100%, 95%), $background, 16%); $sel-background: rgba($accent, .996); $sel-text: #000; @@ -30,22 +31,6 @@ body { color: $text; } -.wrap { - text-align: center; -} - -.content { - display: inline-block; - min-width: 50em; - min-width: 5ch + 80; - margin: 0; - padding: 2em 1em; - text-align: left; - font-family: 'Iosevka Term SS03', 'IBM Plex Mono', 'Fira Code', 'Fira Mono', 'Roboto Mono', 'Droid Sans Mono', Monaco, Consolas, Courier, monospace; - font-size: 1 / 16 * 17em; - line-height: 1.5; -} - ::selection { color: $sel-text; background-color: $sel-background; @@ -65,6 +50,39 @@ body { // } // } +.header { + padding: 1em; + border-bottom: 1px solid $border; + font-family: 'Iosevka Term SS03', 'IBM Plex Mono', 'Fira Code', 'Fira Mono', 'Roboto Mono', 'Droid Sans Mono', Monaco, Consolas, Courier, monospace; + font-size: 1 / 16 * 17em; + line-height: 1.1; + color: $text-faint; +} + +.header__uripart { + color: $text-minus; +} + +.header__uripart--last { + color: $text-plus; +} + +.wrap { + text-align: center; +} + +.content { + display: inline-block; + min-width: 50em; + min-width: 5ch + 80; + margin: 0; + padding: 2em 1em; + text-align: left; + font-family: 'Iosevka Term SS03', 'IBM Plex Mono', 'Fira Code', 'Fira Mono', 'Roboto Mono', 'Droid Sans Mono', Monaco, Consolas, Courier, monospace; + font-size: 1 / 16 * 17em; + line-height: 1.5; +} + .link-type { color: $text-minus; } diff --git a/go.sum b/go.sum index 7eb5a57..89aa345 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,5 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gobuffalo/attrs v0.0.0-20190224210810-a9411de4debd/go.mod h1:4duuawTqi2wkkpB4ePgWMaai6/Kc6WEz83bhFwpHzj0= github.com/gobuffalo/depgen v0.0.0-20190329151759-d478694a28d3/go.mod h1:3STtPUQYuzV0gBVOY3vy6CfMm/ljR4pABfrTeHNLHUY= @@ -35,6 +36,7 @@ github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kN github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prologic/go-gopher v0.0.0-20181230133552-0c68ed5f58b0 h1:10LO/S8HVjIuEHsHea//Cena1Ztgy23f/e8HFC0w5ow= github.com/prologic/go-gopher v0.0.0-20181230133552-0c68ed5f58b0/go.mod h1:LiuwIXz4es4YIUOD6yRv8mES9n9dFbe4z0+TcrLkhXg= @@ -49,6 +51,7 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/temoto/robotstxt v0.0.0-20180810133444-97ee4a9ee6ea h1:hH8P1IiDpzRU6ZDbDh/RDnVuezi2oOXJpApa06M0zyI= github.com/temoto/robotstxt v0.0.0-20180810133444-97ee4a9ee6ea/go.mod h1:aOux3gHPCftJ3KHq6Pz/AlDjYJ7Y+yKfm1gU/3B0u04= diff --git a/gopherproxy.go b/gopherproxy.go index 961806a..8ba515e 100644 --- a/gopherproxy.go +++ b/gopherproxy.go @@ -25,7 +25,7 @@ type Item struct { Text string } -func renderDirectory(w http.ResponseWriter, tpl *template.Template, styletext string, hostport string, d gopher.Directory) error { +func renderDirectory(w http.ResponseWriter, tpl *template.Template, styletext string, uri string, hostport string, d gopher.Directory) error { var title string out := make([]Item, len(d.Items)) @@ -76,10 +76,11 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, styletext st return tpl.Execute(w, struct { Title string + URI string Style string Lines []Item RawText string - }{title, styletext, out, ""}) + }{title, fmt.Sprintf("%s/%s", hostport, uri), styletext, out, ""}) } // GopherHandler returns a Handler that proxies requests @@ -137,15 +138,16 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, rob buf.ReadFrom(res.Body) tpl.Execute(w, struct { Title string + URI string Style string RawText string Lines []Item - }{uri, styletext, buf.String(), nil}) + }{uri, fmt.Sprintf("%s/%s", hostport, uri), styletext, buf.String(), nil}) } else { io.Copy(w, res.Body) } } else { - if err := renderDirectory(w, tpl, styletext, hostport, res.Dir); err != nil { + if err := renderDirectory(w, tpl, styletext, uri, hostport, res.Dir); err != nil { io.WriteString(w, fmt.Sprintf("Error:
%s
", err)) return } @@ -258,6 +260,13 @@ func ListenAndServe(bind, robotsfile string, robotsdebug bool, uri string) error "HTMLEscape": func(s string) string { return html.EscapeString(s) }, + "split": strings.Split, + "last": func(s []string) string { + return s[len(s)-1] + }, + "pop": func(s []string) []string { + return s[:len(s)-1] + }, } tpl, err = template.New("gophermenu").Funcs(funcMap).Parse(tpltext) diff --git a/template.go b/template.go index 781d2bb..b1dbaf1 100644 --- a/template.go +++ b/template.go @@ -12,6 +12,28 @@ var tpltext = ` +
+ {{- $href := "" -}} + {{- $uriParts := split .URI "/" -}} + {{- $uriLast := $uriParts | last -}} + {{- $uriParts = $uriParts | pop -}} + {{- if eq $uriLast "" -}} + {{- $uriLast = $uriParts | last -}} + {{- $uriParts = $uriParts | pop -}} + {{- end -}} + + {{- range $i, $part := $uriParts -}} + {{- if ne $i 1 -}} + {{- $href = printf "%s/%s" $href . -}} + {{- if ne $i 0 }} / {{ end -}} + {{ . }} + {{- else -}} + {{- $href = printf "%s/1" $href -}} + {{- end -}} + {{- end -}} + {{- if ne (len $uriParts) 0 }} / {{ end -}} + {{ $uriLast -}} +
         {{- if .Lines -}}
-- 
cgit v1.2.3-70-g09d2