From 22aa7cf966c38e5f3986ff8f14c54c1a7d1e0f27 Mon Sep 17 00:00:00 2001 From: bratekarate Date: Thu, 3 Oct 2024 02:25:06 +0200 Subject: [PATCH] Use cmark-gfm by default, adapt CLI args, use escapes via find and replace if discount must be used. --- main.go | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index a18bf9b..86e4222 100644 --- a/main.go +++ b/main.go @@ -74,21 +74,21 @@ func highlight(w io.Writer, source, lexer, style string) error { return err } - /* HACK: If background color is white, set to a reasonable, light grey - * default. */ - if s.Get(chroma.Background).Background.String() == "#ffffff" { - senr := s.Get(chroma.Background) - senr.Background = chroma.ParseColour("#f6f8fa"); - s, err = s.Builder().AddEntry(chroma.Background, senr).Build(); - if err != nil { - return err - } - } + /* HACK: If background color is white, set to a reasonable, light grey + * default. */ + if s.Get(chroma.Background).Background.String() == "#ffffff" { + senr := s.Get(chroma.Background) + senr.Background = chroma.ParseColour("#f6f8fa"); + s, err = s.Builder().AddEntry(chroma.Background, senr).Build(); + if err != nil { + return err + } + } return f.Format(w, s, it) } -const Version = "0.1.2" +const Version = "0.1.3" func main() { css := flag.String("css", "", "Path to a CSS import to include at the beginning of the output") @@ -119,6 +119,7 @@ func main() { bs := bufio.NewScanner(r) lang := "" needCSS := false + cmark, lookErr := exec.LookPath("cmark-gfm") for bs.Scan() { text := bs.Text() trimmed := strings.TrimSpace(text) @@ -141,6 +142,12 @@ func main() { currentCodeBlock.WriteString(text) currentCodeBlock.WriteByte('\n') } else { + /* If cmark is not installed then discount will be used, and we have to + * do some encoding ourselves. */ + if lookErr != nil { + text = strings.Replace(text, "\\<", "<", -1) + text = strings.Replace(text, "\\>", ">", -1) + } out.WriteString(text) out.WriteByte('\n') } @@ -157,12 +164,13 @@ func main() { checkError(writeErr, "writing data to temporary file") // shell out to markdown because of // https://github.com/russross/blackfriday/issues/403 - cmark, lookErr := exec.LookPath("cmark") - args := []string{cmark, "--unsafe", f.Name()} + var args []string if lookErr != nil { cmark, lookErr = exec.LookPath("markdown") checkError(lookErr, "finding markdown binary") - args = []string{cmark, f.Name()} + args = []string{cmark, "-f", "smarty,pants", f.Name()} + } else { + args = []string{cmark, "--smart", "--unsafe", "--extension", "table", f.Name()} } execErr := localExec(cmark, args, []string{}) checkError(execErr, "executing markdown binary")