all repos — flounder @ 758676c26d0a87c9354a795aed370bc06b91be6b

A small site builder for the Gemini protocol

gmi2html.go (view raw)

  1package main
  2
  3import (
  4	"fmt"
  5	"html"
  6	"net/url"
  7	"strings"
  8
  9	"git.sr.ht/~adnano/go-gemini"
 10)
 11
 12func textToHTML(reqUrl *url.URL, text gemini.Text) string {
 13	var b strings.Builder
 14	var pre bool
 15	var list bool
 16	for _, l := range text {
 17		if _, ok := l.(gemini.LineListItem); ok {
 18			if !list {
 19				list = true
 20				fmt.Fprint(&b, "<ul>\n")
 21			}
 22		} else if list {
 23			list = false
 24			fmt.Fprint(&b, "</ul>\n")
 25		}
 26		switch l.(type) {
 27		case gemini.LineLink:
 28			link := l.(gemini.LineLink)
 29			urlstring := html.EscapeString(link.URL)
 30			// u = ctx.URL.ResolveReference(u) ?
 31			u, err := url.Parse(urlstring)
 32			if err != nil {
 33				continue
 34			}
 35			if reqUrl != nil {
 36				u = reqUrl.ResolveReference(u)
 37			}
 38			if u.Scheme == "gemini" || (reqUrl != nil && u.Scheme == "") {
 39				if strings.HasSuffix(u.Host, c.Host) {
 40					u.Scheme = ""
 41					urlstring = html.EscapeString(u.String())
 42				} else {
 43					u.Path = fmt.Sprintf("/%s%s", u.Host, u.Path)
 44					u.Scheme = ""
 45					u.Host = "proxy." + c.Host
 46					urlstring = html.EscapeString(u.String())
 47				}
 48			}
 49			name := html.EscapeString(link.Name)
 50			if name == "" {
 51				name = urlstring
 52			}
 53			fmt.Fprintf(&b, "<p><a href='%s'>%s</a></p>\n", urlstring, name)
 54		case gemini.LinePreformattingToggle:
 55			pre = !pre
 56			if pre {
 57				altText := string(l.(gemini.LinePreformattingToggle))
 58				if altText != "" {
 59					altText = html.EscapeString(altText)
 60					fmt.Fprintf(&b, "<pre title='%s'>\n", altText)
 61				} else {
 62					fmt.Fprint(&b, "<pre>\n")
 63				}
 64			} else {
 65				fmt.Fprint(&b, "</pre>\n")
 66			}
 67		case gemini.LinePreformattedText:
 68			text := string(l.(gemini.LinePreformattedText))
 69			fmt.Fprintf(&b, "%s\n", html.EscapeString(text))
 70		case gemini.LineHeading1:
 71			text := string(l.(gemini.LineHeading1))
 72			fmt.Fprintf(&b, "<h1>%s</h1>\n", html.EscapeString(text))
 73		case gemini.LineHeading2:
 74			text := string(l.(gemini.LineHeading2))
 75			fmt.Fprintf(&b, "<h2>%s</h2>\n", html.EscapeString(text))
 76		case gemini.LineHeading3:
 77			text := string(l.(gemini.LineHeading3))
 78			fmt.Fprintf(&b, "<h3>%s</h3>\n", html.EscapeString(text))
 79		case gemini.LineListItem:
 80			text := string(l.(gemini.LineListItem))
 81			fmt.Fprintf(&b, "<li>%s</li>\n", html.EscapeString(text))
 82		case gemini.LineQuote:
 83			text := string(l.(gemini.LineQuote))
 84			fmt.Fprintf(&b, "<blockquote>%s</blockquote>\n", html.EscapeString(text))
 85		case gemini.LineText:
 86			text := string(l.(gemini.LineText))
 87			if text == "" {
 88				fmt.Fprint(&b, "<br>\n")
 89			} else {
 90				fmt.Fprintf(&b, "<p>%s</p>\n", html.EscapeString(text))
 91			}
 92		}
 93	}
 94	if pre {
 95		fmt.Fprint(&b, "</pre>\n")
 96	}
 97	if list {
 98		fmt.Fprint(&b, "</ul>\n")
 99	}
100	return b.String()
101}