-
Notifications
You must be signed in to change notification settings - Fork 0
/
fipmd.go
119 lines (105 loc) · 2.21 KB
/
fipmd.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
package fipmd
import (
"bytes"
"io/ioutil"
)
/*var htmlTags = []string{
"p": true,
"dl": true,
"h1": true,
"h2": true,
"h3": true,
"h4": true,
"h5": true,
"h6": true,
"ol": true,
"ul": true,
"del": true,
"div": true,
"ins": true,
"pre": true,
"form": true,
"math": true,
"table": true,
"iframe": true,
"script": true,
"fieldset": true,
"noscript": true,
"blockquote": true,
// HTML5
"video": true,
"aside": true,
"canvas": true,
"figure": true,
"footer": true,
"header": true,
"hgroup": true,
"output": true,
"article": true,
"section": true,
"progress": true,
"figcaption": true,
}}*/
func ParseFile(path string) (content []byte, err error) {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return
}
content = Parse(bytes)
return
}
func ParseString(input string) string {
return string(Parse([]byte(input)))
}
func Parse(input []byte) []byte {
els := parse(input)
if len(els) == 0 {
return nil
}
buf := new(bytes.Buffer)
for _, el := range els {
//fmt.Println("el",el)
el.html(buf)
}
return buf.Bytes()
}
type Parser struct {
headerHooks []func(*Header)
paraHooks []func(*Para)
quoteHooks []func(*Quote)
ulHooks []func(*Ul)
olHooks []func(*Ol)
liHooks []func(*Li)
codeBlockHooks []func(*CodeBlock)
plainHooks []func(*Plain)
rawHtmlHooks []func(*RawHtml)
linkHooks []func(*Link)
imgHooks []func(*Img)
codeHooks []func(*Code)
emHooks []func(*Em)
strongHooks []func(*Strong)
delHooks []func(*Del)
brHooks []func(*Br)
hrHooks []func(*Hr)
}
func NewParser() *Parser {
return new(Parser)
}
func (parser *Parser) AddHeaderHook(headerHook func(*Header)) {
parser.headerHooks = append(parser.headerHooks, headerHook)
}
func (parser *Parser) AddParaHook(hook func(*Para)) {
parser.paraHooks = append(parser.paraHooks, hook)
}
func (parser *Parser) Parse(input []byte) []byte {
els := parse(input)
if len(els) == 0 {
return nil
}
buf := new(bytes.Buffer)
for _, el := range els {
el.execHooks(parser)
el.html(buf)
}
return buf.Bytes()
}