-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.go
57 lines (47 loc) · 842 Bytes
/
content.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
package main
import (
"strings"
)
type Content struct {
Markdown string
Html string
}
func CompileContent(markdown string) (content Content, err error) {
content.Markdown = markdown
content.Html = compile(markdown)
err = nil
return
}
var (
htmlEscapeMap = map[byte]string{
'&': "&",
'\'': "'", // "'" is shorter than "'" and apos was not in HTML until HTML5.
'<': "<",
'>': ">",
'"': """,
}
)
func compile(s string) string {
var sb strings.Builder
newLines := 0
for _, c := range []byte(s) {
if c == '\r' {
continue
}
if c == '\n' {
newLines++
if newLines <= 2 {
sb.WriteString("<br/>")
}
continue
} else {
newLines = 0
}
if rep, ok := htmlEscapeMap[c]; ok {
sb.WriteString(rep)
} else {
sb.WriteByte(c)
}
}
return sb.String()
}