all repos — flounder @ ce21fbbef51b224e3897cbcf2b4639a191c97e39

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			u = reqUrl.ResolveReference(u)
 36			if u.Scheme == "gemini" || u.Scheme == "" {
 37				if strings.HasSuffix(u.Host, c.Host) {
 38					u.Scheme = ""
 39					urlstring = html.EscapeString(u.String())
 40				} else {
 41					u.Path = fmt.Sprintf("/%s%s", u.Host, u.Path)
 42					u.Scheme = ""
 43					u.Host = "proxy." + c.Host
 44					urlstring = html.EscapeString(u.String())
 45				}
 46			}
 47			name := html.EscapeString(link.Name)
 48			if name == "" {
 49				name = urlstring
 50			}
 51			fmt.Fprintf(&b, "<p><a href='%s'>%s</a></p>\n", urlstring, name)
 52		case gemini.LinePreformattingToggle:
 53			pre = !pre
 54			if pre {
 55				altText := string(l.(gemini.LinePreformattingToggle))
 56				if altText != "" {
 57					altText = html.EscapeString(altText)
 58					fmt.Fprintf(&b, "<pre title='%s'>\n", altText)
 59				} else {
 60					fmt.Fprint(&b, "<pre>\n")
 61				}
 62			} else {
 63				fmt.Fprint(&b, "</pre>\n")
 64			}
 65		case gemini.LinePreformattedText:
 66			text := string(l.(gemini.LinePreformattedText))
 67			fmt.Fprintf(&b, "%s\n", html.EscapeString(text))
 68		case gemini.LineHeading1:
 69			text := string(l.(gemini.LineHeading1))
 70			fmt.Fprintf(&b, "<h1>%s</h1>\n", html.EscapeString(text))
 71		case gemini.LineHeading2:
 72			text := string(l.(gemini.LineHeading2))
 73			fmt.Fprintf(&b, "<h2>%s</h2>\n", html.EscapeString(text))
 74		case gemini.LineHeading3:
 75			text := string(l.(gemini.LineHeading3))
 76			fmt.Fprintf(&b, "<h3>%s</h3>\n", html.EscapeString(text))
 77		case gemini.LineListItem:
 78			text := string(l.(gemini.LineListItem))
 79			fmt.Fprintf(&b, "<li>%s</li>\n", html.EscapeString(text))
 80		case gemini.LineQuote:
 81			text := string(l.(gemini.LineQuote))
 82			fmt.Fprintf(&b, "<blockquote>%s</blockquote>\n", html.EscapeString(text))
 83		case gemini.LineText:
 84			text := string(l.(gemini.LineText))
 85			if text == "" {
 86				fmt.Fprint(&b, "<br>\n")
 87			} else {
 88				fmt.Fprintf(&b, "<p>%s</p>\n", html.EscapeString(text))
 89			}
 90		}
 91	}
 92	if pre {
 93		fmt.Fprint(&b, "</pre>\n")
 94	}
 95	if list {
 96		fmt.Fprint(&b, "</ul>\n")
 97	}
 98	return b.String()
 99}
100
101// Convert a Gemini link to a proxy link
102func proxyLink() {
103}