From 5d79801b655aae7801fb27fc3a9422bdd5764882 Mon Sep 17 00:00:00 2001 From: bptato Date: Sun, 31 Jan 2021 19:24:27 +0100 Subject: new awful html parser that doesn't even work --- htmlelement.nim | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'htmlelement.nim') diff --git a/htmlelement.nim b/htmlelement.nim index 3ead2f15..7c0d9169 100644 --- a/htmlelement.nim +++ b/htmlelement.nim @@ -3,7 +3,6 @@ import terminal import uri import unicode -import fusion/htmlparser import fusion/htmlparser/xmltree import twtstr @@ -28,10 +27,8 @@ type fmttext*: seq[string] x*: int y*: int - fmtchar*: int - rawchar*: int - fmtend*: int - rawend*: int + ex*: int + ey*: int width*: int height*: int openblock*: bool @@ -45,8 +42,8 @@ type HtmlElement* = ref HtmlElementObj HtmlElementObj = object of HtmlNodeObj id*: string + class*: string tagType*: TagType - name*: string centered*: bool display*: DisplayType innerText*: string @@ -77,6 +74,7 @@ type HtmlSelectElement* = ref HtmlSelectElementObj HtmlSelectElementObj = object of HtmlElementObj + name*: string value*: string HtmlOptionElement* = ref HtmlOptionElementObj @@ -103,7 +101,7 @@ macro genEnumCase(s: string): untyped = branch.add(ret) casestmt.add(branch) -func tagType*(s: string): TagType = +func tagType(s: string): TagType = genEnumCase(s) func nodeAttr*(node: HtmlNode): HtmlElement = @@ -192,6 +190,9 @@ func ancestor*(htmlNode: HtmlNode, tagType: TagType): HtmlElement = while result != nil and result.tagType != tagType: result = result.parentElement +func displayWhitespace*(htmlElem: HtmlElement): bool = + return htmlElem.display == DISPLAY_INLINE or htmlElem.display == DISPLAY_INLINE_BLOCK + proc getRawText*(htmlNode: HtmlNode): string = if htmlNode.isElemNode(): case HtmlElement(htmlNode).tagType @@ -202,9 +203,9 @@ proc getRawText*(htmlNode: HtmlNode): string = result = htmlNode.rawtext.remove("\n") if unicode.strip(result).runeLen() > 0: if htmlNode.nodeAttr().display != DISPLAY_INLINE: - if htmlNode.previousSibling == nil or htmlNode.previousSibling.nodeAttr().display != DISPLAY_INLINE: + if htmlNode.previousSibling == nil or htmlNode.previousSibling.nodeAttr().displayWhitespace(): result = unicode.strip(result, true, false) - if htmlNode.nextSibling == nil or htmlNode.nextSibling.nodeAttr().display != DISPLAY_INLINE: + if htmlNode.nextSibling == nil or htmlNode.nextSibling.nodeAttr().displayWhitespace(): result = unicode.strip(result, false, true) else: result = "" @@ -259,12 +260,12 @@ proc getHtmlElement*(xmlElement: XmlNode, parentNode: HtmlNode): HtmlElement = result.id = xmlElement.attr("id") - if tagType in InlineTagTypes: + if tagType in DisplayInlineTags: result.display = DISPLAY_INLINE - elif tagType in BlockTagTypes: + elif tagType in DisplayBlockTags: result.display = DISPLAY_BLOCK - elif tagType in SingleTagTypes: - result.display = DISPLAY_SINGLE + elif tagType in DisplayInlineBlockTags: + result.display = DISPLAY_INLINE_BLOCK elif tagType == TAG_LI: result.display = DISPLAY_LIST_ITEM else: -- cgit 1.4.1-2-gfad0
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200