all repos — flounder @ 2799ede93e92daa87144e345d5b086aac4e8c037

A small site builder for the Gemini protocol

add inline images and preformatted alt text
alex wennerberg alex@alexwennerberg.com
Thu, 05 Nov 2020 22:30:35 -0800
commit

2799ede93e92daa87144e345d5b086aac4e8c037

parent

2b6bd994fd8d356226f5d5e365f5628280a5c318

1 files changed, 21 insertions(+), 3 deletions(-)

jump to
M gmi2html.gogmi2html.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")