forked from mmarkdown/mmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmark.go
214 lines (191 loc) · 5.68 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
202
203
204
205
206
207
208
209
210
211
212
213
214
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"
mmarkout "github.com/mmarkdown/mmark/markdown"
"github.com/mmarkdown/mmark/mast"
"github.com/mmarkdown/mmark/mhtml"
"github.com/mmarkdown/mmark/mparser"
"github.com/mmarkdown/mmark/xml"
"github.com/mmarkdown/mmark/xml2"
)
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")
flagTwo = flag.Bool("2", false, "generate RFC 7749 XML")
flagMarkdown = flag.Bool("markdown", false, "generate markdown (experimental)")
flagWrite = flag.Bool("w", false, "write to source file when generating markdown")
flagWidth = flag.Int("width", 80, "text width when generating markdown")
flagUnsafe = flag.Bool("unsafe", false, "allow unsafe includes")
flagVersion = flag.Bool("version", false, "show mmark version")
)
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
}
}
if *flagUnsafe {
init.Flags |= mparser.UnsafeInclude
}
documentTitle := "" // hack to get document title from toml title block and then set it here.
if !*flagMarkdown {
Extensions |= parser.Includes
}
p := parser.NewWithExtensions(Extensions)
parserFlags := parser.FlagsNone
if !*flagHTML {
parserFlags |= parser.SkipFootnoteList // both xml formats don't deal with footnotes well.
}
p.Opts = parser.ParserOptions{
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
}
return node, data, consumed
},
ReadIncludeFn: init.ReadInclude,
Flags: parserFlags,
}
doc := markdown.Parse(d, p)
if *flagBib {
addBibliography(doc)
}
if *flagIndex {
addIndex(doc)
}
if *flagAst {
ast.Print(os.Stdout, doc)
fmt.Print("\n")
return
}
var renderer markdown.Renderer
switch {
case *flagHTML:
opts := html.RendererOptions{
// TODO(miek): make this an option.
Comments: [][]byte{[]byte("//"), []byte("#")},
RenderNodeHook: mhtml.RenderHook,
Flags: html.CommonFlags | html.FootnoteNoHRTag | html.FootnoteReturnLinks,
Generator: ` <meta name="GENERATOR" content="github.com/mmarkdown/mmark Mmark Markdown Processor - mmark.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 *flagTwo:
opts := xml2.RendererOptions{
Flags: xml2.CommonFlags,
Comments: [][]byte{[]byte("//"), []byte("#")},
}
if *flagFragment {
opts.Flags |= xml2.XMLFragment
}
renderer = xml2.NewRenderer(opts)
case *flagMarkdown:
opts := mmarkout.RendererOptions{TextWidth: *flagWidth}
renderer = mmarkout.NewRenderer(opts)
default:
opts := xml.RendererOptions{
Flags: xml.CommonFlags,
Comments: [][]byte{[]byte("//"), []byte("#")},
}
if *flagFragment {
opts.Flags |= xml.XMLFragment
}
renderer = xml.NewRenderer(opts)
}
x := markdown.Render(doc, renderer)
if *flagMarkdown && *flagWrite && fileName != "os.Stdin" {
ioutil.WriteFile(fileName, x, 0600)
continue
}
if *flagMarkdown {
fmt.Print(string(x))
continue
}
fmt.Println(string(x))
}
}
func addBibliography(doc ast.Node) bool {
where := mparser.NodeBackMatter(doc)
if where == nil {
return false
}
norm, inform := mparser.CitationToBibliography(doc)
if norm != nil {
ast.AppendChild(where, norm)
}
if inform != nil {
ast.AppendChild(where, inform)
}
return (norm != nil) || (inform != nil)
}
func addIndex(doc ast.Node) bool {
idx := mparser.IndexToDocumentIndex(doc)
if idx == nil {
return false
}
ast.AppendChild(doc, idx)
return true
}
// Extensions is exported to we can use it in tests.
var Extensions = parser.Tables | parser.FencedCode | parser.Autolink | parser.Strikethrough |
parser.SpaceHeadings | parser.HeadingIDs | parser.BackslashLineBreak | parser.SuperSubscript |
parser.DefinitionLists | parser.MathJax | parser.AutoHeadingIDs | parser.Footnotes |
parser.Strikethrough | parser.OrderedListStart | parser.Attributes | parser.Mmark