gronk/notes2web

125 lines
3.0 KiB
Plaintext
Raw Normal View History

2021-04-30 20:55:13 +00:00
#!/usr/bin/env bash
[[ "$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
# set default config values, load user config, export config variables
name=""
article_template="/opt/notes2web/templates/article.html"
2021-05-01 12:02:35 +00:00
textarticlehead_template="/opt/notes2web/templates/textarticlehead.html"
textarticlefoot_template="/opt/notes2web/templates/textarticlefoot.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
export name
export article_template
2021-05-01 12:02:35 +00:00
export textarticlehead_template
export textarticlefoot_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\
--template "$article_template"\
2021-05-01 12:02:35 +00:00
-o "${1}.html"\
2021-04-30 20:55:13 +00:00
"$1"\
--mathjax
}
2021-05-01 11:08:21 +00:00
2021-05-01 12:02:35 +00:00
function _rendertextarticle {
[[ "$(file -b "$1")" == "ASCII text" ]] || exit
echo "rendering text file $1"
sed -e "s#\\\$title\\\$#$1#" "$textarticlehead_template"\
> "${1}.html"
cat "$1" >> "${1}.html"
cat "$textarticlefoot_template" >> "${1}.html"
}
function _adddirtoindex {
dir="$(dirname "$1")"
2021-05-01 10:48:41 +00:00
echo "<h2>$(basename "$dir") notes</h2>" >> index.md
find "$dir" -name '*.md' -exec bash -c "_addarticletoindex '{}'" \;
2021-05-01 12:02:35 +00:00
find "$dir" -not -path '**/.git/**' -not -name '*.md' -type f -exec bash -c "_addtextarticletoindex '{}'" \;
}
function _addtextarticletoindex {
[[ "$(file -b "$1")" == "ASCII text" ]] || exit
pandoc\
-t html\
-V "filepath=${1}.html"\
-V "title=$1"\
--template "$listitem_template"\
"$1"\
>> index.md
}
2021-05-01 11:08:21 +00:00
function _addarticletoindex {
2021-04-30 20:55:13 +00:00
echo "adding $1 to list of notes"
pandoc\
-t html\
2021-05-01 12:02:35 +00:00
-V "filepath=${1}.html"\
--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 12:02:35 +00:00
export -f _rendertextarticle
export -f _adddirtoindex
export -f _addarticletoindex
2021-05-01 12:02:35 +00:00
export -f _addtextarticletoindex
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 12:02:35 +00:00
for dir in "$@"
2021-04-30 20:55:13 +00:00
do
2021-05-01 12:02:35 +00:00
find "$dir" -name '*.md' -exec bash -c "_renderarticle '{}'" \;
find "$dir" -not -path '**/.git/**' -not -name '*.md' -type f -exec bash -c "_rendertextarticle '{}'" \;
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
for file in "$@"
2021-04-30 20:55:13 +00:00
do
[[ ! -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
find -name '.2web' -exec bash -c "_adddirtoindex '{}'" \;
2021-05-01 11:08:21 +00:00
echo "copying styles.css to current directory"
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\
--template "$index_template"\
2021-05-01 10:29:48 +00:00
-o index.html\
"index.md"