2021-04-30 20:55:13 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2021-05-01 09:57:16 +00:00
|
|
|
[[ "$1" == "--help" ]] && echo "USAGE: $0 [NOTES_DIRECTORY_1 [NOTES_DIRECTORY_2 [...]]]" && exit 0
|
2021-04-30 20:55:13 +00:00
|
|
|
|
2021-05-01 11:08:21 +00:00
|
|
|
|
2021-05-01 11:21:59 +00:00
|
|
|
# set default config values, load user config, export config variables
|
|
|
|
name=""
|
|
|
|
article_template="/opt/notes2web/templates/article.html"
|
|
|
|
listitem_template="/opt/notes2web/templates/listitem.html"
|
|
|
|
index_template="/opt/notes2web/templates/index.html"
|
|
|
|
stylesheet="/opt/notes2web/styles.css"
|
|
|
|
|
2021-05-01 10:48:04 +00:00
|
|
|
for configpath in "$HOME/.notes2web.conf" "$HOME/.config/notes2web/config" ".notes2web.conf"
|
|
|
|
do
|
|
|
|
[[ -f "$configpath" ]] && source "$configpath"
|
|
|
|
done
|
|
|
|
|
2021-05-01 11:21:59 +00:00
|
|
|
export name
|
|
|
|
export article_template
|
|
|
|
export listitem_template
|
|
|
|
export index_template
|
|
|
|
export stylesheet
|
|
|
|
|
2021-05-01 11:08:21 +00:00
|
|
|
|
2021-05-01 10:29:48 +00:00
|
|
|
function _renderarticle {
|
2021-04-30 20:55:13 +00:00
|
|
|
echo "rendering $1"
|
|
|
|
pandoc\
|
|
|
|
--toc\
|
|
|
|
--standalone\
|
|
|
|
-t html\
|
2021-05-01 11:21:59 +00:00
|
|
|
--template "$article_template"\
|
2021-04-30 20:55:13 +00:00
|
|
|
-o "$(dirname "$1")/index.html"\
|
|
|
|
"$1"\
|
|
|
|
--mathjax
|
|
|
|
}
|
|
|
|
|
2021-05-01 11:08:21 +00:00
|
|
|
|
2021-05-01 09:57:16 +00:00
|
|
|
function _adddirtoindex {
|
|
|
|
dir="$(dirname "$1")"
|
2021-05-01 10:48:41 +00:00
|
|
|
echo "<h2>$(basename "$dir") notes</h2>" >> index.md
|
2021-05-01 09:57:16 +00:00
|
|
|
find "$dir" -name '*.md' -exec bash -c "_addarticletoindex '{}'" \;
|
|
|
|
}
|
|
|
|
|
2021-05-01 11:08:21 +00:00
|
|
|
|
2021-05-01 09:57:16 +00:00
|
|
|
function _addarticletoindex {
|
2021-04-30 20:55:13 +00:00
|
|
|
echo "adding $1 to list of notes"
|
|
|
|
pandoc\
|
|
|
|
-t html\
|
|
|
|
-V "filepath=$(dirname "$1")"\
|
2021-05-01 11:21:59 +00:00
|
|
|
--template "$listitem_template"\
|
2021-04-30 20:55:13 +00:00
|
|
|
"$1"\
|
|
|
|
>> index.md
|
|
|
|
}
|
|
|
|
|
2021-05-01 11:08:21 +00:00
|
|
|
|
2021-05-01 10:29:48 +00:00
|
|
|
export -f _renderarticle
|
2021-05-01 09:57:16 +00:00
|
|
|
export -f _adddirtoindex
|
|
|
|
export -f _addarticletoindex
|
2021-04-30 20:55:13 +00:00
|
|
|
|
2021-05-01 11:08:21 +00:00
|
|
|
|
|
|
|
#render each markdown file in every folder passed in args
|
2021-05-01 09:57:16 +00:00
|
|
|
for file in "$@"
|
2021-04-30 20:55:13 +00:00
|
|
|
do
|
2021-05-01 10:29:48 +00:00
|
|
|
find "$file" -name '*.md' -exec bash -c "_renderarticle '{}'" \;
|
2021-04-30 20:55:13 +00:00
|
|
|
done
|
|
|
|
|
2021-05-01 11:08:21 +00:00
|
|
|
|
2021-04-30 20:55:13 +00:00
|
|
|
# create an intermediate markdown file of links to each article
|
2021-05-01 10:48:04 +00:00
|
|
|
echo "---" > index.md
|
|
|
|
[[ -z "$name" ]] && echo "title: notes" >> index.md || echo "title: ${name}'s notes" >> index.md
|
|
|
|
echo "---" >> index.md
|
2021-04-30 20:55:13 +00:00
|
|
|
|
2021-05-01 11:08:21 +00:00
|
|
|
|
|
|
|
# mark folders to be included in notes2web's index
|
2021-05-01 09:57:16 +00:00
|
|
|
for file in "$@"
|
2021-04-30 20:55:13 +00:00
|
|
|
do
|
2021-05-01 09:57:16 +00:00
|
|
|
[[ ! -f "$file" ]] && echo "the presence of this files tells notes2web that it should be added to the notes2web index" > "$file/.2web"
|
2021-04-30 20:55:13 +00:00
|
|
|
done
|
|
|
|
|
2021-05-01 11:08:21 +00:00
|
|
|
|
|
|
|
# add articles to index and render
|
2021-05-01 09:57:16 +00:00
|
|
|
find -name '.2web' -exec bash -c "_adddirtoindex '{}'" \;
|
|
|
|
|
2021-05-01 11:08:21 +00:00
|
|
|
|
|
|
|
echo "copying styles.css to current directory"
|
2021-05-01 11:21:59 +00:00
|
|
|
cp "$stylesheet" ./styles.css
|
2021-05-01 11:08:21 +00:00
|
|
|
|
|
|
|
|
2021-05-01 10:29:48 +00:00
|
|
|
echo "rendering index.md"
|
|
|
|
pandoc\
|
|
|
|
-t html\
|
2021-05-01 11:21:59 +00:00
|
|
|
--template "$index_template"\
|
2021-05-01 10:29:48 +00:00
|
|
|
-o index.html\
|
|
|
|
"index.md"
|