forked from russross/blackfriday-tool
/
main.go
284 lines (262 loc) · 7.17 KB
/
main.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//
// Blackfriday Markdown Processor
// Available at http://github.com/russross/blackfriday
//
// Copyright © 2011 Russ Ross <russ@russross.com>.
// Distributed under the Simplified BSD License.
// See README.md for details.
//
//
//
// Example front-end for command-line use
//
//
package main
import (
"flag"
"fmt"
"github.com/russross/blackfriday"
"io/ioutil"
"os"
"runtime/pprof"
"strings"
"path/filepath"
)
const DEFAULT_TITLE = ""
type Settings struct {
latex, smartypants bool
css, templateText string
repeat,extensions,htmlFlags int
}
func main() {
var page, toc, toconly, xhtml, latexdashes, fractions bool
var bf Settings
var cpuprofile, templateFile string
// parse command-line options
flag.BoolVar(&page, "page", false,
"Generate a standalone HTML page (implies -latex=false)")
flag.BoolVar(&toc, "toc", false,
"Generate a table of contents (implies -latex=false)")
flag.BoolVar(&toconly, "toconly", false,
"Generate a table of contents only (implies -toc)")
flag.BoolVar(&xhtml, "xhtml", true,
"Use XHTML-style tags in HTML output")
flag.BoolVar(&bf.latex, "latex", false,
"Generate LaTeX output instead of HTML")
flag.BoolVar(&bf.smartypants, "smartypants", true,
"Apply smartypants-style substitutions")
flag.BoolVar(&latexdashes, "latexdashes", true,
"Use LaTeX-style dash rules for smartypants")
flag.BoolVar(&fractions, "fractions", true,
"Use improved fraction rules for smartypants")
flag.StringVar(&bf.css, "css", "",
"Link to a CSS stylesheet (implies -page)")
flag.StringVar(&templateFile, "template","",
"Template file to add the content in")
flag.StringVar(&cpuprofile, "cpuprofile", "",
"Write cpu profile to a file")
flag.IntVar(&bf.repeat, "repeat", 1,
"Process the input multiple times (for benchmarking)")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Blackfriday Markdown Processor v"+blackfriday.VERSION+
"\nAvailable at http://github.com/russross/blackfriday\n\n"+
"Copyright © 2011 Russ Ross <russ@russross.com>\n"+
"Distributed under the Simplified BSD License\n"+
"See website for details\n\n"+
"Usage:\n"+
" %s [options] [inputfile [outputfile]]\n\n"+
"Options:\n",
os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
// enforce implied options
if bf.css != "" {
page = true
}
if page {
bf.latex = false
}
if toconly {
toc = true
}
if toc {
bf.latex = false
}
// turn on profiling?
if cpuprofile != "" {
f, err := os.Create(cpuprofile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
// set up options
bf.extensions = 0
bf.extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
bf.extensions |= blackfriday.EXTENSION_TABLES
bf.extensions |= blackfriday.EXTENSION_FENCED_CODE
bf.extensions |= blackfriday.EXTENSION_AUTOLINK
bf.extensions |= blackfriday.EXTENSION_STRIKETHROUGH
bf.extensions |= blackfriday.EXTENSION_SPACE_HEADERS
bf.htmlFlags = 0
var err error
if !bf.latex {
// render the data into HTML
if xhtml {
bf.htmlFlags |= blackfriday.HTML_USE_XHTML
}
if bf.smartypants {
bf.htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
}
if fractions {
bf.htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
}
if latexdashes {
bf.htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
}
if page {
bf.htmlFlags |= blackfriday.HTML_COMPLETE_PAGE
}
if templateFile != "" {
var templateString []byte
if templateString, err = ioutil.ReadFile(templateFile); err != nil {
fmt.Fprintln(os.Stderr, "Error reading from", templateFile, ":", err)
os.Exit(-1)
}
bf.templateText = string(templateString[:])
}
if toconly {
bf.htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
}
if toc {
bf.htmlFlags |= blackfriday.HTML_TOC
}
}
// read the input
var input []byte
args := flag.Args()
switch len(args) {
case 0:
if input, err = ioutil.ReadAll(os.Stdin); err != nil {
fmt.Fprintln(os.Stderr, "Error reading from Stdin:", err)
os.Exit(-1)
}
renderContent(bf,input,"","")
case 1, 2:
var fileNames []string
var fileName string
fileNames, err = filepath.Glob(args[0])
for _, fileName = range fileNames{
//fmt.Println(fileName)
if input, err = ioutil.ReadFile(fileName); err != nil {
fmt.Fprintln(os.Stderr, "Error reading from", fileName, ":", err)
os.Exit(-1)
}
if len(fileName) > 3 && strings.ToLower(fileName[len(fileName)-3:len(fileName)]) == ".md" {
fileName = fileName[:len(fileName)-3]
}
_,fileName = filepath.Split(fileName)
outputFileName := ""
if (len(args) == 2){
if bf.latex {
outputFileName = args[1] + fileName + ".tex"
} else {
outputFileName = args[1] + fileName + ".html"
}
}
renderContent(bf,input,outputFileName,fileName)
}
default:
flag.Usage()
os.Exit(-1)
}
}
func renderContent(bf Settings, input []byte, outputFileName string, fileName string){
var renderer blackfriday.Renderer
var templatewithtitle string
if bf.latex {
// render the data into LaTeX
renderer = blackfriday.LatexRenderer(0)
} else {
// render the data into HTML
title := DEFAULT_TITLE
if (bf.htmlFlags | blackfriday.HTML_COMPLETE_PAGE == blackfriday.HTML_COMPLETE_PAGE) {
title = getTitle(input)
}
if bf.templateText != "" {
title = getTitle(input)
templatewithtitle = strings.Replace(bf.templateText,"{{title}}",title,-1)
templatewithtitle = strings.Replace(templatewithtitle,"{{filename}}",fileName,-1)
}
renderer = blackfriday.HtmlRenderer(bf.htmlFlags, title, bf.css)
}
// parse and render
var output []byte
for i := 0; i < bf.repeat; i++ {
output = blackfriday.Markdown(input, renderer, bf.extensions)
}
if templatewithtitle != "" {
output = []byte(strings.Replace(templatewithtitle,"{{content}}",string(output[:]),-1))
}
// output the result
var out *os.File
var err error
if outputFileName != "" {
if out, err = os.Create(outputFileName); err != nil {
fmt.Fprintf(os.Stderr, "Error creating %s: %v", outputFileName, err)
os.Exit(-1)
}
defer out.Close()
} else {
out = os.Stdout
}
if _, err = out.Write(output); err != nil {
fmt.Fprintln(os.Stderr, "Error writing output:", err)
os.Exit(-1)
}
}
// try to guess the title from the input buffer
// just check if it starts with an <h1> element and use that
func getTitle(input []byte) string {
i := 0
// skip blank lines
for i < len(input) && (input[i] == '\n' || input[i] == '\r') {
i++
}
if i >= len(input) {
return DEFAULT_TITLE
}
if input[i] == '\r' && i+1 < len(input) && input[i+1] == '\n' {
i++
}
// find the first line
start := i
for i < len(input) && input[i] != '\n' && input[i] != '\r' {
i++
}
line1 := input[start:i]
if input[i] == '\r' && i+1 < len(input) && input[i+1] == '\n' {
i++
}
i++
// check for a prefix header
if len(line1) >= 3 && line1[0] == '#' && (line1[1] == ' ' || line1[1] == '\t') {
return strings.TrimSpace(string(line1[2:]))
}
// check for an underlined header
if i >= len(input) || input[i] != '=' {
return DEFAULT_TITLE
}
for i < len(input) && input[i] == '=' {
i++
}
for i < len(input) && (input[i] == ' ' || input[i] == '\t') {
i++
}
if i >= len(input) || (input[i] != '\n' && input[i] != '\r') {
return DEFAULT_TITLE
}
return strings.TrimSpace(string(line1))
}