zodiac/bin/zod
2011-10-04 15:40:45 -04:00

78 lines
1.7 KiB
Bash
Executable File

#!/bin/sh
# ZODIAC - a simple static site generator
# Copyright (c) 2011 Chase Allen James <nx-zodiac@nu-ex.com>
ZODLIB="$( cd "$( dirname "$0" )" && pwd )/../lib"
proj=$1
target=$2
if [ -z "$1" ] || [ -z "$2" ]; then
echo "usage: zod projectdir targetdir"
exit
fi
if [ ! -e $proj ]; then
echo "error: project directory does not exist"
exit
fi
if [ ! -e $target ]; then
echo "error: target directory does not exist"
exit
fi
helpers=$proj/helpers.awk
if [ -e $helpers ]; then
helper_opts="-f `pwd`/$helpers"
fi
global_meta=$proj/global.meta
if [ -e $global_meta ]; then
global_meta_opts=$global_meta
fi
layout=$proj/main.layout
if [ -e $layout ]; then
layout_opts=$layout
fi
files=$(find $proj -type f ! -name "*.layout" ! -name "*.meta" ! -name "*.awk")
for f in $files; do
dir=$(dirname $f)
# Find the target directory if one must be created
# Start by pulling the project directory out of the filepath
# with awk and then removing the filename with sed
targetdir=$(awk "BEGIN { split(\"$f\",a,\"$proj/\"); print a[2] }" | sed "s/`basename $f`//")
targetpath=$target/$targetdir
if [ ! -z "$targetdir" ]; then
mkdir -p $target/$targetdir
else
# There is no directory to create in target,
# i.e. file is in the root of the project
targetpath=$target
fi
if [ ! -z "$(find $f -name *.md)" ]; then
page_name=$(basename $f | sed 's/\.md//')
if [ -e $dir/$page_name.meta ]; then
page_meta_opts=$dir/$page_name.meta
fi
awk -f $ZODLIB/render.awk \
-v AWKLIB="$ZODLIB" \
$helper_opts \
$global_meta_opts \
$page_meta_opts \
$f \
$layout_opts > $targetpath/$page_name.html
else
cp $f $targetpath # Copying a non-template file
fi
done