import dom import fuzzysearch proc textContent(e: Element): cstring {. importcpp: "#.textContent", nodecl.} proc textContent(e: Node): cstring {. importcpp: "#.textContent", nodecl.} proc tree(tag: string; kids: varargs[Element]): Element = result = document.createElement tag for k in kids: result.appendChild k proc add(parent, kid: Element) = if parent.nodeName == cstring"TR" and ( kid.nodeName == cstring"TD" or kid.nodeName == cstring"TH"): let k = document.createElement("TD") appendChild(k, kid) appendChild(parent, k) else: appendChild(parent, kid) proc setClass(e: Element; value: string) = e.setAttribute("class", value) proc text(s: string): Element = cast[Element](document.createTextNode(s)) proc text(s: cstring): Element = cast[Element](document.createTextNode(s)) proc getElementById(id: cstring): Element {.importc: "document.getElementById", nodecl.} proc replaceById(id: cstring; newTree: Node) = let x = getElementById(id) x.parentNode.replaceChild(newTree, x) newTree.id = id proc findNodeWith(x: Element; tag, content: cstring): Element = if x.nodeName == tag and x.textContent == content: return x for i in 0.. y: return 1 return 0 else: # ensure sorting is stable: return a.sortId - b.sortId ) for k in x.kids: let y = toHtml(k) if y != nil: ul.add tree("LI", y) if ul.len != 0: result.add ul if result.len == 0: result = nil #proc containsWord(a, b: cstring): bool {.asmNoStackFrame.} = #{.emit: """ #var escaped = `b`.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); #return new RegExp("\\b" + escaped + "\\b").test(`a`); #""".} proc isWhitespace(text: cstring): bool {.asmNoStackFrame.} = {.emit: """ return !/[^\s]/.test(`text`); """.} proc isWhitespace(x: Element): bool = x.nodeName == cstring"#text" and x.textContent.isWhitespace or x.nodeName == cstring"#comment" proc toToc(x: Element; father: TocEntry) = if x.nodeName == cstring"UL": let f = TocEntry(heading: nil, kids: @[], sortId: father.kids.len) var i = 0 while i < x.len: var nxt = i+1 while nxt < x.len and x[nxt].isWhitespace: inc nxt if nxt < x.len and x[i].nodeName == cstring"LI" and x[i].len == 1 and x[nxt].nodeName == cstring"UL": let e = TocEntry(heading: x[i][0], kids: @[], sortId: f.kids.len) let it = x[nxt] for j in 0..", "text/html"); `stuff` = doc.documentElement; """.} db = stuff.getElementsByClass"reference" contents = @[] for ahref in db: contents.add ahref.getAttribute("data-doc-search-tag") let ul = tree("UL") result = tree("DIV") result.setClass"search_results" var matches: seq[(Node, int)] = @[] for i in 0..