Compare commits

...

2 Commits

Author SHA1 Message Date
51ba98f045 searching! 2021-08-19 14:43:42 +01:00
1e0135a8e5 move notes into notes subdirectory for cleaner look 2021-08-19 13:41:19 +01:00
7 changed files with 2361 additions and 6 deletions

View File

@ -4,6 +4,8 @@ install:
mkdir -p /opt/notes2web
cp -r templates /opt/notes2web
cp styles.css /opt/notes2web
cp fuse.js /opt/notes2web
cp search.js /opt/notes2web
uninstall:
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,8 @@ import pypandoc
import shutil
import os
import re
import json
TEXT_ARTICLE_TEMPLATE_FOOT = None
TEXT_ARTICLE_TEMPLATE_HEAD = None
@ -17,6 +19,7 @@ INDEX_TEMPLATE_FOOT = None
INDEX_TEMPLATE_HEAD = None
EXTRA_INDEX_CONTENT = None
def get_files(folder):
markdown = []
plaintext = []
@ -24,6 +27,8 @@ def get_files(folder):
for root, folders, files in os.walk(folder):
for filename in files:
if '/.git' in root:
continue
name = os.path.join(root, filename)
if os.path.splitext(name)[1] == '.md':
markdown.append(name)
@ -35,6 +40,7 @@ def get_files(folder):
return markdown, plaintext, other
def get_dirs(folder):
r = []
@ -43,6 +49,7 @@ def get_dirs(folder):
return r
def update_required(src_filename, output_filename):
return not os.path.exists(output_filename) or os.path.getmtime(src_filename) > os.path.getmtime(output_filename)
@ -60,9 +67,12 @@ def get_args():
parser.add_argument('-i', '--template-index-head', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/templates/indexhead.html'))
parser.add_argument('-I', '--template-index-foot', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/templates/indexfoot.html'))
parser.add_argument('-s', '--stylesheet', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/styles.css'))
parser.add_argument('--home_index', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/templates/home_index.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('-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()
@ -92,6 +102,7 @@ def main(args):
markdown_files, plaintext_files, other_files = get_files(args.notes)
all_entries=[]
print(f"{args.index_article_names=}")
@ -105,12 +116,12 @@ def main(args):
if os.path.basename(filename) in args.index_article_names:
output_filename = os.path.join(
os.path.dirname(re.sub(f"^{args.notes.name}", args.output_dir.name, filename)),
os.path.dirname(re.sub(f"^{args.notes.name}", os.path.join(args.output_dir.name, 'notes', filename))),
'index.html'
)
dirs_with_index_article.append(os.path.dirname(re.sub(f"^{args.notes.name}", args.output_dir.name, filename)))
dirs_with_index_article.append(os.path.dirname(re.sub(f"^{args.notes.name}", os.path.join(args.output_dir.name, 'notes'), filename)))
else:
output_filename = os.path.splitext(re.sub(f"^{args.notes.name}", args.output_dir.name, filename))[0] + '.html'
output_filename = os.path.splitext(re.sub(f"^{args.notes.name}", os.path.join(args.output_dir.name, 'notes'), filename))[0] + '.html'
fm = frontmatter.load(filename)
if isinstance(fm.get('tags'), list):
@ -126,6 +137,11 @@ def main(args):
'title': fm.get('title')
} ]
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:
html = pypandoc.convert_file(filename, 'html', extra_args=[f'--template={args.template}'])
@ -137,7 +153,7 @@ def main(args):
print(f"{plaintext_files=}")
for filename in plaintext_files:
title = os.path.basename(re.sub(f"^{args.notes.name}", args.output_dir.name, filename))
output_filename = re.sub(f"^{args.notes.name}", args.output_dir.name, filename) + '.html'
output_filename = re.sub(f"^{args.notes.name}", os.path.join(args.output_dir.name, 'notes'), filename) + '.html'
os.makedirs(os.path.dirname(output_filename), exist_ok=True)
html = re.sub(r'\$title\$', title, TEXT_ARTICLE_TEMPLATE_HEAD)
html = re.sub(r'\$h1title\$', title, html)
@ -148,11 +164,21 @@ def main(args):
with open(output_filename, 'w+') as fp:
fp.write(html)
all_entries.append({
'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])),
'title': title,
'tags': []
})
print(f"{other_files=}")
for filename in other_files:
output_filename = re.sub(f"^{args.notes.name}", args.output_dir.name, 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)
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)
tagdir = os.path.join(args.output_dir, '.tags')
@ -231,6 +257,16 @@ def main(args):
fp.write(html)
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(args.home_index) as fp2:
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'\$data\$', json.dumps(all_entries), html)
fp.write(html)
print(tag_dict)
return 0

View File

@ -40,7 +40,8 @@ Output of `notes2web.py --help`:
usage: notes2web.py [-h] [-o OUTPUT_DIR] [-t TEMPLATE] [-H TEMPLATE_TEXT_HEAD]
[-f TEMPLATE_TEXT_FOOT] [-i TEMPLATE_INDEX_HEAD]
[-I TEMPLATE_INDEX_FOOT] [-s STYLESHEET]
[-e EXTRA_INDEX_CONTENT] [-n INDEX_ARTICLE_NAMES]
[--home_index HOME_INDEX] [-e EXTRA_INDEX_CONTENT]
[-n INDEX_ARTICLE_NAMES] [-F]
notes
positional arguments:
@ -55,8 +56,11 @@ optional arguments:
-i TEMPLATE_INDEX_HEAD, --template-index-head TEMPLATE_INDEX_HEAD
-I TEMPLATE_INDEX_FOOT, --template-index-foot TEMPLATE_INDEX_FOOT
-s STYLESHEET, --stylesheet STYLESHEET
--home_index HOME_INDEX
-e EXTRA_INDEX_CONTENT, --extra-index-content EXTRA_INDEX_CONTENT
-n INDEX_ARTICLE_NAMES, --index-article-names INDEX_ARTICLE_NAMES
-F, --force Generate new output html even if source file was
modified before output html
```
The command will generate a website in the `output-dir` directory (`./web` by default).

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 * {
margin: 0;
}
#search {
padding: 1em;
margin: 1em;
font-size: 1em;
width: auto;
}
#results {
overflow-x: scroll;
}

24
templates/home_index.html Normal file
View File

@ -0,0 +1,24 @@
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Open+Sans&display=swap" />
<link rel="stylesheet" type="text/css" href="/styles.css" />
<title>$title$</title>
</head>
<body>
<h1>$h1title$</h1>
<p>
These are my personal notes. Correctness is not guaranteed.
Browse <a href="/notes">here</a> or by tag <a href="/.tags">here</a>.
</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>
<script src="/fuse.js"> </script>
<script> const data = $data$ </script>
<script src="/search.js"> </script>
</body>