flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit 8bdbde659ef9a0b517d88a38caf080dfcb4cbe9a
parent 5b4a8b771c8c011af9c4f60ce0725225545b325d
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Mon,  1 Feb 2021 22:11:04 -0800

Parse page title from first header line

Diffstat:
Mgmi2html.go | 22++++++++++++++++++++--
Mhttp.go | 11+++++++----
Mproxy.go | 4++--
3 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/gmi2html.go b/gmi2html.go @@ -9,10 +9,16 @@ import ( "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 @@ func textToHTML(reqUrl *url.URL, text gemini.Text) string { 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 @@ func textToHTML(reqUrl *url.URL, text gemini.Text) string { if list { fmt.Fprint(&b, "</ul>\n") } - return b.String() + return ConvertedGmiDoc{ + b.String(), + title, + } } diff --git a/http.go b/http.go @@ -572,15 +572,15 @@ func userFile(w http.ResponseWriter, r *http.Request) { _, 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 @@ func userFile(w http.ResponseWriter, r *http.Request) { Host: hostname, Path: p, } + if htmlDoc.Title == "" { + htmlDoc.Title = userName + p + } data := struct { SiteBody template.HTML Favicon string @@ -596,7 +599,7 @@ func userFile(w http.ResponseWriter, r *http.Request) { 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) diff --git a/proxy.go b/proxy.go @@ -116,7 +116,7 @@ func proxyGemini(w http.ResponseWriter, r *http.Request) { 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 @@ func proxyGemini(w http.ResponseWriter, r *http.Request) { 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 {