aboutsummaryrefslogtreecommitdiff
path: root/src/gui/terminal.nim
blob: f4c7c92ce9d607e8165d834ae91dd472c0b8e2e7 (plain) (blame)
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
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.children:
      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, ../formats/uri
  let url = "https://example.org:443/index.html"
  let request = httpRequest(parseUrl(url))
  let parsed = parseHTML(request.body)
  renderSource(parsed)