From 3d697e774668e65bfef9bdd18224feecf086da97 Mon Sep 17 00:00:00 2001 From: xengineering Date: Mon, 10 Apr 2023 16:40:22 +0200 Subject: Implement markup to HTML conversion The new custom and text/gemini inspired markup has to be converted to HTML to display the recipe. --- markup.go | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 6 deletions(-) (limited to 'markup.go') diff --git a/markup.go b/markup.go index c0a6d45..7170bd8 100644 --- a/markup.go +++ b/markup.go @@ -2,17 +2,84 @@ package main import ( "bufio" + "fmt" "strings" ) -func titleFromMarkup(markup string) string { - scanner := bufio.NewScanner(strings.NewReader(markup)) +type Markup []byte +type LineType int8 + +const ( + TextLine LineType = iota + LinkLine + HeadingLine + UnorderedListItem +) + +func getLineType(line string) LineType { + if strings.HasPrefix(line, "=>") { + return LinkLine + } + if strings.HasPrefix(line, "#") { + return HeadingLine + } + if strings.HasPrefix(line, "* ") { + return UnorderedListItem + } + return TextLine +} + +func (m Markup) title() string { + text := string(m) + scanner := bufio.NewScanner(strings.NewReader(text)) for scanner.Scan() { line := scanner.Text() - cut, found := strings.CutPrefix(line, "# ") - if found { - return cut + if strings.HasPrefix(line, "# ") { + return strings.TrimPrefix(line, "# ") + } + } + return "" +} + +func (m Markup) html() string { + retval := "" + var line string + var lineType LineType + var lastType LineType = -1 + + text := string(m) + scanner := bufio.NewScanner(strings.NewReader(text)) + for scanner.Scan() { + line = scanner.Text() + lineType = getLineType(line) + if lineType != UnorderedListItem && lastType == UnorderedListItem { + retval += "\n" + } + if line == "" { + continue } + switch lineType { + case LinkLine: + retval += fmt.Sprintf("\n
%s
", line) + case HeadingLine: + retval += fmt.Sprintf("\n

%s

", line) + case UnorderedListItem: + if lastType != UnorderedListItem { + retval += "\n" } - return "no title detected" + + retval = strings.TrimPrefix(retval, "\n") + + return retval } -- cgit v1.2.3-70-g09d2