searching!

This commit is contained in:
Akbar Rahman 2021-08-19 14:43:42 +01:00
parent 1e0135a8e5
commit 51ba98f045
6 changed files with 2325 additions and 0 deletions

View File

@ -4,6 +4,8 @@ install:
mkdir -p /opt/notes2web mkdir -p /opt/notes2web
cp -r templates /opt/notes2web cp -r templates /opt/notes2web
cp styles.css /opt/notes2web cp styles.css /opt/notes2web
cp fuse.js /opt/notes2web
cp search.js /opt/notes2web
uninstall: uninstall:
rm -rf /usr/local/bin/notes2web.py /opt/notes2web rm -rf /usr/local/bin/notes2web.py /opt/notes2web

2255
fuse.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -10,6 +10,7 @@ import pypandoc
import shutil import shutil
import os import os
import re import re
import json
TEXT_ARTICLE_TEMPLATE_FOOT = None TEXT_ARTICLE_TEMPLATE_FOOT = None
@ -26,6 +27,8 @@ def get_files(folder):
for root, folders, files in os.walk(folder): for root, folders, files in os.walk(folder):
for filename in files: for filename in files:
if '/.git' in root:
continue
name = os.path.join(root, filename) name = os.path.join(root, filename)
if os.path.splitext(name)[1] == '.md': if os.path.splitext(name)[1] == '.md':
markdown.append(name) markdown.append(name)
@ -68,6 +71,8 @@ def get_args():
parser.add_argument('-e', '--extra-index-content', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/templates/extra_index_content.html')) parser.add_argument('-e', '--extra-index-content', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/templates/extra_index_content.html'))
parser.add_argument('-n', '--index-article-names', action='append', default=['index.md']) parser.add_argument('-n', '--index-article-names', action='append', default=['index.md'])
parser.add_argument('-F', '--force', action="store_true", help="Generate new output html even if source file was modified before output html") parser.add_argument('-F', '--force', action="store_true", help="Generate new output html even if source file was modified before output html")
parser.add_argument('--fuse', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/fuse.js'))
parser.add_argument('--searchjs', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/search.js'))
return parser.parse_args() return parser.parse_args()
@ -97,6 +102,7 @@ def main(args):
markdown_files, plaintext_files, other_files = get_files(args.notes) markdown_files, plaintext_files, other_files = get_files(args.notes)
all_entries=[]
print(f"{args.index_article_names=}") print(f"{args.index_article_names=}")
@ -131,6 +137,11 @@ def main(args):
'title': fm.get('title') 'title': fm.get('title')
} ] } ]
print(f"{output_filename=}") print(f"{output_filename=}")
all_entries.append({
'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])),
'title': fm.get('title'),
'tags': fm.get('tags')
})
if update_required(filename, output_filename) or args.force: if update_required(filename, output_filename) or args.force:
html = pypandoc.convert_file(filename, 'html', extra_args=[f'--template={args.template}']) html = pypandoc.convert_file(filename, 'html', extra_args=[f'--template={args.template}'])
@ -153,11 +164,21 @@ def main(args):
with open(output_filename, 'w+') as fp: with open(output_filename, 'w+') as fp:
fp.write(html) fp.write(html)
all_entries.append({
'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])),
'title': title,
'tags': []
})
print(f"{other_files=}") print(f"{other_files=}")
for filename in other_files: for filename in other_files:
output_filename = re.sub(f"^{args.notes.name}", os.path.join(args.output_dir.name, 'notes'), filename) output_filename = re.sub(f"^{args.notes.name}", os.path.join(args.output_dir.name, 'notes'), filename)
os.makedirs(os.path.dirname(output_filename), exist_ok=True) os.makedirs(os.path.dirname(output_filename), exist_ok=True)
all_entries.append({
'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])),
'title': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])),
'tags': []
})
shutil.copyfile(filename, output_filename) shutil.copyfile(filename, output_filename)
tagdir = os.path.join(args.output_dir, '.tags') tagdir = os.path.join(args.output_dir, '.tags')
@ -236,10 +257,15 @@ def main(args):
fp.write(html) fp.write(html)
shutil.copyfile(args.stylesheet, os.path.join(args.output_dir.name, 'styles.css')) shutil.copyfile(args.stylesheet, os.path.join(args.output_dir.name, 'styles.css'))
shutil.copyfile(args.fuse, os.path.join(args.output_dir.name, 'fuse.js'))
shutil.copyfile(args.searchjs, os.path.join(args.output_dir.name, 'search.js'))
with open(os.path.join(args.output_dir.name, 'index.html'), 'w+') as fp: with open(os.path.join(args.output_dir.name, 'index.html'), 'w+') as fp:
with open(args.home_index) as fp2: with open(args.home_index) as fp2:
html = re.sub(r'\$title\$', args.output_dir.parts[0], fp2.read()) html = re.sub(r'\$title\$', args.output_dir.parts[0], fp2.read())
html = re.sub(r'\$h1title\$', args.output_dir.parts[0], html) html = re.sub(r'\$h1title\$', args.output_dir.parts[0], html)
html = re.sub(r'\$data\$', json.dumps(all_entries), html)
fp.write(html) fp.write(html)
print(tag_dict) print(tag_dict)

23
search.js Normal file
View File

@ -0,0 +1,23 @@
const fuse = new Fuse(data, {
keys: ['path', 'title', 'tags']
})
const searchBar = document.getElementById('search')
const results = document.getElementById('results')
function callback() {
console.log("called")
console.log(searchBar.value)
results.innerHTML = ''
fuse.search(searchBar.value).forEach(r => {
wrapper = document.createElement('div')
content = document.createElement('a')
content.innerHTML = r.item.title
content.href = r.item.path
wrapper.appendChild(content)
wrapper.className = "article"
results.appendChild(wrapper)
})
}
searchBar.addEventListener('keyup', callback)

View File

@ -72,3 +72,14 @@ blockquote {
blockquote * { blockquote * {
margin: 0; margin: 0;
} }
#search {
padding: 1em;
margin: 1em;
font-size: 1em;
width: auto;
}
#results {
overflow-x: scroll;
}

View File

@ -12,5 +12,13 @@ These are my personal notes. Correctness is not guaranteed.
Browse <a href="/notes">here</a> or by tag <a href="/.tags">here</a>. Browse <a href="/notes">here</a> or by tag <a href="/.tags">here</a>.
</p> </p>
<input placeholder="Search" id="search">
<div id="results">
</div>
<p style="font-size: 0.7em;"> page generated by <a href="https://github.com/alvierahman90/notes2web">notes2web</a></p> <p style="font-size: 0.7em;"> page generated by <a href="https://github.com/alvierahman90/notes2web">notes2web</a></p>
<script src="/fuse.js"> </script>
<script> const data = $data$ </script>
<script src="/search.js"> </script>
</body> </body>