* use parameter expansion for path munging

* properly quote variables
* consistent command susbtitution
* condense conditionals to one line
* better error handling

thanks to zsw for teaching me some sh
This commit is contained in:
nuex 2011-10-06 12:27:48 -04:00
parent 545695c410
commit 2b78299184
2 changed files with 44 additions and 61 deletions

87
bin/zod.template Executable file → Normal file
View File

@ -3,80 +3,69 @@
# ZODIAC - a simple static site generator
# Copyright (c) 2011 Chase Allen James <nx-zodiac@nu-ex.com>
ZODLIB=ZODLIB_PATH
zod_lib=ZODLIB_PATH
markdown_filter_cmd="awk -f $zod_lib/markdown.awk"
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
_zod_error() {
echo -e ">>> ERROR: $*" >&2
exit 1
}
[ ! "$1" ] || [ ! "$2" ] && { echo "usage: zod projectdir targetdir"; exit; }
[ ! -d $proj ] && _zod_error "project directory does not exist"
[ ! -d $target ] && _zod_error "target directory does not exist"
helpers=$proj/helpers.awk
if [ -e $helpers ]; then
helper_opts="-f `pwd`/$helpers"
fi
[ -f $helpers ] && helper_opts="-f $(pwd)/$helpers"
global_meta=$proj/global.meta
if [ -e $global_meta ]; then
global_meta_opts=$global_meta
fi
[ -f $global_meta ] && global_meta_opts=$global_meta
layout=$proj/main.layout
if [ -e $layout ]; then
layout_opts=$layout
fi
[ -f $layout ] && layout_opts=$layout
files=$(find $proj -type f \
! -name "*.layout" \
! -name "*.meta" \
! -name "helpers.awk")
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
subdir=${f%/*}
subdir=${subdir#$proj}
if [ "$subdir" ]; then
destination="$target$subdir"
mkdir -p "$destination"
else
# There is no directory to create in target,
# i.e. file is in the root of the project
targetpath=$target
destination=$target
fi
file_basename=$(basename $f)
file_extension=$(echo $file_basename | awk -F . '{print $NF}')
file_name=$(echo $file_basename | sed -e 's/\.md//' -e 's/\.html//')
ext=${f##*.}
if [ $ext == "md" ] || [ $ext == "html" ]; then
# If the file's extension wasn't replaced (i.e. same as basename) then
# we know it isn't a template.
if [ $file_name != $file_basename ]; then
meta=${f%.$ext}.meta
[ -f $meta ] && page_meta_opts=$meta
if [ -e $dir/$file_name.meta ]; then
page_meta_opts=$dir/$file_name.meta
fi
[ $ext == "md" ] && filter_opts=""
awk -f $ZODLIB/render.awk \
-v AWKLIB="$ZODLIB" \
page=${f##*/}
page=${page%.$ext}.html
awk -f $zod_lib/render.awk \
-v markdown_filter_cmd="$markdown_filter_cmd" \
$helper_opts \
$global_meta_opts \
$page_meta_opts \
$f \
$layout_opts > $targetpath/$file_name.html
"$global_meta_opts" \
"$page_meta_opts" \
"$f" \
"$layout_opts" > "$destination/$page"
else
cp $f $targetpath # Copying a non-template file
cp "$f" "$destination" # Copying a non-template file
fi
done

View File

@ -11,16 +11,10 @@ BEGIN {
{
split(FILENAME, parts, ".")
ext = parts[length(parts)]
if (ext == "meta") action = "meta"
if (ext == "layout") action = "layout"
if (ext == "md") {
action = "page"
filter = "markdown"
}
if (ext == "html") {
action = "page"
filter = "html"
}
if (ext == "meta") { action = "meta" }
if (ext == "layout") { action = "layout" }
if (ext == "md") { action = "page"; filter = "markdown" }
if (ext == "html") { action = "page"; filter = "html" }
}
# Process lines from meta files
@ -92,8 +86,8 @@ function markdown(txt, rand_date, tmpfile, rendered_txt, date_cmd, markdown_c
date_cmd | getline rand_date
close(date_cmd)
tmpfile = "/tmp/render" rand_date
markdown_cmd = "awk -f " AWKLIB "/markdown.awk > " tmpfile
tmpfile = "/tmp/awk_render" rand_date
markdown_cmd = markdown_filter_cmd " > " tmpfile
# pipe content to markdown.awk
print txt | markdown_cmd