/
markdown.go
163 lines (141 loc) · 3.87 KB
/
markdown.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
package datadex
import (
"bytes"
"github.com/russross/blackfriday"
"io/ioutil"
"path"
"strings"
)
// Markdown stuff.
func ReadMarkdownWebPage(filename string) (*DocWebPage, error) {
p := &DocWebPage{}
Toc := []byte{}
Doc := []byte{}
// figure out route.
p.route = "/" + path.Base(filename)
p.route = strings.Replace(p.route, ".md", "", -1)
p.route = strings.Replace(p.route, "_", "/", -1)
// get the markdown first
m, err := RenderMarkdownFile(filename)
if err != nil {
return nil, err
}
// perform surgery on the markdown.
// this is hideous. but it will have to do.
// split on nav, removing nav.
{
no := []byte("<nav>")
nc := []byte("</nav>")
ns := bytes.Index(m, no)
ne := bytes.Index(m, nc)
Toc = m[ns+len(no) : ne]
Doc = m[ne+len(nc):]
}
// patch up the toc. (discard top ul)
{
ulo := []byte("<ul>")
ulc := []byte("</ul>")
uls := bytes.Index(Toc, ulo)
ule := bytes.LastIndex(Toc, ulc)
if uls >= 0 && ule > uls+4 {
Toc = Toc[uls+4 : ule]
}
}
// replace <ul> -> <ul class="nav">
{
ulo := []byte("<ul>")
ulos := []byte("<ul class=\"nav\">")
Toc = bytes.Replace(Toc, ulo, ulos, -1)
}
// figure out title
{
to := []byte("<!-- title: ")
tc := []byte(" -->")
ts := bytes.Index(Doc, to)
if ts >= 0 {
r := Doc[ts+len(to):]
te := bytes.Index(r, tc)
if te >= 0 {
p.Title = string(r[:te])
}
}
}
// figure out description
{
do := []byte("<!-- description: ")
dc := []byte(" -->")
ds := bytes.Index(Doc, do)
if ds >= 0 {
r := Doc[ds+len(do):]
de := bytes.Index(r, dc)
if de >= 0 {
p.Description = string(r[:de])
}
}
}
// Convert to strings
p.Toc = string(Toc)
p.Doc = string(Doc)
// pOut("Parsed Markdown Page: %s\n", filename)
// pOut("route: %s\n", p.route)
// pOut("title: %s\n", p.Title)
// pOut("description: %s\n", p.Description)
// pOut("toc:\n %s\n", p.Toc)
// pOut("doc:\n %s\n", p.Doc)
return p, nil
}
func RenderMarkdownFile(filename string) ([]byte, error) {
buf, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return RenderMarkdown(buf), nil
}
func RenderMarkdown(input []byte) []byte {
// set up the HTML renderer
flags := 0
flags |= blackfriday.HTML_USE_XHTML
flags |= blackfriday.HTML_USE_SMARTYPANTS
flags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
flags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
flags |= blackfriday.HTML_SKIP_SCRIPT
flags |= blackfriday.HTML_TOC
// flags |= blackfriday.HTML_GITHUB_BLOCKCODE
renderer := blackfriday.HtmlRenderer(flags, "", "")
// set up the parser
ext := 0
ext |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
ext |= blackfriday.EXTENSION_TABLES
ext |= blackfriday.EXTENSION_FENCED_CODE
ext |= blackfriday.EXTENSION_AUTOLINK
ext |= blackfriday.EXTENSION_STRIKETHROUGH
ext |= blackfriday.EXTENSION_SPACE_HEADERS
ext |= blackfriday.EXTENSION_HARD_LINE_BREAK
ext |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
return blackfriday.Markdown(input, renderer, ext)
}
func RenderMarkdownSafe(input []byte) []byte {
// set up the HTML renderer
flags := 0
flags |= blackfriday.HTML_USE_XHTML
flags |= blackfriday.HTML_USE_SMARTYPANTS
flags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
flags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
flags |= blackfriday.HTML_SKIP_HTML
flags |= blackfriday.HTML_SKIP_SCRIPT
flags |= blackfriday.HTML_SKIP_STYLE
// flags |= blackfriday.HTML_TOC
// flags |= blackfriday.HTML_GITHUB_BLOCKCODE
renderer := blackfriday.HtmlRenderer(flags, "", "")
// set up the parser
ext := 0
ext |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
ext |= blackfriday.EXTENSION_TABLES
ext |= blackfriday.EXTENSION_FENCED_CODE
ext |= blackfriday.EXTENSION_AUTOLINK
ext |= blackfriday.EXTENSION_STRIKETHROUGH
ext |= blackfriday.EXTENSION_SPACE_HEADERS
ext |= blackfriday.EXTENSION_HARD_LINE_BREAK
ext |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
return blackfriday.Markdown(input, renderer, ext)
}