// +build ignore // This example illustrates a gemtext to HTML converter. package main import ( "fmt" "html" "strings" "git.sr.ht/~adnano/go-gemini" ) func main() { text := gemini.Text{ gemini.LineHeading1("Hello, world!"), gemini.LineText("This is a gemini text document."), } html := textToHTML(text) fmt.Print(html) } // textToHTML returns the Gemini text response as HTML. func textToHTML(text gemini.Text) string { var b strings.Builder var pre bool var list bool for _, l := range text { if _, ok := l.(gemini.LineListItem); ok { if !list { list = true fmt.Fprint(&b, "\n") } switch l := l.(type) { case gemini.LineLink: url := html.EscapeString(l.URL) name := html.EscapeString(l.Name) if name == "" { name = url } fmt.Fprintf(&b, "

%s

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

%s

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

%s

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

%s

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

    %s

    \n", html.EscapeString(string(l))) } } } if pre { fmt.Fprint(&b, "\n") } if list { fmt.Fprint(&b, "\n") } return b.String() }