From 5bdbb822e35e06c6b7b051fc79db34462345798b Mon Sep 17 00:00:00 2001 From: adnano Date: Mon, 26 Oct 2020 15:18:26 -0400 Subject: [PATCH] Update go-gemini dependency --- go.mod | 2 +- go.sum | 4 +-- html.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 2 +- 4 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 html.go diff --git a/go.mod b/go.mod index a94e4c4..7734465 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module kiln go 1.15 -require git.sr.ht/~adnano/go-gemini v0.1.0 +require git.sr.ht/~adnano/go-gemini v0.1.1 diff --git a/go.sum b/go.sum index bab6f79..811d3ca 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -git.sr.ht/~adnano/go-gemini v0.1.0 h1:UqRT90kR+/8q5s/JemmDPH6A9VocBezIuWbOpZYBypU= -git.sr.ht/~adnano/go-gemini v0.1.0/go.mod h1:If1VxEWcZDrRt5FeAFnGTcM2Ud1E3BXs3VJ5rnZWKq0= +git.sr.ht/~adnano/go-gemini v0.1.1 h1:g6OwUxLviy6dkPiuW2eRQP5Fow412vUsKmKYbCr2H9U= +git.sr.ht/~adnano/go-gemini v0.1.1/go.mod h1:If1VxEWcZDrRt5FeAFnGTcM2Ud1E3BXs3VJ5rnZWKq0= diff --git a/html.go b/html.go new file mode 100644 index 0000000..77673c8 --- /dev/null +++ b/html.go @@ -0,0 +1,76 @@ +package main + +import ( + "fmt" + "html" + "strings" + + "git.sr.ht/~adnano/go-gemini" +) + +// 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.(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 { + 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)) + 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 == "" { + 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 b.String() +} diff --git a/main.go b/main.go index f2e0a0d..cf8eff1 100644 --- a/main.go +++ b/main.go @@ -446,7 +446,7 @@ func outputHTML(p *Page) (path string, content []byte) { r := bytes.NewReader(p.content) text := gemini.Parse(r) - content = []byte(text.HTML()) + content = []byte(textToHTML(text)) type tmplCtx struct { Title string