Skip to content

Commit

Permalink
expose markdown.NormalizeNewlines; add -to-html option to cmd/printast
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Sep 18, 2021
1 parent 34eadb9 commit 0410668
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 30 deletions.
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,8 @@ html := bluemonday.UGCPolicy().SanitizeBytes(maybeUnsafeHTML)
## Windows / Mac newlines

The library only supports Unix newlines. If you have markdown text with possibly
Windows / Mac newlines, normalize newlines before caling this library. Use function like:

```go
func normalizeNewlines(s string) string {
// replace CR LF (windows) with LF (unix)
s = strings.Replace(s, string([]byte{13, 10}), "\n", -1)
// replace CF (mac) with LF (unix)
s = strings.Replace(s, string([]byte{13}), "\n", -1)
return s
}
```
Windows / Mac newlines, normalize newlines before caling this librar using
`d = markdown.NormalizeNewlines(d)`

## mdtohtml command-line tool

Expand Down
42 changes: 34 additions & 8 deletions cmd/printast/main.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,66 @@
package main

import (
"flag"
"fmt"
"io/ioutil"
"os"

"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/ast"
mdhtml "github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
)

// This prints AST of parsed markdown document.
// Usage: printast <markdown-file>

func usageAndExit() {
fmt.Printf("Usage: printast <markdown-file>\n")
fmt.Printf("Usage: printast [-to-html] <markdown-file>\n")
os.Exit(1)
}

func main() {
nFiles := len(os.Args) - 1
if nFiles < 1 {
var (
flgToHTML bool
)
{
flag.BoolVar(&flgToHTML, "to-html", false, "convert to HTML")
flag.Parse()
}

files := flag.Args()
if len(files) < 1 {
usageAndExit()
}
for i := 0; i < nFiles; i++ {
fileName := os.Args[i+1]
for _, fileName := range files {
d, err := ioutil.ReadFile(fileName)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't open '%s', error: '%s'\n", fileName, err)
continue
}
d = markdown.NormalizeNewlines(d)

exts := parser.CommonExtensions // parser.OrderedListStart | parser.NoEmptyLineBeforeBlock
p := parser.NewWithExtensions(exts)
doc := markdown.Parse(d, p)
fmt.Printf("Ast of file '%s':\n", fileName)
ast.PrintWithPrefix(os.Stdout, doc, " ")
fmt.Print("\n")

if flgToHTML {
htmlFlags := mdhtml.Smartypants |
mdhtml.SmartypantsFractions |
mdhtml.SmartypantsDashes |
mdhtml.SmartypantsLatexDashes
htmlOpts := mdhtml.RendererOptions{
Flags: htmlFlags,
}
renderer := mdhtml.NewRenderer(htmlOpts)
html := markdown.Render(doc, renderer)
fmt.Printf("HTML of file '%s':\n%s\n", fileName, string(html))

} else {
fmt.Printf("Ast of file '%s':\n", fileName)
ast.PrintWithPrefix(os.Stdout, doc, " ")
fmt.Print("\n")
}
}
}
13 changes: 2 additions & 11 deletions helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package markdown

import (
"bytes"
"io/ioutil"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -121,7 +120,7 @@ func doTestsReference(t *testing.T, files []string, flag parser.Extensions) {
t.Errorf("Couldn't open '%s', error: %v\n", filename, err)
return
}
inputBytes = normalizeNewlines(inputBytes)
inputBytes = NormalizeNewlines(inputBytes)
input := string(inputBytes)

filename = filepath.Join("testdata", basename+".html")
Expand All @@ -130,7 +129,7 @@ func doTestsReference(t *testing.T, files []string, flag parser.Extensions) {
t.Errorf("Couldn't open '%s', error: %v\n", filename, err)
return
}
expectedBytes = normalizeNewlines(expectedBytes)
expectedBytes = NormalizeNewlines(expectedBytes)
expected := string(expectedBytes)

actual := string(runMarkdown(input, params))
Expand All @@ -141,11 +140,3 @@ func doTestsReference(t *testing.T, files []string, flag parser.Extensions) {
})
}
}

func normalizeNewlines(d []byte) []byte {
// replace CR LF (windows) with LF (unix)
d = bytes.Replace(d, []byte{13, 10}, []byte{10}, -1)
// replace CF (mac) with LF (unix)
d = bytes.Replace(d, []byte{13}, []byte{10}, -1)
return d
}
11 changes: 11 additions & 0 deletions markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,14 @@ func ToHTML(markdown []byte, p *parser.Parser, renderer Renderer) []byte {
}
return Render(doc, renderer)
}

// NormalizeNewlines converts Windows and Mac newlines to Unix newlines
// The parser only supports Unix newlines. If your mardown content
// might contain Windows or Mac newlines, use this function to convert to Unix newlines
func NormalizeNewlines(d []byte) []byte {
// replace CR LF (windows) with LF (unix)
d = bytes.Replace(d, []byte{13, 10}, []byte{10}, -1)
// replace CF (mac) with LF (unix)
d = bytes.Replace(d, []byte{13}, []byte{10}, -1)
return d
}

0 comments on commit 0410668

Please sign in to comment.