zodiac/lib/render.awk

149 lines
3.2 KiB
Awk
Raw Normal View History

2011-09-22 17:28:24 +02:00
# render.awk - Awk-based templating
BEGIN {
action = "none"
2011-10-08 02:28:42 +02:00
ext = "none"
main_content_type = "_main"
2011-09-22 17:28:24 +02:00
helpers_loaded = "no"
layout = ""
2011-10-08 02:28:42 +02:00
filter_cmds["htm"] = "none"
filter_cmds["html"] = "none"
filter_cmds["md"] = markdown_filter_cmd
2011-09-22 17:28:24 +02:00
}
{
2011-10-04 23:34:34 +02:00
split(FILENAME, parts, ".")
ext = parts[length(parts)]
2011-10-08 02:28:42 +02:00
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
2011-09-22 17:28:24 +02:00
}
# Process lines from meta files
action == "meta" {
split($0, kv, ": ")
data[kv[1]] = kv[2]
next
}
# Done processing meta
# Since data is loaded now, load the helpers
action != "meta" && helpers_loaded == "no" && helpers == "yes" {
load_helpers()
helpers_loaded = "yes"
}
# Process lines from the page
action == "page" {
2011-10-08 02:28:42 +02:00
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
2011-09-22 17:28:24 +02:00
} else {
2011-10-08 02:28:42 +02:00
contents[main_content_type] = contents[main_content_type] "\n" bind_data($0)
2011-09-22 17:28:24 +02:00
}
next
}
# Process lines from the layout
action == "layout" {
# replace yield with rendered content
if (match($0, /{{{yield}}}/)) {
2011-10-08 02:28:42 +02:00
sub(/{{{yield}}}/, render_content(main_content_type))
2011-09-22 17:28:24 +02:00
}
if (layout == "") {
layout = bind_data($0)
} else {
layout = layout "\n" bind_data($0)
}
}
END {
if (layout != "") {
print layout
} else {
2011-10-08 02:28:42 +02:00
print render_content(main_content_type)
2011-09-22 17:28:24 +02:00
}
}
function bind_data(txt, tag, key) {
if (match(txt, /{{([^}]*)}}/)) {
tag = substr(txt, RSTART, RLENGTH)
match(tag, /(\w|[?]).*[^}]/)
key = substr(tag, RSTART, RLENGTH)
gsub(tag, data[key], txt)
return bind_data(txt, data)
} else {
return txt
}
}
2011-10-08 02:28:42 +02:00
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
}
2011-09-22 17:28:24 +02:00
}
2011-10-08 02:28:42 +02:00
function run_filter(cmd, txt, rand_date, tmpfile, rendered_txt, date_cmd, markdown_cmd, line) {
2011-09-22 17:28:24 +02:00
date_cmd = "date +%Y%m%d%H%M%S"
date_cmd | getline rand_date
close(date_cmd)
tmpfile = "/tmp/awk_render" rand_date
2011-10-08 02:28:42 +02:00
filter_cmd = cmd " > " tmpfile
2011-09-22 17:28:24 +02:00
2011-10-08 02:28:42 +02:00
# pipe content to filter.awk
print txt | filter_cmd
close(filter_cmd)
2011-09-22 17:28:24 +02:00
# pull out the filtered page
while((getline line < tmpfile) > 0) {
rendered_txt = rendered_txt "\n" line
}
close(tmpfile)
system("rm " tmpfile)
return rendered_txt
}