gronk/notes2web

138 lines
3.3 KiB
Plaintext
Raw Normal View History

2021-04-30 20:55:13 +00:00
#!/usr/bin/env bash
2021-05-01 11:08:21 +00:00
# set default config values, load user config, export config variables
name=""
2021-05-01 12:37:23 +00:00
output="web"
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
2021-05-01 12:37:23 +00:00
export output
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 12:37:57 +00:00
[[ "$1" == "--help" ]] && echo "USAGE: $0 [NOTES_DIRECTORY_1 [NOTES_DIRECTORY_2 [...]]]" && exit 0
[[ "$1" == "--clean" ]] && {
find -name ".2web" -exec rm {} \;
rm -rf "$output"
exit 0
}
mkdir -p "$output"
2021-05-01 10:29:48 +00:00
function _renderarticle {
2021-04-30 20:55:13 +00:00
echo "rendering $1"
2021-05-01 12:37:23 +00:00
mkdir -p "$(dirname "$output/${1}.html")"
2021-04-30 20:55:13 +00:00
pandoc\
--toc\
--standalone\
-t html\
--template "$article_template"\
2021-05-01 12:37:23 +00:00
-o "$output/${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"
2021-05-01 12:37:23 +00:00
mkdir -p "$(dirname "$output/${1}.html")"
2021-05-01 12:02:35 +00:00
sed -e "s#\\\$title\\\$#$1#" "$textarticlehead_template"\
2021-05-01 12:37:23 +00:00
> "$output/${1}.html"
cat "$1" >> "$output/${1}.html"
cat "$textarticlefoot_template" >> "$output/${1}.html"
2021-05-01 12:02:35 +00:00
}
function _adddirtoindex {
dir="$(dirname "$1")"
2021-05-01 12:37:23 +00:00
echo "<h2>$(basename "$dir") notes</h2>" >> $output/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"\
2021-05-01 12:37:23 +00:00
>> $output/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"\
2021-05-01 12:37:23 +00:00
>> $output/index.md
2021-04-30 20:55:13 +00:00
}
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 12:37:23 +00:00
echo "---" > $output/index.md
[[ -z "$name" ]] && echo "title: notes" >> $output/index.md || echo "title: ${name}'s notes" >> $output/index.md
echo "---" >> $output/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"
2021-05-01 12:37:23 +00:00
cp "$stylesheet" "$output/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 12:37:23 +00:00
-o "$output/index.html"\
"$output/index.md"