Edd Salkield: 1 Add command line renderer options 5 files changed, 67 insertions(+), 9 deletions(-)
Thanks, I've made the changes in the latest patches.
Copy & paste the following snippet into your terminal to import this patchset into git:
curl -s https://lists.sr.ht/~adnano/kiln-devel/patches/35784/mbox | git am -3Learn more about email & git
-u to render potentially unsafe HTML as written -w to hard wrap, rendering newlines as <br> -x to render as XHTML -h to show help --- Makefile | 4 +++- go.mod | 1 + go.sum | 4 ++++ main.go | 50 ++++++++++++++++++++++++++++++++++++++++++-------- mdtohtml.1.scd | 17 +++++++++++++++++ 5 files changed, 67 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 61524e8..5578205 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,8 @@ all: mdtohtml mdtohtml.1 mdtohtml: main.go go.mod go.sum $(GO) build $(GOFLAGS) -o $@ +docs: mdtohtml.1 + mdtohtml.1: mdtohtml.1.scd scdoc < mdtohtml.1.scd > $@ @@ -30,4 +32,4 @@ uninstall: $(RM) $(DESTDIR)$(BINDIR)/mdtohtml $(RM) $(DESTDIR)$(MANDIR)/man1/mdtohtml.1 -.PHONY: all clean install uninstall +.PHONY: all docs clean install uninstall diff --git a/go.mod b/go.mod index 082b10e..652a860 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.16 require ( github.com/alecthomas/chroma v0.10.0 // indirect + github.com/jessevdk/go-flags v1.5.0 // indirect
I'm not too fond of pulling in a dependency for this: I would rather use the standard library's flag package. Also, that specific library seems to be abandoned: https://github.com/jessevdk/go-flags/issues/380 You can look at the gmnitohtml tool for an idea of what I mean.
github.com/yuin/goldmark v1.4.5 // indirect github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594 // indirect ) diff --git a/go.sum b/go.sum index d8273aa..7f470d1 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E= github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= +github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc= +github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -11,5 +13,7 @@ github.com/yuin/goldmark v1.4.5 h1:4OEQwtW2uLXjEdgnGM3Vg652Pq37X7NOIRzFWb3BzIc= github.com/yuin/goldmark v1.4.5/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2LIilg= github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594 h1:yHfZyN55+5dp1wG7wDKv8HQ044moxkyGq12KFFMFDxg= github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594/go.mod h1:U9ihbh+1ZN7fR5Se3daSPoz1CGF9IYtSvWwVQtnzGHU= +golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 h1:EZ2mChiOa8udjfp6rRmswTbtZN/QzUQp4ptM4rnjHvc= +golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index e53f89f..e26be65 100644 --- a/main.go +++ b/main.go @@ -6,31 +6,65 @@ import ( "os" chromahtml "github.com/alecthomas/chroma/formatters/html" + "github.com/jessevdk/go-flags" "github.com/yuin/goldmark" highlighting "github.com/yuin/goldmark-highlighting" "github.com/yuin/goldmark/extension" + "github.com/yuin/goldmark/renderer" + "github.com/yuin/goldmark/renderer/html" ) +type Options struct { + Unsafe bool `short:"u" long:"unsafe" description:"Render raw HTML as written"` + Wrap bool `short:"w" long:"wrap" description:"Render newlines as <br>"` + XHTML bool `short:"x" long:"xhtml" description:"Render as XHTML"` +} + func main() { - if err := run(); err != nil { + var opts Options + parser := flags.NewParser(&opts, flags.Default & ^flags.PrintErrors) + _, err := parser.ParseArgs(os.Args) + if err != nil { + fmt.Fprint(os.Stderr, err) + os.Exit(1) + } + if err := run(opts); err != nil { fmt.Fprint(os.Stderr, err) os.Exit(1) } } -func run() error { +func run(opts Options) error { + fmt.Print(opts.Unsafe)
Leftover debugging code?
bytes, err := io.ReadAll(os.Stdin) if err != nil { return err } - markdown := goldmark.New(goldmark.WithExtensions( - extension.Footnote, - highlighting.NewHighlighting( - highlighting.WithFormatOptions( - chromahtml.WithClasses(true), + + renderer_options := []renderer.Option{}
rendererOptions (or options)
+ if opts.Unsafe { + renderer_options = append(renderer_options, html.WithUnsafe()) + } + if opts.Wrap { + renderer_options = append(renderer_options, html.WithHardWraps()) + } + if opts.XHTML { + renderer_options = append(renderer_options, html.WithXHTML()) + } + + markdown := goldmark.New( + goldmark.WithExtensions( + extension.Footnote, + highlighting.NewHighlighting( + highlighting.WithFormatOptions( + chromahtml.WithClasses(true), + ), ), ), - )) + goldmark.WithRendererOptions( + renderer_options..., + ), + ) if err := markdown.Convert(bytes, os.Stdout); err != nil { return err } diff --git a/mdtohtml.1.scd b/mdtohtml.1.scd index 058bde8..8e1983b 100644 --- a/mdtohtml.1.scd +++ b/mdtohtml.1.scd @@ -4,7 +4,24 @@ mdtohtml(1) mdtohtml - convert Markdown to HTML +# SYNOPSIS + *mdtohtml* [*-uwxh*] [--unsafe] [--wrap] [--xhtml] [--help] + # DESCRIPTION The mdtohtml utility reads CommonMark-compliant Markdown from the standard input and writes HTML to the standard output. + +The options are as follows: + +*-u*, *--unsafe* + Render raw HTML as written. + +*-w*, *--wrap* + Hard wrap, rendering newlines as <br>. + +*-x*, *--xhtml* + Render as XHTML. + +*-h*, *--help* + Show the help message. -- 2.37.3
Also, please update the patch prefix to [PATCH mdtohtml] to make it clear which repository this belongs to.