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"
[ -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/main.layout" ] && layout_opts="$proj/main.layout"
@ -34,6 +35,7 @@ if [ $ext == "md" ] || [ $ext == "html" ]; then
set -- -f "$zod_lib/render.awk"
[ "$helper_opts" ] && set -- "$@" -f "$helper_opts"
set -- "$@" -v markdown_filter_cmd="$markdown_filter_cmd" \
"$filter_opts" \
"$global_meta_opts" \
"$page_meta_opts" \
"$f" \

View File

@ -2,19 +2,44 @@
BEGIN {
action = "none"
ext = "none"
main_content_type = "_main"
helpers_loaded = "no"
content = ""
layout = ""
filter = "html"
filter_cmds["htm"] = "none"
filter_cmds["html"] = "none"
filter_cmds["md"] = markdown_filter_cmd
}
{
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 == "config") {
action = "config"
} else if (ext == "meta") {
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
@ -33,10 +58,14 @@ action != "meta" && helpers_loaded == "no" && helpers == "yes" {
# Process lines from the page
action == "page" {
if (content == "") {
content = bind_data($0)
if (!contents[main_content_type]) {
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 {
content = content "\n" bind_data($0)
contents[main_content_type] = contents[main_content_type] "\n" bind_data($0)
}
next
}
@ -46,7 +75,7 @@ action == "layout" {
# replace yield with rendered content
if (match($0, /{{{yield}}}/)) {
sub(/{{{yield}}}/, render_content(content))
sub(/{{{yield}}}/, render_content(main_content_type))
}
if (layout == "") {
@ -60,7 +89,7 @@ END {
if (layout != "") {
print layout
} 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) {
if (filter == "html") return txt
if (filter == "markdown") return markdown(txt)
function render_content(type, ext_key, content_extension, filter_cmd, txt) {
ext_key = type "_ext"
# 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 | getline rand_date
close(date_cmd)
tmpfile = "/tmp/awk_render" rand_date
markdown_cmd = markdown_filter_cmd " > " tmpfile
filter_cmd = cmd " > " tmpfile
# pipe content to markdown.awk
print txt | markdown_cmd
close(markdown_cmd)
# pipe content to filter.awk
print txt | filter_cmd
close(filter_cmd)
# pull out the filtered page
while((getline line < tmpfile) > 0) {