From 6f07c75f2ae1c3b164b66c2d7ed5fde242f8a744 Mon Sep 17 00:00:00 2001 From: Feuerfuchs Date: Thu, 20 Jun 2019 18:16:12 +0200 Subject: Add navigation header --- gopherproxy.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'gopherproxy.go') 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) -- cgit v1.2.3-54-g00ecf