summaryrefslogtreecommitdiff
path: root/markup.go
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2023-04-10 16:40:22 +0200
committerxengineering <me@xengineering.eu>2023-04-12 18:40:53 +0200
commit3d697e774668e65bfef9bdd18224feecf086da97 (patch)
tree2ba6a240dcfea2dba832be4ed3e80d42df12b762 /markup.go
parenta7a7bc184eb010166c8eb4a3255aad73b6d06edc (diff)
downloadceres-3d697e774668e65bfef9bdd18224feecf086da97.tar
ceres-3d697e774668e65bfef9bdd18224feecf086da97.tar.zst
ceres-3d697e774668e65bfef9bdd18224feecf086da97.zip
Implement markup to HTML conversion
The new custom and text/gemini inspired markup has to be converted to HTML to display the recipe.
Diffstat (limited to 'markup.go')
-rw-r--r--markup.go79
1 files changed, 73 insertions, 6 deletions
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</ul>"
+ }
+ if line == "" {
+ continue
}
+ switch lineType {
+ case LinkLine:
+ retval += fmt.Sprintf("\n<pre>%s</pre>", line)
+ case HeadingLine:
+ retval += fmt.Sprintf("\n<h3>%s</h3>", line)
+ case UnorderedListItem:
+ if lastType != UnorderedListItem {
+ retval += "\n<ul>"
+ }
+ item := strings.TrimPrefix(line, "*")
+ retval += fmt.Sprintf("\n\t<li>%s</li>", item)
+ default:
+ retval += fmt.Sprintf("\n<p>%s</p>", line)
+ }
+ lastType = lineType
+ }
+
+ if lineType == UnorderedListItem {
+ retval += "\n</ul>"
}
- return "no title detected"
+
+ retval = strings.TrimPrefix(retval, "\n")
+
+ return retval
}