Skip to content

Commit

Permalink
Search prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
maybeetree committed Sep 2, 2024
1 parent 9782181 commit 7ad7955
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 3 deletions.
44 changes: 44 additions & 0 deletions doc/js/search.js
Original file line number Diff line number Diff line change
@@ -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 += `<a href="${page[0]}">${page[0]}</a><br>`;
}
}

2 changes: 1 addition & 1 deletion doc/templ/root.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
<link href="{{webroot}}fontawesome/css/fontawesome.css" rel="stylesheet" />
<link href="{{webroot}}fontawesome/css/brands.css" rel="stylesheet" />
<link href="{{webroot}}fontawesome/css/solid.css" rel="stylesheet" />

</head>
<body>
<div id="page">
<header>
<h1>RAIDOC</h1>
<a href="{{webroot}}index.html">HOME</a>
<a href="{{webroot}}map.html">ALL PAGES</a>
<a href="{{webroot}}search.html">SEARCH</a>
</header>

<div id="content">
Expand Down
35 changes: 35 additions & 0 deletions doc/templ/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="{{webroot}}css/root.css">
<link rel="stylesheet" href="{{webroot}}pyg.css">

<script src="{{webroot}}search_index.js"></script>
<script src="{{webroot}}js/search.js"></script>

<link href="{{webroot}}fontawesome/css/fontawesome.css" rel="stylesheet" />
<link href="{{webroot}}fontawesome/css/brands.css" rel="stylesheet" />
<link href="{{webroot}}fontawesome/css/solid.css" rel="stylesheet" />
</head>
<body>
<div id="page">
<header>
<h1>RAIDOC</h1>
<a href="{{webroot}}index.html">HOME</a>
<a href="{{webroot}}map.html">ALL PAGES</a>
<a href="{{webroot}}search.html">SEARCH</a>
</header>

<div id="content">
<input type="text" id="searchbox">
<button id="searchbutton" onclick="do_search();">
Search
</button>
<div id="search-results">
</div>
</div>
</div>
</body>
</html>

25 changes: 24 additions & 1 deletion src/raidoc/build.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -137,10 +147,23 @@ def build(source='./doc', dest='./build'):
],
).wait()


template = env.from_string((source / 'templ/map.html').read_text())
html = template.render({
'webroot': '',
'map_cmap': map_cmap.read_text()
})
(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)


31 changes: 30 additions & 1 deletion src/raidoc/raimark_ext.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import subprocess
import re
import copy

import marko

Expand Down Expand Up @@ -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__ = []
Expand Down Expand Up @@ -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,
]
)

0 comments on commit 7ad7955

Please sign in to comment.