From 3728cb7b440e8fc9858c4f67475fbbc54be30dea Mon Sep 17 00:00:00 2001 From: nuex Date: Tue, 4 Oct 2011 17:34:34 -0400 Subject: [PATCH] plain html page template support --- README.md | 7 +++++-- bin/zod | 15 ++++++++++----- bin/zod.template | 15 ++++++++++----- lib/render.awk | 23 +++++++++++++++-------- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 793d860..6d08ef7 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,11 @@ Page metadata will always override global metadata of the same key. ### Templates -Templates come in two forms: markdown files with an `.md` extension or layout files with a `.layout` extension. Metadata can be bound to templates by using the `{{key}}` notation in your markdown and layout files. A `main.layout` file could look something like this: +Templates come in two forms, page templates and layout templates. Metadata can be bound to templates by using the `{{key}}` notation in your markdown and layout files. + +Page templates can be either markdown files with an `.md` extension or plain HTML files with a `.html` extension. + +The `main.layout` file wraps HTML content around a page template. A `main.layout` file could look something like this: @@ -97,7 +101,6 @@ Just be sure to set the data array in the load_helpers() function at the top of ## FUTURE -- HTML content pages (i.e. no filtering) - multiple filters support - multiple layout support - mustache support diff --git a/bin/zod b/bin/zod index 58e55b5..3e60043 100755 --- a/bin/zod +++ b/bin/zod @@ -56,11 +56,16 @@ for f in $files; do targetpath=$target fi - if [ ! -z "$(find $f -name *.md)" ]; then - page_name=$(basename $f | sed 's/\.md//') + 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//') - if [ -e $dir/$page_name.meta ]; then - page_meta_opts=$dir/$page_name.meta + # 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 + + if [ -e $dir/$file_name.meta ]; then + page_meta_opts=$dir/$file_name.meta fi awk -f $ZODLIB/render.awk \ @@ -69,7 +74,7 @@ for f in $files; do $global_meta_opts \ $page_meta_opts \ $f \ - $layout_opts > $targetpath/$page_name.html + $layout_opts > $targetpath/$file_name.html else cp $f $targetpath # Copying a non-template file diff --git a/bin/zod.template b/bin/zod.template index 4837859..5406929 100755 --- a/bin/zod.template +++ b/bin/zod.template @@ -56,11 +56,16 @@ for f in $files; do targetpath=$target fi - if [ ! -z "$(find $f -name *.md)" ]; then - page_name=$(basename $f | sed 's/\.md//') + 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//') - if [ -e $dir/$page_name.meta ]; then - page_meta_opts=$dir/$page_name.meta + # 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 + + if [ -e $dir/$file_name.meta ]; then + page_meta_opts=$dir/$file_name.meta fi awk -f $ZODLIB/render.awk \ @@ -69,7 +74,7 @@ for f in $files; do $global_meta_opts \ $page_meta_opts \ $f \ - $layout_opts > $targetpath/$page_name.html + $layout_opts > $targetpath/$file_name.html else cp $f $targetpath # Copying a non-template file diff --git a/lib/render.awk b/lib/render.awk index 2afac38..8bc417e 100644 --- a/lib/render.awk +++ b/lib/render.awk @@ -5,10 +5,22 @@ BEGIN { helpers_loaded = "no" content = "" layout = "" + filter = "html" } { - action = action_from_filetype(FILENAME) + 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" + } } # Process lines from meta files @@ -58,12 +70,6 @@ END { } } -function action_from_filetype(filename) { - if (match(filename, /\.meta/)) return "meta" - if (match(filename, /\.layout/)) return "layout" - if (match(filename, /\.md/)) return "page" -} - function bind_data(txt, tag, key) { if (match(txt, /{{([^}]*)}}/)) { tag = substr(txt, RSTART, RLENGTH) @@ -77,7 +83,8 @@ function bind_data(txt, tag, key) { } function render_content(txt) { - return markdown(txt) + if (filter == "html") return txt + if (filter == "markdown") return markdown(txt) } function markdown(txt, rand_date, tmpfile, rendered_txt, date_cmd, markdown_cmd, line) {