aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorj-james2022-07-02 05:58:19 +0000
committerj-james2022-07-02 05:58:19 +0000
commitaace82a89073d3547856742e9f3a403b1f3fba03 (patch)
treee430cd0b5b8688a4512b80981968ac6eff685f5d /src
parent1b9287fcfb345aab4b31d7d6f4c0d330bd5e13f3 (diff)
Move URI handling and rename node.nested to node.children
Diffstat (limited to 'src')
-rw-r--r--src/browser.nim11
-rw-r--r--src/formats/html.nim15
-rw-r--r--src/formats/uri.nim (renamed from src/uri.nim)0
-rw-r--r--src/gui/terminal.nim4
-rw-r--r--src/protocols/http.nim2
5 files changed, 17 insertions, 15 deletions
diff --git a/src/browser.nim b/src/browser.nim
index 97713a3..c8fdea8 100644
--- a/src/browser.nim
+++ b/src/browser.nim
@@ -1,21 +1,21 @@
-import std/strutils, protocols/http, html, uri
+import std/strutils, protocols/http, formats/[html, uri], gui/terminal
let url = "https://example.org:443/index.html"
# let url = paramStr(1)
-proc request(uri: string) =
+proc request*(uri: string) =
# This is probably the best place to implement scheme-specific stuff
let url = parseURL(uri)
case url.scheme:
of "http", "https":
let response = httpRequest(url)
- renderHTML(response.body)
+ render(parseHTML(response.body))
# Exercise: view-source
of "view-source":
# We must parse the url again without the view-source: prefix
let url = uri.split(':', maxsplit=1)[1]
let response = httpRequest(parseURL(url))
- renderSource(response.body)
+ renderSource(parseHTML((response.body)))
# Exercise: file:// scheme
of "file":
discard
@@ -25,7 +25,8 @@ proc request(uri: string) =
else:
raise newException(Exception, "Not a valid scheme: " & url.scheme)
-request(url)
+when isMainModule:
+ request(url)
# HTTP/1.1
# Compression
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
diff --git a/src/uri.nim b/src/formats/uri.nim
index e2fc7f3..e2fc7f3 100644
--- a/src/uri.nim
+++ b/src/formats/uri.nim
diff --git a/src/gui/terminal.nim b/src/gui/terminal.nim
index e893081..f4c7c92 100644
--- a/src/gui/terminal.nim
+++ b/src/gui/terminal.nim
@@ -9,7 +9,7 @@ proc print(node: Node, indent=0, raw=false) =
stdout.write(" " & attribute[0] & "=" & attribute[1])
stdout.write(">")
stdout.write('\n')
- for i in node.nested:
+ for i in node.children:
i.print(indent+2)
if not raw:
stdout.write(" ".repeat(indent))
@@ -28,7 +28,7 @@ proc renderSource*(html: Html) =
node.print(0, true)
when isMainModule:
- import ../protocols/http, ../uri
+ import ../protocols/http, ../formats/uri
let url = "https://example.org:443/index.html"
let request = httpRequest(parseUrl(url))
let parsed = parseHTML(request.body)
diff --git a/src/protocols/http.nim b/src/protocols/http.nim
index df812fd..2de11df 100644
--- a/src/protocols/http.nim
+++ b/src/protocols/http.nim
@@ -1,4 +1,4 @@
-import std/[strutils, net], ../uri
+import std/[strutils, net], ../formats/uri
# https://datatracker.ietf.org/doc/html/rfc1945