From c44f011b15f92373d58b451c8620d99fcf3c4296 Mon Sep 17 00:00:00 2001 From: Adnan Maolood Date: Mon, 26 Oct 2020 12:49:16 -0400 Subject: [PATCH] Remove (Text).HTML function --- examples/html.go | 90 ++++++++++++++++++++++++++++++++++++++++++++++++ text.go | 68 ------------------------------------ 2 files changed, 90 insertions(+), 68 deletions(-) create mode 100644 examples/html.go diff --git a/examples/html.go b/examples/html.go new file mode 100644 index 0000000..fe59c87 --- /dev/null +++ b/examples/html.go @@ -0,0 +1,90 @@ +// +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.(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/text.go b/text.go index e41424a..c35f503 100644 --- a/text.go +++ b/text.go @@ -3,7 +3,6 @@ package gemini import ( "bufio" "fmt" - "html" "io" "strings" ) @@ -151,70 +150,3 @@ func (t Text) String() string { } return b.String() } - -// HTML returns the Gemini text response as HTML. -func (t Text) HTML() string { - var b strings.Builder - var pre bool - var list bool - for _, l := range t { - if _, ok := l.(LineListItem); ok { - if !list { - list = true - fmt.Fprint(&b, "\n") - } - switch l.(type) { - case LineLink: - link := l.(LineLink) - url := html.EscapeString(link.URL) - name := html.EscapeString(link.Name) - if name == "" { - name = url - } - fmt.Fprintf(&b, "

    %s

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

    %s

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

    %s

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

    %s

    \n", html.EscapeString(text)) - case LineListItem: - text := string(l.(LineListItem)) - fmt.Fprintf(&b, "
  • %s
  • \n", html.EscapeString(text)) - case LineQuote: - text := string(l.(LineQuote)) - fmt.Fprintf(&b, "
    %s
    \n", html.EscapeString(text)) - case LineText: - text := string(l.(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() -}