Skip to content

Commit

Permalink
more functionality to md2latex
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Dec 2, 2022
1 parent 167e93e commit ff5b010
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
*.aux
testresult
*_bin

_data.tex
# IDE
.vscode
18 changes: 17 additions & 1 deletion cmd/md2latex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

latex "github.com/soypat/goldmark-latex"
"github.com/yuin/goldmark"
Expand All @@ -19,6 +20,7 @@ import (

var (
usehtml bool
verbose bool
print bool
unhead bool
unsafe bool
Expand All @@ -28,10 +30,11 @@ var (
)

func main() {
flag.BoolVar(&verbose, "v", false, "Verbose output.")
flag.BoolVar(&usehtml, "html", false, "Output html")
flag.BoolVar(&print, "p", false, "Output to stdout")
flag.BoolVar(&unsafe, "unsafe", false, "Render unsafe segments of document such as links or verbatim.")
flag.BoolVar(&unhead, "unhead", false, "No heading numbering")
flag.BoolVar(&unhead, "unhead", false, "No section numbering")
flag.StringVar(&outputFilename, "o", "", "Output filename. By default just adds .tex to input filename.")
flag.StringVar(&preambleFilename, "preamble", "", "Preamble filename. If not set uses a default preamble.")
flag.IntVar(&headingOffset, "headingoffset", 0, "Section heading offset. Can be negative. Results are clipped between 1 and 6.")
Expand All @@ -47,6 +50,7 @@ func run(args []string) error {
if len(args) == 0 {
return errors.New("missing filename argument")
}
verb("beginning verbose run")
filename := args[0]
input, err := readFile(filename)
if err != nil {
Expand Down Expand Up @@ -86,10 +90,12 @@ func renderGoldmark(input []byte) ([]byte, error) {
if err != nil {
return nil, err
}
verb("replacing default preamble with", preambleFilename, "of length", len(b))
preamble = b
}
var rd renderer.Renderer
if usehtml {
verb("using html renderer")
rd = goldmark.DefaultRenderer()
} else {
rd = renderer.NewRenderer(renderer.WithNodeRenderers(util.Prioritized(latex.NewRenderer(latex.Config{
Expand All @@ -101,12 +107,16 @@ func renderGoldmark(input []byte) ([]byte, error) {
}
md := goldmark.New(goldmark.WithRenderer(rd))
var b bytes.Buffer
verb("start rendering using goldmark")
start := time.Now()
err := md.Convert(input, &b)
verb("finished rendering in", time.Since(start))
return b.Bytes(), err
}

// Opens, reads and closes file and returns contents.
func readFile(filename string) ([]byte, error) {
verb("opening ", filename)
fp, err := os.Open(filename)
if err != nil {
return nil, err
Expand All @@ -117,3 +127,9 @@ func readFile(filename string) ([]byte, error) {
}
return input, fp.Close()
}

func verb(a ...any) {
if verbose {
log.Println(a...)
}
}

0 comments on commit ff5b010

Please sign in to comment.