-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
166 lines (149 loc) · 3.8 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
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
164
165
166
package gomailify
import (
"bytes"
"fmt"
"golang.org/x/net/html"
"html/template"
"strings"
)
// CodeToHTML generates the HTML string from the inline code.
func CodeToHTML(code string) (string, error) {
// Parse the template.
tmpl, err := template.New("codeToHTML").Parse(InlineCodeTemplate)
if err != nil {
return "", fmt.Errorf("error parsing template: %w", err)
}
// Execute the template.
var buf bytes.Buffer
if err := tmpl.Execute(&buf, code); err != nil {
return "", fmt.Errorf("error executing template: %w", err)
}
// Return the generated HTML string.
return buf.String(), nil
}
// HTMLFragmentToNodes parses an HTML fragment string with a given context and returns a slice of nodes.
func HTMLFragmentToNodes(fragmentHTML string, context *html.Node) ([]*html.Node, error) {
dumbContext := &html.Node{
Type: context.Type,
Data: context.Data,
DataAtom: context.DataAtom,
}
// Parse the HTML fragment using the provided context node.
nodes, err := html.ParseFragment(strings.NewReader(fragmentHTML), dumbContext)
if err != nil {
return nil, fmt.Errorf("error parsing HTML fragment: %w", err)
}
return nodes, nil
}
// ReplaceChildren TODO: FIX ME
func ReplaceChildren(parentNode *html.Node, newChildren []*html.Node) {
// Remove all existing children
for child := parentNode.FirstChild; child != nil; {
next := child.NextSibling
parentNode.RemoveChild(child)
child = next
}
// Append new children
for _, child := range newChildren {
// Detach the node from its current parent, if necessary
if child.Parent != nil {
child.Parent.RemoveChild(child)
}
// Append the node to parentNode
parentNode.AppendChild(child)
}
}
func extractParagraphAttributes(templateStr string) (map[string]string, error) {
attributes := make(map[string]string)
doc, err := html.Parse(strings.NewReader(templateStr))
if err != nil {
return nil, fmt.Errorf("invalid HTML template: %v", err)
}
// Find our p tag
var findP func(*html.Node) *html.Node
findP = func(n *html.Node) *html.Node {
if n.Type == html.ElementNode && n.Data == "p" {
return n
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
if found := findP(c); found != nil {
return found
}
}
return nil
}
pNode := findP(doc)
if pNode == nil {
return nil, fmt.Errorf("no p tag found in template")
}
// Extract attributes
for _, attr := range pNode.Attr {
attributes[attr.Key] = attr.Val
}
return attributes, nil
}
func InlineCodeFromNode(n *html.Node) string {
var code string
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.TextNode {
return code + c.Data
}
}
return code
}
func DepthFirstTraversal(n *html.Node, depth int) {
switch n.Type {
case html.ElementNode:
switch n.Data {
case "p":
newAttr := make([]html.Attribute, 0)
attributes, err := extractParagraphAttributes(ParagraphTemplate)
if err != nil {
panic(err)
}
for k, v := range attributes {
newAttr = append(newAttr, html.Attribute{
Key: k,
Val: v,
})
}
n.Attr = newAttr
case "code":
// check if its inline or code block
var isInline bool
var parentNode *html.Node
if n.Parent != nil && n.Parent.Data == "p" {
isInline = true
parentNode = n.Parent
}
if isInline {
code := InlineCodeFromNode(n)
codeHTML, err := CodeToHTML(code)
if err != nil {
panic(err)
}
nodes, err := HTMLFragmentToNodes(codeHTML, parentNode)
if err != nil {
panic(err)
}
ReplaceChildren(parentNode, nodes)
}
}
case html.TextNode:
break
default:
break
}
// Recursively traverse child nodes
for c := n.FirstChild; c != nil; c = c.NextSibling {
DepthFirstTraversal(c, depth+1)
}
}
func Render(doc *html.Node) string {
var buf bytes.Buffer
err := html.Render(&buf, doc)
if err != nil {
panic(err)
}
return buf.String()
}