-
Notifications
You must be signed in to change notification settings - Fork 45
/
mmark.go
201 lines (182 loc) · 5.65 KB
/
mmark.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/ast"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"github.com/mmarkdown/mmark/v2/lang"
"github.com/mmarkdown/mmark/v2/mast"
"github.com/mmarkdown/mmark/v2/mparser"
"github.com/mmarkdown/mmark/v2/render/man"
"github.com/mmarkdown/mmark/v2/render/mhtml"
"github.com/mmarkdown/mmark/v2/render/xml"
)
var (
flagCSS = flag.String("css", "", "link to a CSS stylesheet (only used with -html)")
flagHead = flag.String("head", "", "link to HTML to be included in head (only used with -html)")
flagAst = flag.Bool("ast", false, "print abstract syntax tree and exit")
flagBib = flag.Bool("bibliography", true, "generate a bibliography section after the back matter")
flagFragment = flag.Bool("fragment", false, "don't create a full document")
flagHTML = flag.Bool("html", false, "create HTML output")
flagIndex = flag.Bool("index", true, "generate an index at the end of the document")
flagMan = flag.Bool("man", false, "generate manual pages (nroff)")
flagUnsafe = flag.Bool("unsafe", false, "allow unsafe includes")
flagIntraEmph = flag.Bool("intra-emphasis", false, "interpret camel_case_value as emphasizing \"case\" (legacy behavior)")
flagVersion = flag.Bool("version", false, "show mmark version")
flagUnicode = flag.Bool("unicode", true, "from xml2rfc 3.16 onwards unicode is allowed in <t>")
)
func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "SYNOPSIS: %s [OPTIONS] %s\n", os.Args[0], "[FILE...]")
fmt.Println("\nOPTIONS:")
flag.PrintDefaults()
}
flag.Parse()
args := flag.Args()
if len(args) == 0 {
args = []string{"os.Stdin"}
}
if *flagVersion {
fmt.Println(Version)
os.Exit(0)
}
for _, fileName := range args {
var (
d []byte
err error
init mparser.Initial
)
if fileName == "os.Stdin" {
init = mparser.NewInitial("")
d, err = ioutil.ReadAll(os.Stdin)
if err != nil {
log.Printf("Couldn't read %q: %q", fileName, err)
continue
}
} else {
init = mparser.NewInitial(fileName)
d, err = ioutil.ReadFile(fileName)
if err != nil {
log.Printf("Couldn't open %q: %q", fileName, err)
continue
}
}
d = markdown.NormalizeNewlines(d)
if *flagUnsafe {
init.Flags |= mparser.UnsafeInclude
}
if !*flagIntraEmph {
mparser.Extensions |= parser.NoIntraEmphasis
}
p := parser.NewWithExtensions(mparser.Extensions)
parserFlags := parser.FlagsNone
documentTitle := "" // hack to get document title from toml title block and then set it here.
documentLanguage := "en" // get document language from title block if it is set.
if !*flagHTML && !*flagMan {
parserFlags |= parser.SkipFootnoteList // both xml formats don't deal with footnotes well.
}
p.Opts = parser.Options{
ParserHook: func(data []byte) (ast.Node, []byte, int) {
node, data, consumed := mparser.Hook(data)
if t, ok := node.(*mast.Title); ok {
documentTitle = t.TitleData.Title
documentLanguage = t.TitleData.Language
}
return node, data, consumed
},
ReadIncludeFn: init.ReadInclude,
Flags: parserFlags,
}
doc := markdown.Parse(d, p)
if *flagMan {
title := false
// If there isn't a title block the resulting manual page does not start
// with .TH, this messes up the entire rendering. Walk to AST to check for
// a title block, and if none is found inject an empty one.
ast.WalkFunc(doc, func(node ast.Node, entering bool) ast.WalkStatus {
if _, ok := node.(*mast.Title); ok {
title = true
return ast.Terminate
}
return ast.GoToNext
})
if !title {
t := &mast.Title{TitleData: &mast.TitleData{Title: "User Commands 1"}}
c := doc.GetChildren()
newc := append([]ast.Node{t}, c...)
doc.SetChildren(newc) // t must be the first element.
} else {
ast.AppendChild(doc, &mast.Authors{})
}
}
if *flagBib {
mparser.AddBibliography(doc)
}
if *flagIndex {
mparser.AddIndex(doc)
}
if *flagAst {
ast.Print(os.Stdout, doc)
fmt.Print("\n")
return
}
var renderer markdown.Renderer
switch {
case *flagHTML:
mhtmlOpts := mhtml.RendererOptions{
Language: lang.New(documentLanguage),
}
opts := html.RendererOptions{
Comments: [][]byte{[]byte("//"), []byte("#")}, // TODO(miek): make this an option.
RenderNodeHook: mhtmlOpts.RenderHook,
Flags: html.CommonFlags | html.FootnoteNoHRTag | html.FootnoteReturnLinks,
Generator: ` <meta name="GENERATOR" content="github.com/mmarkdown/mmark Mmark Markdown Processor - mmark.miek.nl`,
}
if !*flagFragment {
opts.Flags |= html.CompletePage
}
opts.CSS = *flagCSS
if *flagHead != "" {
head, err := ioutil.ReadFile(*flagHead)
if err != nil {
log.Printf("Couldn't open %q, error: %q", *flagHead, err)
continue
}
opts.Head = head
}
if documentTitle != "" {
opts.Title = documentTitle
}
renderer = html.NewRenderer(opts)
case *flagMan:
opts := man.RendererOptions{
Comments: [][]byte{[]byte("//"), []byte("#")},
Language: lang.New(documentLanguage),
}
if *flagFragment {
opts.Flags |= man.ManFragment
}
renderer = man.NewRenderer(opts)
default:
opts := xml.RendererOptions{
Flags: xml.CommonFlags,
Comments: [][]byte{[]byte("//"), []byte("#")},
Language: lang.New(documentLanguage),
}
if *flagFragment {
opts.Flags |= xml.XMLFragment
}
if *flagUnicode {
opts.Flags |= xml.AllowUnicode
}
renderer = xml.NewRenderer(opts)
}
x := markdown.Render(doc, renderer)
fmt.Println(string(x))
}
}