zodiac/lib/render.awk

138 lines
2.7 KiB
Awk
Raw Normal View History

#
# Render a zodiac page
#
2011-09-22 17:28:24 +02:00
BEGIN {
action = "none"
2011-10-08 02:28:42 +02:00
ext = "none"
main = "_main"
2011-09-22 17:28:24 +02:00
helpers_loaded = "no"
layout = ""
}
{
2011-10-04 23:34:34 +02:00
split(FILENAME, parts, ".")
ext = parts[length(parts)]
if (FILENAME == "-") {
2011-10-08 02:28:42 +02:00
action = "config"
} else if (ext == "meta") {
action = "meta"
} else if (ext == "layout") {
action = "layout"
} else {
action = "page"
filter_ext = ext
2011-10-08 02:28:42 +02:00
}
}
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" && action != "config" && helpers_loaded == "no" && helpers == "yes" {
2011-09-22 17:28:24 +02:00
load_helpers()
helpers_loaded = "yes"
}
# Process lines from the page
action == "page" {
if (!contents[main]) {
contents[main] = bind_data($0)
2011-10-08 02:28:42 +02:00
# save the extension for this content type
# to find the appropriate filter to render it
filter_exts[main] = ext
2011-09-22 17:28:24 +02:00
} else {
contents[main] = contents[main] "\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}}}/)) {
sub(/{{{yield}}}/, escape_special_chars(render_content(main)))
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 {
print render_content(main)
2011-09-22 17:28:24 +02:00
}
}
function bind_data(txt, tag, key) {
if (match(txt, /{{([^}]*)}}/)) {
tag = substr(txt, RSTART, RLENGTH)
match(tag, /([[:alnum:]_]|[?]).*[^}]/)
2011-09-22 17:28:24 +02:00
key = substr(tag, RSTART, RLENGTH)
gsub(tag, data[key], txt)
return bind_data(txt, data)
} else {
return txt
}
}
function render_content(type, ext_key, filter_ext, filter_cmd, txt) {
2011-10-08 02:28:42 +02:00
ext_key = type "_ext"
# Get the extension of the content type
filter_ext = filter_exts[type]
2011-10-08 02:28:42 +02:00
# Get the appropriate filter command for this extension
filter_cmd = filter[filter_ext]
2011-10-08 02:28:42 +02:00
# 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-24 16:37:08 +02:00
function run_filter(cmd, txt, rand_date, tmpfile, rendered_txt, date_cmd, filter_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-24 16:37:08 +02:00
# pipe content to filter command
2011-10-08 02:28:42 +02:00
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
}
2011-12-02 16:28:23 +01:00
# Prevent awk from replacing ampersands with matched text
function escape_special_chars( txt) {
gsub(/&/, "\\\\&", txt)
return txt
}