-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.go
151 lines (132 loc) · 3.56 KB
/
html.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
package runic
import (
"fmt"
"regexp"
"strings"
)
type htmlCtxType int
const (
htmlCtxNone htmlCtxType = iota
htmlCtxParagraph
)
func (p *parser) Html(input string) string {
tree := p.Parse(input)
s := ""
return p.toHtml(tree, &s, htmlCtxNone)
}
func (p *parser) toHtml(currentNode *Node, htmlString *string, htmlCtx htmlCtxType) string {
for _, child := range currentNode.Children {
switch child.Typ {
case nodeError:
*htmlString += "<span class='error'>"
case nodeHeadingOne:
*htmlString += "<h1>"
case nodeHeadingTwo:
*htmlString += "<h2>"
case nodeHeadingThree:
*htmlString += "<h3>"
case nodeHeadingFour:
*htmlString += "<h4>"
case nodeHeadingFive:
*htmlString += "<h5>"
case nodeHeadingSix:
*htmlString += "<h6>"
case nodeParagraph:
*htmlString += "<p>"
htmlCtx = htmlCtxParagraph
case nodeBoldTag:
*htmlString += "<b>"
case nodeItalicTag:
*htmlString += "<em>"
case nodeList:
*htmlString += "<ul>"
case nodeListItem:
*htmlString += "<li>"
}
if child.Typ == nodeText {
*htmlString += child.Val + " "
}
if len(child.Children) > 0 {
p.toHtml(child, htmlString, htmlCtx)
*htmlString = strings.TrimSpace(*htmlString)
}
switch child.Typ {
case nodeError:
*htmlString += "</span>"
case nodeHeadingOne:
*htmlString += "</h1>"
case nodeHeadingTwo:
*htmlString += "</h2>"
case nodeHeadingThree:
*htmlString += "</h3>"
case nodeHeadingFour:
*htmlString += "</h4>"
case nodeHeadingFive:
*htmlString += "</h5>"
case nodeHeadingSix:
*htmlString += "</h6>"
case nodeParagraph:
*htmlString += "</p>"
htmlCtx = htmlCtxNone
case nodeBoldTag:
*htmlString += "</b> "
case nodeItalicTag:
*htmlString += "</em> "
case nodeList:
*htmlString += "</ul>"
case nodeListItem:
*htmlString += "</li>"
}
}
return *htmlString
}
func (p *parser) htmlSanitiseSlice(start, end int) (s string) {
re := regexp.MustCompile("^\\s|\\s\\s+|\\s$")
s = strings.ReplaceAll(p.input[start:end], "\n", "<br>")
s = re.ReplaceAllStringFunc(s, func(s string) string {
return strings.Repeat(" ", len(s))
})
return
}
func (p *parser) HighlightText(input string) (highlightedText string) {
p.input = input
lexer := lex(input)
var prevToken token
for lexer.nextToken() {
if prevToken.Typ == typeNone {
prevToken = lexer.token
prevToken.Pos = 0
continue
}
start := prevToken.Pos
end := lexer.token.Pos
switch prevToken.Typ {
case typeText:
highlightedText += fmt.Sprintf(`<span class="runic__text">%s</span>`, p.htmlSanitiseSlice(start, end))
case typeHeading:
highlightedText += fmt.Sprintf(`<span class="runic__heading">%s</span>`, p.htmlSanitiseSlice(start, end))
case typeTag:
highlightedText += fmt.Sprintf(`<span class="runic__tag">%s</span>`, p.htmlSanitiseSlice(start, end))
case typeOpeningSquare:
highlightedText += fmt.Sprintf(`<span class="runic__osq">%s</span>`, p.htmlSanitiseSlice(start, end))
case typeClosingSquare:
highlightedText += fmt.Sprintf(`<span class="runic__csq">%s</span>`, p.htmlSanitiseSlice(start, end))
case typeBulletpoint:
highlightedText += fmt.Sprintf(`<span class="runic__bulletpoint">%s</span>`, p.htmlSanitiseSlice(start, end))
default:
highlightedText += p.htmlSanitiseSlice(start, end)
}
prevToken = lexer.token
}
return
}
type editorData struct {
Html string `json:"html"`
HighlightText string `json:"highlightText"`
}
func (p *parser) EditorData(input string) (*editorData, error) {
return &editorData{
Html: p.Html(input),
HighlightText: p.HighlightText(input),
}, nil
}