markdown/cmd/printast/main.go

38 lines
726 B
Go
Raw Normal View History

package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/ast"
)
// 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
}
doc := markdown.Parse(d, nil)
fmt.Printf("Ast of file '%s':\n", fileName)
2019-02-02 03:14:26 +00:00
ast.PrintWithPrefix(os.Stdout, doc, " ")
fmt.Print("\n")
}
}