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

View File

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