about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2021-01-31 23:03:58 +0100
committerbptato <nincsnevem662@gmail.com>2021-01-31 23:03:58 +0100
commit5dfffae926009defec5cc3ab170c645967c68f33 (patch)
treedf8154a8af182b5e13e6e9b1a1a9b70a3129915c
parente3a2ad473e2239bef3726e3ad6657f5f41c9462c (diff)
downloadchawan-5dfffae926009defec5cc3ab170c645967c68f33.tar.gz
meh
-rw-r--r--buffer.nim3
-rw-r--r--display.nim6
-rw-r--r--main.nim50
-rw-r--r--parser.nim11
4 files changed, 39 insertions, 31 deletions
diff --git a/buffer.nim b/buffer.nim
index 1c244baa..e6be7975 100644
--- a/buffer.nim
+++ b/buffer.nim
@@ -257,7 +257,6 @@ proc cursorNextWord*(buffer: Buffer): bool =
   var r: Rune
   var x = buffer.cursorx
   var y = buffer.cursory
-  eprint 1
   if llen >= 0:
     fastRuneAt(buffer.rawtext[y], x, r, false)
 
@@ -265,14 +264,12 @@ proc cursorNextWord*(buffer: Buffer): bool =
       if x >= llen:
         break
       inc x
-      eprint 2
       fastRuneAt(buffer.rawtext[y], x, r, false)
 
     while r == Rune(' '):
       if x >= llen:
         break
       inc x
-      eprint 3
       fastRuneAt(buffer.rawtext[y], x, r, false)
 
   if x >= llen:
diff --git a/display.nim b/display.nim
index da34f6c5..c7cd4bb4 100644
--- a/display.nim
+++ b/display.nim
@@ -275,8 +275,10 @@ proc nrenderHtml*(buffer: Buffer) =
     let currElem = stack.pop()
     buffer.addNode(currElem)
     buffer.renderNode(currElem, state)
-    for item in currElem.childNodes:
-      stack.add(item)
+    var i = currElem.childNodes.len - 1
+    while i >= 0:
+      stack.add(currElem.childNodes[i])
+      i -= 1
 
   buffer.setLastHtmlLine(state)
 
diff --git a/main.nim b/main.nim
index bb709562..a9549165 100644
--- a/main.nim
+++ b/main.nim
@@ -24,16 +24,17 @@ proc loadLocalPage*(url: string): string =
 proc getRemotePage*(url: string): Stream =
   return clientInstance.get(url).bodyStream
 
-proc loadPageUri(uri: Uri, currentcontent: XmlNode): XmlNode =
+proc getLocalPage*(url: string): Stream =
+  return newFileStream(url, fmRead)
+
+proc getPageUri(uri: Uri): Stream =
   var moduri = uri
   moduri.anchor = ""
-  if uri.scheme == "" and uri.path == "" and uri.anchor != "" and currentcontent != nil:
-    return currentcontent
-  elif uri.scheme == "" or uri.scheme == "file":
-    return parseHtml(loadLocalPage($moduri))
+  if uri.scheme == "" or uri.scheme == "file":
+    return getLocalPage($moduri)
   else:
     #return nparseHtml(getRemotePage($moduri))
-    return parseHtml(loadRemotePage($moduri))
+    return getRemotePage($moduri)
 
 var buffers: seq[Buffer]
 
@@ -45,26 +46,25 @@ proc main*() =
     eprint "Failed to read keymap, falling back to default"
   let attrs = getTermAttributes()
   let buffer = newBuffer(attrs)
-  buffer.document = nparseHtml(getRemotePage("http://lite.duckduckgo.com"))
+  let uri = parseUri(paramStr(1))
+  buffers.add(buffer)
+  buffer.document = nparseHtml(getPageUri(uri))
+  buffer.setLocation(uri)
   buffer.nrenderHtml()
-  discard displayPage(getTermAttributes(), buffer)
-  return
-  #let uri = parseUri(paramStr(1))
-  #buffers.add(buffer)
-  #buffer.setLocation(uri)
-  #buffer.htmlSource = loadPageUri(uri, buffer.htmlSource)
-  #buffer.renderHtml()
-  #var lastUri = uri
-  #while displayPage(attrs, buffer):
-  #  statusMsg("Loading...", buffer.height)
-  #  var newUri = buffer.document.location
-  #  lastUri.anchor = ""
-  #  newUri.anchor = ""
-  #  if $lastUri != $newUri:
-  #    buffer.clearBuffer()
-  #    buffer.htmlSource = loadPageUri(buffer.document.location, buffer.htmlSource)
-  #    buffer.renderHtml()
-  #  lastUri = newUri
+  var lastUri = uri
+  while displayPage(attrs, buffer):
+    statusMsg("Loading...", buffer.height)
+    var newUri = buffer.document.location
+    lastUri.anchor = ""
+    newUri.anchor = ""
+    if $lastUri != $newUri:
+      buffer.clearBuffer()
+      if uri.scheme == "" and uri.path == "" and uri.anchor != "":
+        discard
+      else:
+        buffer.document = nparseHtml(getPageUri(buffer.document.location))
+      buffer.nrenderHtml()
+    lastUri = newUri
 
 #waitFor loadPage("https://lite.duckduckgo.com/lite/?q=hello%20world")
 #eprint mk_wcswidth_cjk("abc•de")
diff --git a/parser.nim b/parser.nim
index bb4ab6e1..3873566d 100644
--- a/parser.nim
+++ b/parser.nim
@@ -86,6 +86,8 @@ func newHtmlElement(tagType: TagType, parentNode: HtmlNode): HtmlElement =
   of TAG_H1, TAG_H2, TAG_H3, TAG_H4, TAG_H5, TAG_H6:
     result.bold = true
     result.marginbottom = 1
+  of TAG_A:
+    result.islink = true
   else: discard
 
   if parentNode.isElemNode():
@@ -179,8 +181,10 @@ proc applyNodeText(htmlNode: HtmlNode) =
   htmlNode.rawtext = htmlNode.getRawText()
   htmlNode.fmttext = htmlNode.getFmtText()
 
+#TODO honestly parsexml sucks I should just make my own
 proc nparseHtml*(inputStream: Stream): Document =
   var x: XmlParser
+  let options = @[reportWhitespace, allowUnquotedAttribs, allowEmptyAttribs]
   x.open(inputStream, "")
   var state: ParseState
   let document = newDocument()
@@ -212,8 +216,13 @@ proc nparseHtml*(inputStream: Stream): Document =
         elif x.kind == xmlCharData:
           if x.rawData.strip() == "/>":
             break
+        elif x.kind == xmlElementEnd:
+          break
+        elif x.kind == xmlElementOpen:
+          #wtf??? TODO
+          break
         else:
-          assert(false, "wtf") #TODO
+          assert(false, "wtf " & $x.kind & " " & x.rawdata) #TODO
         x.next()
       s &= ">"
       eprint s