Skip to content

Commit

Permalink
add md2latex
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Dec 2, 2022
1 parent 74017c6 commit 1c85c04
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ This renderer seeks to be as extensible as Goldmark itself. Please file an issue
## Results
So far this implementation renders the CommonMark specification with the exception of embedded HTML. It does have some bugs related to undefined ASCII sequences. Any help is appreciated.

![result](https://user-images.githubusercontent.com/26156425/188299284-8dd2fca1-dc50-4574-8128-c78017b42e73.png)
![result](https://user-images.githubusercontent.com/26156425/188299284-8dd2fca1-dc50-4574-8128-c78017b42e73.png)

## md2latex program
This command converts a single markdown file to latex and writes to contents to a new .text file or to stdout.
91 changes: 91 additions & 0 deletions cmd/md2latex/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"bytes"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"

latex "github.com/soypat/goldmark-latex"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/util"
)

var (
usehtml bool
print bool
unhead bool
outputFilename string
)

func main() {
flag.BoolVar(&usehtml, "html", false, "Output html")
flag.BoolVar(&print, "p", false, "Output to stdout")
flag.BoolVar(&unhead, "unhead", false, "No heading numbering")
flag.StringVar(&outputFilename, "o", "", "Output filename. By default just adds .tex to input filename.")
flag.Parse()
args := flag.Args()
err := run(args)
if err != nil {
log.Fatal(err)
}
}

func run(args []string) error {
if len(args) == 0 {
return errors.New("missing filename argument")
}
filename := args[0]
fp, err := os.Open(filename)
if err != nil {
return err
}
input, err := io.ReadAll(fp)
fp.Close()
if err != nil {
return err
}
output, err := renderGoldmark(input)
if err != nil {
return err
}
if print {
fmt.Println(string(output))
return nil
}
// Generate output file.
ext := filepath.Ext(filename)
if ext == "" && outputFilename == "" {
outputFilename = filename + ".tex"
} else if outputFilename == "" {
outputFilename = strings.TrimSuffix(filename, ext) + ".tex"
}
outfp, err := os.Create(outputFilename)
if err != nil {
return err
}
defer outfp.Close()
_, err = io.Copy(outfp, bytes.NewBuffer(output))
return err
}

func renderGoldmark(input []byte) ([]byte, error) {
var rd renderer.Renderer
if usehtml {
rd = goldmark.DefaultRenderer()
} else {
rd = renderer.NewRenderer(renderer.WithNodeRenderers(util.Prioritized(latex.NewRenderer(latex.Config{
NoHeadingNumbering: unhead,
}), 1000)))
}
md := goldmark.New(goldmark.WithRenderer(rd))
var b bytes.Buffer
err := md.Convert(input, &b)
return b.Bytes(), err
}

0 comments on commit 1c85c04

Please sign in to comment.