aboutsummaryrefslogtreecommitdiff
path: root/src/gui
diff options
context:
space:
mode:
authorj-james2022-06-24 06:24:18 +0000
committerj-james2022-06-24 06:24:18 +0000
commit1b9287fcfb345aab4b31d7d6f4c0d330bd5e13f3 (patch)
treeb78009bb59e88c14bf6b4e3bae2af13c921a8d0b /src/gui
parentb629bff6211a18327d49d34eaee5a98471308030 (diff)
Basic HTML parsing and terminal rendering
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/terminal.nim35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/gui/terminal.nim b/src/gui/terminal.nim
new file mode 100644
index 0000000..e893081
--- /dev/null
+++ b/src/gui/terminal.nim
@@ -0,0 +1,35 @@
+import std/[strutils, tables], ../formats/html
+
+proc print(node: Node, indent=0, raw=false) =
+ if node.kind == Element:
+ if not raw:
+ stdout.write(" ".repeat(indent))
+ stdout.write("<" & node.tag)
+ for attribute in node.attributes.pairs:
+ stdout.write(" " & attribute[0] & "=" & attribute[1])
+ stdout.write(">")
+ stdout.write('\n')
+ for i in node.nested:
+ i.print(indent+2)
+ if not raw:
+ stdout.write(" ".repeat(indent))
+ stdout.write("</" & node.tag & ">")
+ stdout.write('\n')
+ else:
+ stdout.write(" ".repeat(indent))
+ stdout.write(node.text)
+
+proc render*(html: Html) =
+ for node in html:
+ node.print()
+
+proc renderSource*(html: Html) =
+ for node in html:
+ node.print(0, true)
+
+when isMainModule:
+ import ../protocols/http, ../uri
+ let url = "https://example.org:443/index.html"
+ let request = httpRequest(parseUrl(url))
+ let parsed = parseHTML(request.body)
+ renderSource(parsed)