~adnano/kiln-devel

mdtohtml: Add command line renderer options v2 PROPOSED

Edd Salkield: 1
 Add command line renderer options

 2 files changed, 76 insertions(+), 8 deletions(-)
Thanks for the feedback.  I've fixed these in the latest patch:
https://lists.sr.ht/~adnano/kiln-devel/patches/35843
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~adnano/kiln-devel/patches/35807/mbox | git am -3
Learn more about email & git

[PATCH mdtohtml v2] Add command line renderer options Export this patch

-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
---
 main.go        | 67 ++++++++++++++++++++++++++++++++++++++++++++------
 mdtohtml.1.scd | 17 +++++++++++++
 2 files changed, 76 insertions(+), 8 deletions(-)

diff --git a/main.go b/main.go
index e53f89f..8911014 100644
--- a/main.go
+++ b/main.go
@@ -1,6 +1,7 @@
package main

import (
	"flag"
	"fmt"
	"io"
	"os"
@@ -9,28 +10,78 @@ import (
	"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"
)

func main() {
	if err := run(); err != nil {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr,
			`Usage:
  mdtohtml [OPTIONS]

Options:
  -u, --unsafe  Render raw HTML as written
  -w, --wrap    Render newlines as <br>
  -x, --xhtml   Render as XHTML
  -h, --help    Show this help message
`)
	}

	var unsafe bool
	flag.BoolVar(&unsafe, "u", false, "Render raw HTML as written")
	var unsafe_long bool
	flag.BoolVar(&unsafe_long, "unsafe", false, "Render raw HTML as written")
	var wrap bool
	flag.BoolVar(&wrap, "w", false, "Render newlines as <br>")
	var wrap_long bool
	flag.BoolVar(&wrap, "wrap", false, "Render newlines as <br>")
	var xhtml bool
	flag.BoolVar(&xhtml, "x", false, "Render as XHTML")
	var xhtml_long bool
	flag.BoolVar(&xhtml_long, "xhtml", false, "Render as XHTML")
	flag.Parse()

	unsafe = unsafe || unsafe_long
	wrap = wrap || wrap_long
	xhtml = xhtml || xhtml_long

	if err := run(unsafe, wrap, xhtml); err != nil {
		fmt.Fprint(os.Stderr, err)
		os.Exit(1)
	}
}

func run() error {
func run(unsafe bool, wrap bool, xhtml bool) error {
	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),

	rendererOptions := []renderer.Option{}
	if unsafe {
		rendererOptions = append(rendererOptions, html.WithUnsafe())
	}
	if wrap {
		rendererOptions = append(rendererOptions, html.WithHardWraps())
	}
	if xhtml {
		rendererOptions = append(rendererOptions, html.WithXHTML())
	}

	markdown := goldmark.New(
		goldmark.WithExtensions(
			extension.Footnote,
			highlighting.NewHighlighting(
				highlighting.WithFormatOptions(
					chromahtml.WithClasses(true),
				),
			),
		),
	))
		goldmark.WithRendererOptions(
			rendererOptions...,
		),
	)
	if err := markdown.Convert(bytes, os.Stdout); err != nil {
		return err
	}
diff --git a/mdtohtml.1.scd b/mdtohtml.1.scd
index 058bde8..ccd282b 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.38.0