pseudorandom/ssb

107 lines
2.1 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
function usage
{
echo \
"Usage: $0 [-d|-h|-r] [-e HEADER_PATH] [-f FOOTER_PATH]" \
"[-m MD_RENDERER] [-o OUTPUT_DIR] [-p POSTS_DIR]"
}
function parse_optargs
{
while getopts "dhre:f:m:o:p:" opt; do
case $opt in
d)
DISABLE_POSTS=true;;
h)
usage
exit 0;;
r)
RECURSE_POSTS=true;;
e)
HEADER_PATH=$OPTARG;;
f)
FOOTER_PATH=$OPTARG;;
m)
MD_RENDERER=$OPTARG;;
o)
OUTPUT_DIR=$OPTARG;;
p)
POSTS_DIR=$OPTARG;;
esac
done
}
function set_default_args
{
FOOTER_PATH=./footer.html
HEADER_PATH=./header.html
MD_RENDERER=pandoc
DISABLE_POSTS=false
POSTS_DIR=./posts
OUTPUT_DIR=.
}
function get_posts
{
if [ "$RECURSE_POSTS" = false ]; then
DEPTH_LIMITER="-maxdepth 1"
fi
find $POSTS_DIR $DEPTH_LIMITER -type f -name "*.md" -printf "%T@\t%p\n" | sort -nr | cut -c23-
}
function get_mod_date
{
case `uname` in
Linux)
stat -c %y "$1" | cut -d ' ' -f 1;;
Darwin|*BSD)
stat -t "%Y-%m-%d" -f "%Sm" "$1";;
esac
}
function append_posts_list
{
posts_list="</article><hr/><h2>Recent Articles</h2>"
for post in $@; do
file_base=`basename $post .md`
post_title=`grep -m 1 "^# .*" $post | cut -c 3-`
date=`get_mod_date "$post"`
post_link="<em>$date</em> [$post_title]($file_base.html)<br>\n"
posts_list="$posts_list$post_link"
done
echo $posts_list | sort -r
}
function make_html_files
{
for md_file in $@; do
file_base=`basename $md_file .md`
output_file="$OUTPUT_DIR/$file_base.html"
post_title=`grep -m 1 "^# .*" $md_file | cut -c 3-`
description=`head -3 $md_file | tail -1 | rev | cut -c 5- | rev`
append_posts_list $posts | cat $md_file - | $MD_RENDERER > $output_file
cat $HEADER_PATH $output_file $FOOTER_PATH | sed -e "s/\$TITLE/$post_title/g" | sed -e "s/\$LINK/$file_base/g" | sed -e "s/\$DESCRIPTION/$description/g" | sed -e "s/@@^/<em class=\"footnotelabel\"><\/em>/g" | tee $output_file
done
}
set_default_args
parse_optargs $@
shift `expr $OPTIND - 1`
pages="$@"
[ -z "$pages" ] && echo No markdown pages given to render && exit 1
[ $DISABLE_POSTS != true ] && posts=`get_posts`
md_files="$pages $posts"
make_html_files $md_files