summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--config/nimdoc.cfg9
-rw-r--r--tools/dochack/dochack.nim59
-rw-r--r--tools/dochack/karax.nim5
3 files changed, 73 insertions, 0 deletions
diff --git a/config/nimdoc.cfg b/config/nimdoc.cfg
index b953383de..742217e59 100644
--- a/config/nimdoc.cfg
+++ b/config/nimdoc.cfg
@@ -74,9 +74,14 @@ $content
 doc.body_toc = """
 <div class="row">
   <div class="three columns">
+  <div>
+    Search: <input type="text" id="searchInput"
+      onkeyup="search()" />
+  </div>
   $tableofcontents
   </div>
   <div class="nine columns" id="content">
+  <div id="tocRoot"></div>
   <p class="module-desc">$moduledesc</p>
   $content
   </div>
@@ -87,6 +92,10 @@ doc.body_toc_group = """
 <div class="row">
   <div class="three columns">
   <div>
+    Search: <input type="text" id="searchInput"
+      onkeyup="search()" />
+  </div>
+  <div>
     Group by:
     <select onchange="groupBy(this.value)">
       <option value="section">Section</option>
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)