-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
113 lines (108 loc) · 2.78 KB
/
parser.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
package main
import (
"bytes"
"fmt"
"github.com/russross/blackfriday/v2"
"golang.org/x/net/html"
"strings"
)
const (
style_h1 = "\n\x1b[1;4;92m" // green, bold, underline, high intensity
style_h2 = "\n\n\x1b[4;92m" // green, underline, high intensity
style_h3 = "\n\n\x1b[1;92m" // green, bold, high intensity
style_h4 = "\n\n\x1b[1;93m" // yellow, bold, high intensity
style_h5 = "\n\n\x1b[0;93m" // yellow, high intensity
style_h6 = "\n\n\x1b[0;33m" // yellow
style_strong = "\x1b[1;4;37m" // grey, bold, underline
style_em = "\x1b[4;37m" // grey, underline
style_reset = "\x1b[0m"
)
func ParseHTML(htmlBytes []byte) ([]string, error) {
var output []string
var listPosition int
var isOrderedList bool
var inListItem bool
var depth int
z := html.NewTokenizer(bytes.NewReader(htmlBytes))
for {
token := z.Next()
switch token {
case html.ErrorToken:
// Add trailing newline to make space before next command prompt
output = append(output, "\n")
return output, z.Err()
case html.TextToken:
text := string(z.Text())
output = append(output, strings.Trim(text, "\n"))
case html.StartTagToken, html.EndTagToken:
tn, _ := z.TagName()
tagName := string(tn)
switch token {
case html.StartTagToken:
switch tagName {
case "h1":
output = append(output, style_h1)
case "h2":
output = append(output, style_h2)
case "h3":
output = append(output, style_h3)
case "h4":
output = append(output, style_h4)
case "h5":
output = append(output, style_h5)
case "h6":
output = append(output, style_h6)
case "strong":
output = append(output, style_strong)
case "em":
output = append(output, style_em)
case "p":
if !inListItem {
output = append(output, "\n")
}
case "li":
inListItem = true
listPosition++
output = append(output, "\n")
if depth > 1 {
output = append(output, strings.Repeat(" ", depth*2-2))
}
switch isOrderedList {
case true:
output = append(output, fmt.Sprintf("\n%d. ", listPosition))
case false:
output = append(output, "- ")
}
case "ol":
isOrderedList = true
listPosition = 0
depth++
case "ul":
depth++
}
case html.EndTagToken:
switch tagName {
case "h1", "h2", "h3", "h4", "h5", "h6", "strong", "em":
output = append(output, style_reset)
case "ol":
isOrderedList = false
listPosition = 0
depth--
case "ul":
depth--
case "li":
inListItem = false
}
}
}
}
}
func RenderMarkdown(mdBytes []byte) error {
htmlBytes := blackfriday.Run(mdBytes)
outputSlice, err := ParseHTML(htmlBytes)
if err.Error() != "EOF" {
return err
}
fmt.Println(strings.Join(outputSlice, ""))
return nil
}