From ac770231436f8a17d348a6a0ab934429df3c57d0 Mon Sep 17 00:00:00 2001 From: Feuerfuchs Date: Mon, 18 May 2020 16:12:43 +0200 Subject: WIP: Refactoring --- internal/port/gopher.go | 138 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 102 insertions(+), 36 deletions(-) (limited to 'internal/port/gopher.go') diff --git a/internal/port/gopher.go b/internal/port/gopher.go index abbc4d9..d2283c6 100644 --- a/internal/port/gopher.go +++ b/internal/port/gopher.go @@ -17,16 +17,73 @@ import ( "github.com/temoto/robotstxt" ) -type Item struct { +type gopherTemplateVariables struct { + Title string + URL string + Assets AssetList + Lines []GopherItem + Nav []GopherNavItem + IsPlain bool +} + +type GopherNavItem struct { + Label string + URL string + Current bool +} + +type GopherItem struct { Link template.URL Type string Text string } +func trimLeftChars(s string, n int) string { + m := 0 + for i := range s { + if m >= n { + return s[i:] + } + m++ + } + return s[:0] +} + +func urlToNav(url string) (items []GopherNavItem) { + partialURL := "/gopher" + parts := strings.Split(url, "/") + + for i, part := range parts { + if i == 1 { + partialURL = partialURL + "/1" + part = trimLeftChars(part, 1) + + if part == "" { + continue + } + } else { + partialURL = partialURL + "/" + part + } + + current := false + if i == len(parts)-1 || (len(parts) == 2 && i == 0) { + current = true + } + + items = append(items, GopherNavItem{ + Label: part, + URL: partialURL, + Current: current, + }) + } + + return +} + func renderGopherDirectory(w http.ResponseWriter, tpl *template.Template, assetList AssetList, uri string, hostport string, d libgopher.Directory) error { var title string - out := make([]Item, len(d.Items)) + out := make([]GopherItem, len(d.Items)) for i, x := range d.Items { if x.Type == libgopher.INFO && x.Selector == "TITLE" { @@ -34,7 +91,7 @@ func renderGopherDirectory(w http.ResponseWriter, tpl *template.Template, assetL continue } - tr := Item{ + tr := GopherItem{ Text: x.Description, Type: x.Type.String(), } @@ -89,12 +146,12 @@ func renderGopherDirectory(w http.ResponseWriter, tpl *template.Template, assetL } } - return tpl.Execute(w, TemplateVariables{ - Title: title, - URI: fmt.Sprintf("%s/%s", hostport, uri), - Assets: assetList, - Lines: out, - Protocol: "gopher", + return tpl.Execute(w, gopherTemplateVariables{ + Title: title, + URL: fmt.Sprintf("%s/%s", hostport, uri), + Assets: assetList, + Lines: out, + Nav: urlToNav(fmt.Sprintf("%s/%s", hostport, uri)), }) } @@ -130,13 +187,15 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass uri, err := url.QueryUnescape(strings.Join(parts[1:], "/")) if err != nil { - if e := tpl.Execute(w, TemplateVariables{ - Title: title, - URI: hostport, - Assets: assetList, - RawText: fmt.Sprintf("Error: %s", err), - Error: true, - Protocol: "gopher", + if e := tpl.Execute(w, gopherTemplateVariables{ + Title: title, + URL: hostport, + Assets: assetList, + Lines: []GopherItem{{ + Text: fmt.Sprintf("Error: %s", err), + }}, + Nav: urlToNav(hostport), + IsPlain: true, }); e != nil { log.Println("Template error: " + e.Error()) log.Println(err.Error()) @@ -158,13 +217,15 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass ) if err != nil { - if e := tpl.Execute(w, TemplateVariables{ - Title: title, - URI: fmt.Sprintf("%s/%s", hostport, uri), - Assets: assetList, - RawText: fmt.Sprintf("Error: %s", err), - Error: true, - Protocol: "gopher", + if e := tpl.Execute(w, gopherTemplateVariables{ + Title: title, + URL: fmt.Sprintf("%s/%s", hostport, uri), + Assets: assetList, + Lines: []GopherItem{{ + Text: fmt.Sprintf("Error: %s", err), + }}, + Nav: urlToNav(fmt.Sprintf("%s/%s", hostport, uri)), + IsPlain: true, }); e != nil { log.Println("Template error: " + e.Error()) } @@ -178,12 +239,15 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass buf := new(bytes.Buffer) buf.ReadFrom(res.Body) - if err := tpl.Execute(w, TemplateVariables{ - Title: title, - URI: fmt.Sprintf("%s/%s", hostport, uri), - Assets: assetList, - RawText: buf.String(), - Protocol: "gopher", + if err := tpl.Execute(w, gopherTemplateVariables{ + Title: title, + URL: fmt.Sprintf("%s/%s", hostport, uri), + Assets: assetList, + Lines: []GopherItem{{ + Text: buf.String(), + }}, + Nav: urlToNav(fmt.Sprintf("%s/%s", hostport, uri)), + IsPlain: true, }); err != nil { log.Println("Template error: " + err.Error()) } @@ -200,13 +264,15 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, ass } } else { if err := renderGopherDirectory(w, tpl, assetList, uri, hostport, res.Dir); err != nil { - if e := tpl.Execute(w, TemplateVariables{ - Title: title, - URI: fmt.Sprintf("%s/%s", hostport, uri), - Assets: assetList, - RawText: fmt.Sprintf("Error: %s", err), - Error: true, - Protocol: "gopher", + if e := tpl.Execute(w, gopherTemplateVariables{ + Title: title, + URL: fmt.Sprintf("%s/%s", hostport, uri), + Assets: assetList, + Lines: []GopherItem{{ + Text: fmt.Sprintf("Error: %s", err), + }}, + Nav: urlToNav(fmt.Sprintf("%s/%s", hostport, uri)), + IsPlain: false, }); e != nil { log.Println("Template error: " + e.Error()) log.Println(e.Error()) -- cgit v1.3.1