101 lines
2.3 KiB
Bash
Executable File
101 lines
2.3 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
_help() {
|
|
echo 'Convert a folder structure of .md files to a HTML site.
|
|
Requires pandoc, rsync, python
|
|
|
|
Commands:
|
|
autobuild source-folder target-folder (builds at change of content)
|
|
build source-folder target-folder
|
|
serve [target-folder] [port] (defaults to . and 8000)
|
|
|
|
Note: Files in target-folder will be deleted!
|
|
'
|
|
exit
|
|
}
|
|
|
|
_serve() {
|
|
root="$1"
|
|
[[ -z "$1" ]] && root="."
|
|
port="$2"
|
|
[[ -z "$2" ]] && port="8000"
|
|
timeout 7200 "$SELF"/../web/webserver.py -a 127.0.0.1 -p $port "$root"
|
|
}
|
|
|
|
_autobuild() {
|
|
_build "$1" "$2"
|
|
old_hash=$( find "$1" -type f -exec cat \{\} \; | md5sum | awk '{ print $1 }' )
|
|
while true; do
|
|
sleep 3
|
|
new_hash=$( find "$1" -type f -exec cat \{\} \; | md5sum | awk '{ print $1 }' )
|
|
[[ "$old_hash" == "$new_hash" ]] && continue
|
|
old_hash="$new_hash"
|
|
_build "$1" "$2"
|
|
done
|
|
}
|
|
|
|
_build() {
|
|
[[ -z "$2" ]] && _help
|
|
[[ "$1" -ef "$2" ]] && {
|
|
echo source and target folders are the same. Cannot continue.
|
|
exit 1
|
|
}
|
|
[[ -d "$1" ]] || {
|
|
echo source is not a directory
|
|
exit 1
|
|
}
|
|
[[ -f "$2" ]] && {
|
|
echo target is a file
|
|
exit 1
|
|
}
|
|
if [[ -d "$2" ]] && [[ ! -f "$2"/.mdsite ]]; then
|
|
echo Target folder not an mdsite folder, delete first
|
|
exit 1
|
|
fi
|
|
rsync -r "$1"/ "$2"
|
|
touch "$2"/.mdsite
|
|
echo "----"
|
|
shopt -s nullglob
|
|
trap _error_in_recursion 1 6 9 15
|
|
_recursive_gen "$2" root $( basename "$1" )
|
|
}
|
|
|
|
_recursive_gen() {
|
|
pushd "$1" &> /dev/null
|
|
parents=""
|
|
[[ "$2" = "root" ]] && parents="-p"
|
|
title=${3:-$( basename "$1" )}
|
|
rm -f index.html
|
|
for md in *.md; do
|
|
[[ -d "$md" ]] && continue
|
|
pandoc -s -f markdown -H "$SELF"/md-site.css -o "${md%.md}.html" "$md" && { rm "$md"; } || _error_in_recursion "$md"
|
|
echo Generated $1/$md
|
|
done
|
|
[[ -f "index.html" ]] || { SimpleWebPage -f $parents -t "$title"; echo Generated $1/index.html; }
|
|
for dir in *; do
|
|
[[ -d "$dir" ]] && {
|
|
_recursive_gen "$dir"
|
|
}
|
|
done
|
|
popd &> /dev/null
|
|
}
|
|
|
|
_error_in_recursion() {
|
|
echo Error prosessing folder
|
|
pwd
|
|
[[ -n "$1" ]] && echo File: "$1"
|
|
exit 1
|
|
}
|
|
|
|
[[ -z "$1" ]] && _help
|
|
for (( i=1; i<=$#; i++ )); do
|
|
[[ "${!i}" = "-h" ]] && _help
|
|
[[ "${!i}" = *"help" ]] && _help
|
|
done
|
|
SELF=$( dirname $( abs-path "$0" ) )
|
|
|
|
[[ "$1" == build ]] && _build "$2" "$3"
|
|
[[ "$1" == autobuild ]] && _autobuild "$2" "$3"
|
|
[[ "$1" == serve ]] && _serve "$2" "$3"
|
|
|