custom filter support

This commit is contained in:
nuex 2011-10-07 20:28:42 -04:00
parent bf6480b08c
commit 0ab0d57d39
2 changed files with 64 additions and 19 deletions

View File

@ -10,6 +10,7 @@ f="$4"
markdown_filter_cmd="awk -f $zod_lib/markdown.awk" markdown_filter_cmd="awk -f $zod_lib/markdown.awk"
[ -f "$proj/helpers.awk" ] && helper_opts="$proj/helpers.awk" [ -f "$proj/helpers.awk" ] && helper_opts="$proj/helpers.awk"
[ -f "$proj/filters.config" ] && filter_opts="$proj/filters.config"
[ -f "$proj/global.meta" ] && global_meta_opts="$proj/global.meta" [ -f "$proj/global.meta" ] && global_meta_opts="$proj/global.meta"
[ -f "$proj/main.layout" ] && layout_opts="$proj/main.layout" [ -f "$proj/main.layout" ] && layout_opts="$proj/main.layout"
@ -34,6 +35,7 @@ if [ $ext == "md" ] || [ $ext == "html" ]; then
set -- -f "$zod_lib/render.awk" set -- -f "$zod_lib/render.awk"
[ "$helper_opts" ] && set -- "$@" -f "$helper_opts" [ "$helper_opts" ] && set -- "$@" -f "$helper_opts"
set -- "$@" -v markdown_filter_cmd="$markdown_filter_cmd" \ set -- "$@" -v markdown_filter_cmd="$markdown_filter_cmd" \
"$filter_opts" \
"$global_meta_opts" \ "$global_meta_opts" \
"$page_meta_opts" \ "$page_meta_opts" \
"$f" \ "$f" \

View File

@ -2,19 +2,44 @@
BEGIN { BEGIN {
action = "none" action = "none"
ext = "none"
main_content_type = "_main"
helpers_loaded = "no" helpers_loaded = "no"
content = ""
layout = "" layout = ""
filter = "html" filter_cmds["htm"] = "none"
filter_cmds["html"] = "none"
filter_cmds["md"] = markdown_filter_cmd
} }
{ {
split(FILENAME, parts, ".") split(FILENAME, parts, ".")
ext = parts[length(parts)] ext = parts[length(parts)]
if (ext == "meta") { action = "meta" } if (ext == "config") {
if (ext == "layout") { action = "layout" } action = "config"
if (ext == "md") { action = "page"; filter = "markdown" } } else if (ext == "meta") {
if (ext == "html") { action = "page"; filter = "html" } action = "meta"
} else if (ext == "layout") {
action = "layout"
} else {
# not a known extension, assuming this line
# is from a page
content_extension = ext
action = "page"
}
}
# Process lines from config
# Also ignore comments and empty lines
action == "config" && (NF > 0) && (!/^;.*/) {
split($0, filter_kv, ": ")
split(filter_kv[1], filter_extensions, ",")
filter_cmd = filter_kv[2]
for (i = 1; i <= length(filter_extensions); i++) {
filter_cmds[filter_extensions[i]] = filter_cmd
}
next
} }
# Process lines from meta files # Process lines from meta files
@ -33,10 +58,14 @@ action != "meta" && helpers_loaded == "no" && helpers == "yes" {
# Process lines from the page # Process lines from the page
action == "page" { action == "page" {
if (content == "") { if (!contents[main_content_type]) {
content = bind_data($0) contents[main_content_type] = bind_data($0)
# save the extension for this content type
# to find the appropriate filter to render it
content_extensions[main_content_type] = ext
} else { } else {
content = content "\n" bind_data($0) contents[main_content_type] = contents[main_content_type] "\n" bind_data($0)
} }
next next
} }
@ -46,7 +75,7 @@ action == "layout" {
# replace yield with rendered content # replace yield with rendered content
if (match($0, /{{{yield}}}/)) { if (match($0, /{{{yield}}}/)) {
sub(/{{{yield}}}/, render_content(content)) sub(/{{{yield}}}/, render_content(main_content_type))
} }
if (layout == "") { if (layout == "") {
@ -60,7 +89,7 @@ END {
if (layout != "") { if (layout != "") {
print layout print layout
} else { } else {
print render_content(content) print render_content(main_content_type)
} }
} }
@ -76,22 +105,36 @@ function bind_data(txt, tag, key) {
} }
} }
function render_content(txt) { function render_content(type, ext_key, content_extension, filter_cmd, txt) {
if (filter == "html") return txt ext_key = type "_ext"
if (filter == "markdown") return markdown(txt)
# Get the extension of the content type
content_extension = content_extensions[type]
# Get the appropriate filter command for this extension
filter_cmd = filter_cmds[content_extension]
# Get the text of the content for the given type
txt = contents[type]
if (filter_cmd != "none") {
return run_filter(filter_cmd, txt)
} else {
return txt
}
} }
function markdown(txt, rand_date, tmpfile, rendered_txt, date_cmd, markdown_cmd, line) { function run_filter(cmd, txt, rand_date, tmpfile, rendered_txt, date_cmd, markdown_cmd, line) {
date_cmd = "date +%Y%m%d%H%M%S" date_cmd = "date +%Y%m%d%H%M%S"
date_cmd | getline rand_date date_cmd | getline rand_date
close(date_cmd) close(date_cmd)
tmpfile = "/tmp/awk_render" rand_date tmpfile = "/tmp/awk_render" rand_date
markdown_cmd = markdown_filter_cmd " > " tmpfile filter_cmd = cmd " > " tmpfile
# pipe content to markdown.awk # pipe content to filter.awk
print txt | markdown_cmd print txt | filter_cmd
close(markdown_cmd) close(filter_cmd)
# pull out the filtered page # pull out the filtered page
while((getline line < tmpfile) > 0) { while((getline line < tmpfile) > 0) {