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() }