From a3c6e4aeb5d0e881da753f54566359dc51cbcaba Mon Sep 17 00:00:00 2001 From: Judah Caruso Date: Sun, 25 Jan 2026 22:39:40 -0700 Subject: [PATCH] init --- go.mod | 5 ++ go.sum | 2 + main.go | 178 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d12e42b --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.brut.systems/judah/rivit2html + +go 1.25.6 + +require github.com/judah-caruso/rivit v0.0.0-20230831014329-e9675f95a220 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..afabc07 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/judah-caruso/rivit v0.0.0-20230831014329-e9675f95a220 h1:ONbXf1EhgmkB1mI+4jLe3sY5Mq7QwdwdRJWjE2bhMnI= +github.com/judah-caruso/rivit v0.0.0-20230831014329-e9675f95a220/go.mod h1:rdvhofSTSy5jwG0DYCuoCU4a72smjTJnQFQTOjO/0pQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..91955db --- /dev/null +++ b/main.go @@ -0,0 +1,178 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/judah-caruso/rivit" +) + +func main() { + exe, args := os.Args[0], os.Args[1:] + exe = filepath.Base(exe) + + if len(args) < 1 { + fmt.Fprintf(os.Stderr, "usage: %s [file.riv] [file.html]\n", exe) + return + } + + inpath := args[0] + file, err := os.ReadFile(inpath) + if err != nil { + fmt.Fprintf(os.Stderr, "error reading file: %v\n", err) + return + } + + outpath := inpath + if len(args) > 1 { + outpath = args[1] + } + + if filepath.Ext(outpath) == ".riv" { + outpath = outpath[:len(outpath)-len(filepath.Ext(outpath))] + ".html" + } + + riv := rivit.Parse(string(file)) + + var ( + out strings.Builder + title string + ) + for _, line := range riv { + if kind := line.Kind(); kind == rivit.LineNavLink { + continue + } + + switch line := line.(type) { + default: + continue + + case rivit.Header: + text := strings.Title(strings.ToLower(string(line))) + if out.Len() == 0 { + title = text + fmt.Fprintf(&out, "

%s

", text) + } else { + fmt.Fprintf(&out, "

%s

", text) + } + + case rivit.Paragraph: + if len(line) == 0 && line[0].Value == ". . ." { + fmt.Fprintf(&out, "
") + } else { + fmt.Fprintf(&out, "

%s

", text2html(line)) + } + + case rivit.List: + fmt.Fprint(&out, list2html(line)) + + case rivit.Block: + out.WriteString("
")
+
+			for i, v := range line.Body {
+				l := v[line.Indent:]
+				l = strings.ReplaceAll(l, "<", "<")
+				l = strings.ReplaceAll(l, ">", ">")
+
+				out.WriteString(l)
+
+				if i < len(line.Body) {
+					out.WriteString("\n")
+				}
+			}
+
+			out.WriteString("
") + } + } + + html := fmt.Sprintf(` + + + %s + + + + %s + + + `, title, out.String()) + + if err := os.WriteFile(outpath, []byte(html), 0644); err != nil { + fmt.Fprintf(os.Stderr, "error writing file: %v\n", err) + return + } + + fmt.Fprintf(os.Stdout, "wrote %s\n", outpath) +} + +func text2html(text []rivit.StyledText) string { + var out strings.Builder + + for _, t := range text { + switch t.Style { + default: + continue + + case rivit.StyleNone: + out.WriteString(t.Value) + case rivit.StyleItalic: + fmt.Fprintf(&out, "%s", t.Value) + case rivit.StyleBold: + fmt.Fprintf(&out, "%s", t.Value) + case rivit.StyleMono: + fmt.Fprintf(&out, "%s", t.Value) + case rivit.StyleExternalLink: + display := t.Value + if len(display) == 0 { + display = t.Link + } + + fmt.Fprintf(&out, "%s", t.Link, display) + } + } + + return out.String() +} + +func list2html(list rivit.List) string { + var out strings.Builder + + fmt.Fprintf(&out, "") + + return out.String() +}