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) usage := func() { fmt.Fprintf(os.Stderr, "usage: %s [file.riv] [file.html]\n", exe) } if len(args) < 1 { usage() return } inpath := args[0] for _, arg := range args { if arg == "-h" || arg == "--help" { usage() return } } 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) == 1 && line[0].Value == ". . ." { fmt.Fprintf(&out, "
") } else { fmt.Fprintf(&out, "

%s

", text2html(line)) } case rivit.List: fmt.Fprint(&out, list2html(line)) case rivit.Block: if len(line.Body) == 0 { continue } first := strings.TrimSpace(line.Body[0]) if len(first) != 0 && first[0] == '|' && first[len(first)-1] == '|' { // table out.WriteString("") for i, v := range line.Body { v = strings.TrimSpace(v) if len(v) == 0 { continue } if v[0] != '|' { fmt.Fprintf(&out, "", text2html(rivit.ParseStyledText(v))) break } parts := strings.Split(v, "|") if i == 0 { out.WriteString("") for _, p := range parts { p = strings.TrimSpace(p) if len(p) == 0 { continue } styled := rivit.ParseStyledText(p) fmt.Fprintf(&out, "", text2html(styled)) } out.WriteString("") continue } if i == 1 { out.WriteString("") } out.WriteString("") for it, p := range parts { p = strings.TrimSpace(p) if len(p) == 0 { continue } styled := rivit.ParseStyledText(p) if it == 0 { fmt.Fprintf(&out, "", text2html(styled)) } } out.WriteString("") } out.WriteString("") out.WriteString("
%s
%s
%s", text2html(styled)) } else { fmt.Fprintf(&out, "%s
") } else { 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() }