markdown/cmd/printast/main.go

41 lines
893 B
Go
Raw Normal View History

package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/status-im/markdown"
"github.com/status-im/markdown/ast"
"github.com/status-im/markdown/parser"
)
// This prints AST of parsed markdown document.
// Usage: printast <markdown-file>
func usageAndExit() {
fmt.Printf("Usage: printast <markdown-file>\n")
os.Exit(1)
}
func main() {
nFiles := len(os.Args) - 1
if nFiles < 1 {
usageAndExit()
}
for i := 0; i < nFiles; i++ {
fileName := os.Args[i+1]
d, err := ioutil.ReadFile(fileName)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't open '%s', error: '%s'\n", fileName, err)
continue
}
2019-02-02 11:26:55 +00:00
exts := parser.CommonExtensions // parser.OrderedListStart | parser.NoEmptyLineBeforeBlock
p := parser.NewWithExtensions(exts)
doc := markdown.Parse(d, p)
fmt.Printf("Ast of file '%s':\n", fileName)
2019-02-02 03:14:26 +00:00
ast.PrintWithPrefix(os.Stdout, doc, " ")
fmt.Print("\n")
}
}