From 7ad79556d055b2350ad4b3db0e1fbe5f54e13d9d Mon Sep 17 00:00:00 2001 From: maybetree Date: Mon, 2 Sep 2024 15:52:18 +0200 Subject: [PATCH] Search prototype --- doc/js/search.js | 44 +++++++++++++++++++++++++++++++++++++++ doc/templ/root.html | 2 +- doc/templ/search.html | 35 +++++++++++++++++++++++++++++++ src/raidoc/build.py | 25 +++++++++++++++++++++- src/raidoc/raimark_ext.py | 31 ++++++++++++++++++++++++++- 5 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 doc/js/search.js create mode 100644 doc/templ/search.html diff --git a/doc/js/search.js b/doc/js/search.js new file mode 100644 index 0000000..14981ed --- /dev/null +++ b/doc/js/search.js @@ -0,0 +1,44 @@ +function search(term) { + results = []; + for (const page of search_index) { + score = 0; + page_name = page[0]; + page_keywords = page[1]; + + for (var rank = 0; rank < page_keywords.length; rank++) { + for (const keyword of page_keywords[rank]) { + if (!keyword.includes(term)) { + continue; + } + score += (page_keywords.length - rank); + } + } + + if (score <= 0) { + continue; + } + + for (var i = 0; i < results.length; i++) { + if (results[i][1] < score) { + break; + } + } + + results.splice(i, 0, [page_name, score]); + } + + return results; +} + +function do_search() { + term = document.getElementById('searchbox').value; + console.log(term); + results = search(term); + div = document.getElementById("search-results"); + div.innerHTML = ""; + + for (const page of results) { + div.innerHTML += `${page[0]}
`; + } +} + diff --git a/doc/templ/root.html b/doc/templ/root.html index bbfd87a..76e5d38 100644 --- a/doc/templ/root.html +++ b/doc/templ/root.html @@ -9,7 +9,6 @@ -
@@ -17,6 +16,7 @@

RAIDOC

HOME ALL PAGES + SEARCH
diff --git a/doc/templ/search.html b/doc/templ/search.html new file mode 100644 index 0000000..894f7cb --- /dev/null +++ b/doc/templ/search.html @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + +
+
+

RAIDOC

+ HOME + ALL PAGES + SEARCH +
+ +
+ + +
+
+
+
+ + + diff --git a/src/raidoc/build.py b/src/raidoc/build.py index 76b9a24..8aeeb16 100644 --- a/src/raidoc/build.py +++ b/src/raidoc/build.py @@ -1,13 +1,14 @@ from pathlib import Path import shutil import subprocess +import json import sass import jinja2 import marko from pygments.formatters import HtmlFormatter -from raidoc.raimark_ext import RaimarkExt, LinkMixin +from raidoc.raimark_ext import RaimarkExt, LinkMixin, IndexerMixin from raidoc.autogen import FilesystemScanner import raimad @@ -44,6 +45,8 @@ def build(source='./doc', dest='./build'): md = marko.Markdown(extensions=['gfm', 'codehilite', RaimarkExt]) + search_index = [] + env = jinja2.Environment( autoescape=False, undefined=jinja2.StrictUndefined, @@ -77,6 +80,8 @@ def build(source='./doc', dest='./build'): markdown = path.read_text() LinkMixin.links_to = [] + IndexerMixin.init() + try: content = md(markdown) except Exception as e: @@ -106,6 +111,11 @@ def build(source='./doc', dest='./build'): destpath = destparent / f"{path.stem}.html" destpath.write_text(html) + search_index.append(( + str(path.relative_to(source).with_suffix('.html')), + IndexerMixin.get_index_entry() + )) + graph.append('}') map_gv = Path(dest / 'map.gv') @@ -137,6 +147,7 @@ def build(source='./doc', dest='./build'): ], ).wait() + template = env.from_string((source / 'templ/map.html').read_text()) html = template.render({ 'webroot': '', @@ -144,3 +155,15 @@ def build(source='./doc', dest='./build'): }) (dest / 'map.html').write_text(html) + with (dest / 'search_index.js').open('w') as file: + file.write('var search_index = ') + json.dump(search_index, file) + file.write(';\n') + + template = env.from_string((source / 'templ/search.html').read_text()) + html = template.render({ + 'webroot': '', + }) + (dest / 'search.html').write_text(html) + + diff --git a/src/raidoc/raimark_ext.py b/src/raidoc/raimark_ext.py index 3d48d07..e72e1d0 100644 --- a/src/raidoc/raimark_ext.py +++ b/src/raidoc/raimark_ext.py @@ -1,5 +1,6 @@ import subprocess import re +import copy import marko @@ -99,6 +100,28 @@ def render_emphasis(self, element): )) +class IndexerMixin(object): + + index_entry = None + + @classmethod + def init(cls): + cls.index_entry = tuple([] for _ in range(10)) + + def render_heading(self, element): + + html_text = super().render_heading(element) + plain_text = re.sub('<[^>]*>', '', html_text).strip() + # TODO horrible inefficient + + self.index_entry[element.level - 1].append(plain_text) + + return html_text + + @classmethod + def get_index_entry(cls): + return copy.deepcopy(cls.index_entry) + codeblock_preamble = """ __raimark_output__ = [] @@ -213,6 +236,12 @@ def _exec_dot(self, code): RaimarkExt = marko.helpers.MarkoExtension( - renderer_mixins=[LinkMixin, CodeBlockMixin, CalloutMixin, EmphasisMixin] + renderer_mixins=[ + LinkMixin, + CodeBlockMixin, + CalloutMixin, + EmphasisMixin, + IndexerMixin, + ] )