mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-23 11:26:52 +00:00
docgen: working search feature
This commit is contained in:
@@ -19,20 +19,14 @@ proc renderPlainSymbolName*(n: PNode): string =
|
||||
## for the HTML hyperlinks.
|
||||
result = ""
|
||||
case n.kind
|
||||
of nkPostfix:
|
||||
for i in 0 .. <n.len:
|
||||
result = renderPlainSymbolName(n[<n.len])
|
||||
if result.len > 0:
|
||||
return
|
||||
of nkPostfix, nkAccQuoted:
|
||||
result = renderPlainSymbolName(n[<n.len])
|
||||
of nkIdent:
|
||||
if n.ident.s != "*":
|
||||
result = n.ident.s
|
||||
result = n.ident.s
|
||||
of nkSym:
|
||||
result = n.sym.renderDefinitionName(noQuotes = true)
|
||||
of nkPragmaExpr:
|
||||
result = renderPlainSymbolName(n[0])
|
||||
of nkAccQuoted:
|
||||
result = renderPlainSymbolName(n[<n.len])
|
||||
else:
|
||||
internalError(n.info, "renderPlainSymbolName() with " & $n.kind)
|
||||
assert(not result.isNil)
|
||||
|
||||
@@ -1275,6 +1275,13 @@ dt pre > span.Operator ~ span.Identifier, dt pre > span.Operator ~ span.Operator
|
||||
span.pragmaend {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
div.search_results {
|
||||
background-color: antiquewhite;
|
||||
margin: 3em;
|
||||
border-style: inset;
|
||||
padding: 1em;
|
||||
}
|
||||
</style>
|
||||
|
||||
<script type="text/javascript" src="../dochack.js"></script>
|
||||
|
||||
@@ -419,10 +419,10 @@ proc escapeLink(s: string): string =
|
||||
result = newStringOfCap(s.len + s.len shr 2)
|
||||
for c in items(s):
|
||||
case c
|
||||
of 'a'..'z', '_', 'A'..'Z', '0'..'9':
|
||||
of 'a'..'z', '_', 'A'..'Z', '0'..'9', '.', '#', ',', '/':
|
||||
result.add c
|
||||
else:
|
||||
add(result, "X")
|
||||
add(result, "%")
|
||||
add(result, toHex(ord(c), 2))
|
||||
|
||||
proc generateSymbolIndex(symbols: seq[IndexEntry]): string =
|
||||
@@ -436,7 +436,7 @@ proc generateSymbolIndex(symbols: seq[IndexEntry]): string =
|
||||
var j = i
|
||||
while j < symbols.len and keyword == symbols[j].keyword:
|
||||
let
|
||||
url = symbols[j].link.escapeLink #replace("&", "&")
|
||||
url = symbols[j].link.escapeLink
|
||||
text = if not symbols[j].linkTitle.isNil: symbols[j].linkTitle else: url
|
||||
desc = if not symbols[j].linkDesc.isNil: symbols[j].linkDesc else: ""
|
||||
if desc.len > 0:
|
||||
|
||||
@@ -241,7 +241,7 @@ proc dosearch(value: cstring): Element =
|
||||
var stuff: Element
|
||||
{.emit: """
|
||||
var request = new XMLHttpRequest();
|
||||
request.open("GET", "theindex.html", false);
|
||||
request.open("GET", "http://nim-lang.org/0.15.0/theindex.html", false);
|
||||
request.send(null);
|
||||
|
||||
var doc = document.implementation.createHTMLDocument("theindex");
|
||||
@@ -259,16 +259,22 @@ proc dosearch(value: cstring): Element =
|
||||
let ul = tree("UL")
|
||||
result = tree("DIV")
|
||||
result.setClass"search_results"
|
||||
var matches = 0
|
||||
var matches: seq[(Element, int)] = @[]
|
||||
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
|
||||
let c = contents[i]
|
||||
if c.containsWord(key):
|
||||
matches.add((db[i], -(30_000 - c.len)))
|
||||
elif c.contains(key):
|
||||
matches.add((db[i], c.len))
|
||||
matches.sort do (a, b: auto) -> int:
|
||||
a[1] - b[1]
|
||||
for i in 0..min(<matches.len, 19):
|
||||
ul.add(tree("LI", matches[i][0]))
|
||||
if ul.len == 0:
|
||||
result.add text"no search results"
|
||||
result.add tree("B", text"no search results")
|
||||
else:
|
||||
result.add tree("B", text"search results")
|
||||
result.add ul
|
||||
|
||||
var oldtoc: Element
|
||||
|
||||
Reference in New Issue
Block a user