flounder

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

commit 2799ede93e92daa87144e345d5b086aac4e8c037
parent 2b6bd994fd8d356226f5d5e365f5628280a5c318
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Thu,  5 Nov 2020 22:30:35 -0800

add inline images and preformatted alt text

Diffstat:
Mgmi2html.go | 24+++++++++++++++++++++---
1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/gmi2html.go b/gmi2html.go @@ -3,6 +3,9 @@ package main import ( "fmt" "html" + "mime" + neturl "net/url" + "path" "strings" "git.sr.ht/~adnano/go-gemini" @@ -27,13 +30,28 @@ func textToHTML(text gemini.Text) string { link := l.(gemini.LineLink) url := html.EscapeString(link.URL) name := html.EscapeString(link.Name) - if name == "" { - name = url + var internal bool + parsedUrl, err := neturl.Parse(url) // simpler way to do this? + if err == nil { + // only show images inline if they are relative links + internal = parsedUrl.Host == "" + } + if strings.HasPrefix(mime.TypeByExtension(path.Ext(url)), "image") && internal { + fmt.Fprintf(&b, "<img src='%s' alt='%s'>\n", url, name) + } else { + if name == "" { + name = url + } + fmt.Fprintf(&b, "<p><a href='%s'>%s</a></p>\n", url, name) } - fmt.Fprintf(&b, "<p><a href='%s'>%s</a></p>\n", url, name) case gemini.LinePreformattingToggle: pre = !pre if pre { + altText := string(l.(gemini.LinePreformattingToggle)) + if altText != "" { + altText = html.EscapeString(altText) + fmt.Fprintf(&b, "<pre title='%s'>\n", altText) + } fmt.Fprint(&b, "<pre>\n") } else { fmt.Fprint(&b, "</pre>\n")