package main import ( "fmt" "html" "net/url" "strings" "git.sr.ht/~adnano/go-gemini" ) type ConvertedGmiDoc struct { Content string Title string } func textToHTML(reqUrl *url.URL, text gemini.Text) ConvertedGmiDoc { var b strings.Builder var pre bool var list bool var title string for _, l := range text { if _, ok := l.(gemini.LineListItem); ok { if !list { list = true fmt.Fprint(&b, "\n") } switch l.(type) { case gemini.LineLink: link := l.(gemini.LineLink) urlstring := html.EscapeString(link.URL) // u = ctx.URL.ResolveReference(u) ? u, err := url.Parse(urlstring) if err != nil { continue } if reqUrl != nil { u = reqUrl.ResolveReference(u) } if u.Scheme == "gemini" || (reqUrl != nil && u.Scheme == "") { if strings.HasSuffix(u.Host, c.Host) { u.Scheme = "" urlstring = html.EscapeString(u.String()) } else { u.Path = fmt.Sprintf("/%s%s", u.Host, u.Path) u.Scheme = "" u.Host = "proxy." + c.Host urlstring = html.EscapeString(u.String()) } } name := html.EscapeString(link.Name) if name == "" { name = urlstring } fmt.Fprintf(&b, "

%s

\n", urlstring, name) case gemini.LinePreformattingToggle: pre = !pre if pre { altText := string(l.(gemini.LinePreformattingToggle)) if altText != "" { altText = html.EscapeString(altText) fmt.Fprintf(&b, "
\n", altText)
				} else {
					fmt.Fprint(&b, "
\n")
				}
			} else {
				fmt.Fprint(&b, "
\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)) if title == "" { title = text } // TODO deal with repetition case gemini.LineHeading2: text := string(l.(gemini.LineHeading2)) fmt.Fprintf(&b, "

%s

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

%s

\n", html.EscapeString(text)) if title == "" { title = 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 == "" { fmt.Fprint(&b, "
    \n") } else { fmt.Fprintf(&b, "

    %s

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