package main import ( "bytes" "fmt" "html" "git.sr.ht/~adnano/go-gemini" ) // textToHTML returns the Gemini text response as HTML. func textToHTML(text gemini.Text) []byte { var b bytes.Buffer var pre bool var list bool for _, l := range text { if _, ok := l.(gemini.LineListItem); ok { if !list { list = true b.WriteString("\n") } switch l.(type) { case gemini.LineLink: link := l.(gemini.LineLink) url := html.EscapeString(link.URL) name := html.EscapeString(link.Name) if name == "" { name = url } fmt.Fprintf(&b, "

%s

\n", url, name) case gemini.LinePreformattingToggle: pre = !pre if pre { b.WriteString("
\n")
			} else {
				b.WriteString("
\n") } case gemini.LinePreformattedText: text := string(l.(gemini.LinePreformattedText)) fmt.Fprintf(&b, "%s\n", html.EscapeString(text)) case gemini.LineHeading1: text := string(l.(gemini.LineHeading1)) fmt.Fprintf(&b, "

%s

\n", html.EscapeString(text)) case gemini.LineHeading2: text := string(l.(gemini.LineHeading2)) fmt.Fprintf(&b, "

%s

\n", html.EscapeString(text)) case gemini.LineHeading3: text := string(l.(gemini.LineHeading3)) fmt.Fprintf(&b, "

%s

\n", html.EscapeString(text)) case gemini.LineListItem: text := string(l.(gemini.LineListItem)) fmt.Fprintf(&b, "
  • %s
  • \n", html.EscapeString(text)) case gemini.LineQuote: text := string(l.(gemini.LineQuote)) fmt.Fprintf(&b, "
    %s
    \n", html.EscapeString(text)) case gemini.LineText: text := string(l.(gemini.LineText)) if text == "" { b.WriteString("
    \n") } else { fmt.Fprintf(&b, "

    %s

    \n", html.EscapeString(text)) } } } if pre { b.WriteString("\n") } if list { b.WriteString("\n") } return b.Bytes() }