all repos — flounder @ 521dc799fb2e4b2080eb37f44115a2f2c6536a18

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