From 246a99c28980e985bb4cff99042459bd5729cde1 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Thu, 31 Oct 2024 17:14:10 +0100 Subject: Move gemtext translation into its own package --- http/handlers/templates/functions/gemtext.go | 120 +++------------------------ 1 file changed, 12 insertions(+), 108 deletions(-) (limited to 'http/handlers/templates/functions') diff --git a/http/handlers/templates/functions/gemtext.go b/http/handlers/templates/functions/gemtext.go index 179d558..68a10ca 100644 --- a/http/handlers/templates/functions/gemtext.go +++ b/http/handlers/templates/functions/gemtext.go @@ -1,9 +1,6 @@ package functions import ( - "bufio" - "bytes" - "errors" "fmt" "html" "io" @@ -11,6 +8,7 @@ import ( "strings" "text/template" + "dev.mediocregopher.com/mediocre-caddy-plugins.git/internal/gemtext" "github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile" @@ -67,120 +65,26 @@ func sanitizeText(str string) string { return html.EscapeString(strings.TrimSpace(str)) } -type gemtextResult struct { - Title string - Body string -} - -func (g *Gemtext) funcGemtext(input any) (gemtextResult, error) { +func (g *Gemtext) funcGemtext(input any) (gemtext.HTML, error) { var ( - r = bufio.NewReader(strings.NewReader(caddy.ToString(input))) - w = new(bytes.Buffer) - title string - pft, list bool - writeErr error + r = strings.NewReader(caddy.ToString(input)) + translator gemtext.HTMLTranslator ) - write := func(fmtStr string, args ...any) { - if writeErr != nil { - return - } - fmt.Fprintf(w, fmtStr, args...) - } - -loop: - for { - if writeErr != nil { - return gemtextResult{}, fmt.Errorf("writing line: %w", writeErr) - } - - line, err := r.ReadString('\n') - - switch { - case errors.Is(err, io.EOF): - break loop - - case err != nil: - return gemtextResult{}, fmt.Errorf("reading next line: %w", err) - - case strings.HasPrefix(line, "```"): - if !pft { - write("
\n")
-				pft = true
-			} else {
-				write("
\n") - pft = false - } - continue - - case pft: - write(line) - continue - - case len(strings.TrimSpace(line)) == 0: - continue - } - - // list case is special, because it requires a prefix and suffix tag - if strings.HasPrefix(line, "*") { - if !list { - write("\n") - list = false - } - switch { - case strings.HasPrefix(line, "=>"): - // TODO convert gemini:// links ? - var ( - line = strings.TrimSpace(line[2:]) - urlStr = line - label = urlStr + _, err := fmt.Fprintf( + w, "

%s (proxied)

\n", urlStr, label, ) - - if i := strings.IndexAny(urlStr, " \t"); i > -1 { - urlStr, label = urlStr[:i], sanitizeText(urlStr[i:]) - } - - if g.GatewayURL != "" { - if u, err := url.Parse(urlStr); err == nil && u.Scheme == "gemini" { - urlStr = g.GatewayURL + u.Host + u.Path - } - } - - write("

%s

\n", urlStr, label) - - case strings.HasPrefix(line, "###"): - write("

%s

\n", sanitizeText(line[3:])) - - case strings.HasPrefix(line, "##"): - write("

%s

\n", sanitizeText(line[2:])) - - case strings.HasPrefix(line, "#"): - line = sanitizeText(line[1:]) - if title == "" { - title = line - } - write("

%s

\n", line) - - case strings.HasPrefix(line, ">"): - write("
%s
\n", sanitizeText(line[1:])) - - default: - line = strings.TrimSpace(line) - write("

%s

\n", line) + return err } } - return gemtextResult{ - Title: title, - Body: w.String(), - }, nil + return translator.Translate(r) } // UnmarshalCaddyfile implements caddyfile.Unmarshaler. -- cgit v1.2.3