aboutsummaryrefslogtreecommitdiff
path: root/src/formats/html.nim
diff options
context:
space:
mode:
Diffstat (limited to 'src/formats/html.nim')
-rw-r--r--src/formats/html.nim15
1 files changed, 8 insertions, 7 deletions
diff --git a/src/formats/html.nim b/src/formats/html.nim
index 8724295..8299a51 100644
--- a/src/formats/html.nim
+++ b/src/formats/html.nim
@@ -13,16 +13,17 @@ type NodeKind* = enum
# Clever node implementation from callsamu and XmlNodeObj
# Note that Text nodes are _only_ text.
# ex. this <a>test</a> node is three nodes: "this ", " node", and the <a> tag
-type Node* {.acyclic.} = object
+type Node* = ref object
+ # parent*: Node # Unfortunately, we will have to deal with cycles.
case kind*: NodeKind:
of Text:
text*: string
of Element:
tag*: string
attributes*: Table[string, string] # change
- nested*: seq[Node]
+ children*: seq[Node]
-# Note that even plain text is valid HTML.
+# Note that even plain text is valid HTML, in this implementation.
type Html* = seq[Node]
type ParserState = enum
@@ -54,7 +55,7 @@ func conclude(buffer: string, unfinished: var seq[Node], result: var Html) =
let split: seq[string] = buffer.strip(false, true, {'/'}).strip().split(' ')
let tag = split[0].toLower
let attributes = split[1..^1].attributes
- let node = Node(kind: Element, tag: tag, attributes: attributes, nested: @[])
+ let node = Node(kind: Element, tag: tag, attributes: attributes, children: @[])
# If we're in a self-closing tag:
if tag in self_closing_tags:
@@ -64,7 +65,7 @@ func conclude(buffer: string, unfinished: var seq[Node], result: var Html) =
if tag.len > 0 and tag[0] == '/' or tag in self_closing_tags:
# Add the element to the parent node
if unfinished.len > 1:
- unfinished[^2].nested.add(unfinished.pop)
+ unfinished[^2].children.add(unfinished.pop)
# Or, if there is no parent node, add the element to the result
else:
result.add(unfinished.pop)
@@ -75,7 +76,7 @@ func conclude(buffer: string, unfinished: var seq[Node], result: var Html) =
func finish(unfinished: var seq[Node], result: var seq[Node]) =
while unfinished.len > 1:
- unfinished[^2].nested.add(unfinished.pop)
+ unfinished[^2].children.add(unfinished.pop)
if unfinished.len == 1:
result.add(unfinished.pop)
@@ -90,7 +91,7 @@ func parseHTML*(html: string): Html =
if not in_tag and c == '<':
# Add the collected text content to the parent node, if there is text
if buffer.strip() != "":
- unfinished[^1].nested.add(Node(kind: Text, text: buffer))
+ unfinished[^1].children.add(Node(kind: Text, text: buffer))
in_tag = true
buffer = ""
# End of a tag