add inline images and preformatted alt text
alex wennerberg alex@alexwennerberg.com
Thu, 05 Nov 2020 22:30:35 -0800
1 files changed,
21 insertions(+),
3 deletions(-)
jump to
M
gmi2html.go
→
gmi2html.go
@@ -3,6 +3,9 @@
import ( "fmt" "html" + "mime" + neturl "net/url" + "path" "strings" "git.sr.ht/~adnano/go-gemini"@@ -27,13 +30,28 @@ case gemini.LineLink:
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 == "" } - fmt.Fprintf(&b, "<p><a href='%s'>%s</a></p>\n", url, name) + 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) + } 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")