summary refs log tree commit diff stats
path: root/tools/dochack
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2016-09-09 16:23:54 +0200
committerAndreas Rumpf <rumpf_a@web.de>2016-09-09 16:25:09 +0200
commit7621bb05c9cda4e236161239236a547994a7245d (patch)
tree7a7b04cd3bf54bc4bd0e0d1f2fd5b64a1c86946f /tools/dochack
parent37c3c0033dd3a1054e375a8770f93e4dc3239f4c (diff)
downloadNim-7621bb05c9cda4e236161239236a547994a7245d.tar.gz
docgen: search feature
Diffstat (limited to 'tools/dochack')
-rw-r--r--tools/dochack/dochack.nim59
-rw-r--r--tools/dochack/karax.nim5
2 files changed, 64 insertions, 0 deletions
diff --git a/tools/dochack/dochack.nim b/tools/dochack/dochack.nim
index 7e4ebb487..5529c0798 100644
--- a/tools/dochack/dochack.nim
+++ b/tools/dochack/dochack.nim
@@ -229,3 +229,62 @@ proc groupBy*(value: cstring) {.exportc.} =
   else:
     replaceById("tocRoot", tree("DIV"))
   togglevis(getElementById"toc-list")
+
+var
+  db: seq[Element]
+  contents: seq[cstring]
+
+template normalize(x: cstring): cstring = x.toLower.replace("_", "")
+
+proc dosearch(value: cstring): Element =
+  if db.isNil:
+    var stuff: Element
+    {.emit: """
+    var request = new XMLHttpRequest();
+    request.open("GET", "theindex.html", false);
+    request.send(null);
+
+    // var doc = document.implementation.createHTMLDocument("theindex");
+    // doc.documentElement.innerHTML = request.responseText;
+
+    parser=new DOMParser();
+    doc=parser.parseFromString(request.responseText, "text/html");
+
+    `stuff` = doc.documentElement;
+    """.}
+    db = stuff.getElementsByClass"reference external"
+    contents = @[]
+    for ahref in db:
+      contents.add ahref.textContent.normalize
+  let ul = tree("UL")
+  result = tree("DIV")
+  result.setClass"search_results"
+  var matches = 0
+  let key = value.normalize
+  for i in 0..<db.len:
+    if containsWord(key, contents[i]):
+      ul.add(tree("LI", db[i]))
+      inc matches
+      if matches > 10: break
+  if ul.len == 0:
+    result.add text"no search results"
+  else:
+    result.add ul
+
+var oldtoc: Element
+var timer: Timeout
+
+proc search*() {.exportc.} =
+  proc wrapper() =
+    let elem = getElementById("searchInput")
+    let value = elem.value
+    if value != "":
+      if oldtoc.isNil:
+        oldtoc = getElementById("tocRoot")
+      let results = dosearch(value)
+      replaceById("tocRoot", results)
+    elif not oldtoc.isNil:
+      replaceById("tocRoot", oldtoc)
+
+  if timer != nil: clearTimeout(timer)
+  timer = setTimeout(wrapper, 400)
diff --git a/tools/dochack/karax.nim b/tools/dochack/karax.nim
index d7d9a059a..d9619992b 100644
--- a/tools/dochack/karax.nim
+++ b/tools/dochack/karax.nim
@@ -17,6 +17,11 @@ proc `value=`*(e: Element; v: cstring) {.importcpp: "#.value = #", nodecl.}
 
 proc getElementsByClass*(e: Element; name: cstring): seq[Element] {.importcpp: "#.getElementsByClassName(#)", nodecl.}
 
+proc toLower*(x: cstring): cstring {.
+  importcpp: "#.toLowerCase()", nodecl.}
+proc replace*(x: cstring; search, by: cstring): cstring {.
+  importcpp: "#.replace(#, #)", nodecl.}
+
 type
   EventHandler* = proc(ev: Event)
   EventHandlerId* = proc(ev: Event; id: int)