Parse page title from first header line
alex wennerberg alex@alexwennerberg.com
Mon, 01 Feb 2021 22:11:04 -0800
3 files changed,
29 insertions(+),
8 deletions(-)
M
gmi2html.go
→
gmi2html.go
@@ -9,10 +9,16 @@
"git.sr.ht/~adnano/go-gemini" ) -func textToHTML(reqUrl *url.URL, text gemini.Text) string { +type ConvertedGmiDoc struct { + Content string + Title string +} + +func textToHTML(reqUrl *url.URL, text gemini.Text) ConvertedGmiDoc { var b strings.Builder var pre bool var list bool + var title string for _, l := range text { if _, ok := l.(gemini.LineListItem); ok { if !list {@@ -70,12 +76,21 @@ fmt.Fprintf(&b, "%s\n", html.EscapeString(text))
case gemini.LineHeading1: text := string(l.(gemini.LineHeading1)) fmt.Fprintf(&b, "<h1>%s</h1>\n", html.EscapeString(text)) + if title == "" { + title = text + } // TODO deal with repetition case gemini.LineHeading2: text := string(l.(gemini.LineHeading2)) fmt.Fprintf(&b, "<h2>%s</h2>\n", html.EscapeString(text)) + if title == "" { + title = text + } case gemini.LineHeading3: text := string(l.(gemini.LineHeading3)) fmt.Fprintf(&b, "<h3>%s</h3>\n", html.EscapeString(text)) + if title == "" { + title = text + } case gemini.LineListItem: text := string(l.(gemini.LineListItem)) fmt.Fprintf(&b, "<li>%s</li>\n", html.EscapeString(text))@@ -97,5 +112,8 @@ }
if list { fmt.Fprint(&b, "</ul>\n") } - return b.String() + return ConvertedGmiDoc{ + b.String(), + title, + } }
M
http.go
→
http.go
@@ -572,15 +572,15 @@ // Dumb content negotiation
_, raw := r.URL.Query()["raw"] acceptsGemini := strings.Contains(r.Header.Get("Accept"), "text/gemini") if !raw && !acceptsGemini && (isGemini(fullPath) || geminiContent != "") { - var htmlString string + var htmlDoc ConvertedGmiDoc if geminiContent == "" { file, _ := os.Open(fullPath) parse, _ := gmi.ParseText(file) - htmlString = textToHTML(nil, parse) + htmlDoc = textToHTML(nil, parse) defer file.Close() } else { parse, _ := gmi.ParseText(strings.NewReader(geminiContent)) - htmlString = textToHTML(nil, parse) + htmlDoc = textToHTML(nil, parse) } favicon := getFavicon(userName) hostname := strings.Split(r.Host, ":")[0]@@ -589,6 +589,9 @@ Scheme: "gemini",
Host: hostname, Path: p, } + if htmlDoc.Title == "" { + htmlDoc.Title = userName + p + } data := struct { SiteBody template.HTML Favicon string@@ -596,7 +599,7 @@ PageTitle string
URI *url.URL GeminiURI *url.URL Config Config - }{template.HTML(htmlString), favicon, userName + p, &uri, &uri, c} + }{template.HTML(htmlDoc.Content), favicon, htmlDoc.Title, &uri, &uri, c} err = t.ExecuteTemplate(w, "user_page.html", data) if err != nil { panic(err)
M
proxy.go
→
proxy.go
@@ -116,7 +116,7 @@ }
w.Header().Add("Content-Type", "text/html") parse, _ := gemini.ParseText(resp.Body) - htmlString := textToHTML(req.URL, parse) + htmlDoc := textToHTML(req.URL, parse) if strings.HasSuffix(r.URL.Path, "/") { r.URL.Path = path.Dir(r.URL.Path) }@@ -127,7 +127,7 @@ PageTitle string
GeminiURI *url.URL URI *url.URL Config Config - }{template.HTML(htmlString), "", r.URL.String(), req.URL, r.URL, c} + }{template.HTML(htmlDoc.Content), "", r.URL.String(), req.URL, r.URL, c} err = t.ExecuteTemplate(w, "user_page.html", data) if err != nil {