partials support and documentation

This commit is contained in:
nuex
2013-11-15 04:18:29 -05:00
parent 45b6f41120
commit b53401fe4b
3 changed files with 92 additions and 4 deletions

View File

@@ -77,17 +77,66 @@ END {
}
function bind_data(txt, tag, key) {
if (match(txt, /{{([^}]*)}}/)) {
if (match(txt, /{{> ([^}]*)}}/)) {
tag = substr(txt, RSTART, RLENGTH)
match(tag, /([[:alnum:]_]|[?]).*[^}]/)
key = substr(tag, RSTART, RLENGTH)
gsub(tag, data[key], txt)
return bind_data(txt, data)
partial_txt = load_partial(key)
gsub(tag, escape_special_chars(partial_txt), txt)
return bind_data(txt)
} else if (match(txt, /{{([^}]*)}}/)) {
tag = substr(txt, RSTART, RLENGTH)
match(tag, /([[:alnum:]_]|[?]).*[^}]/)
key = substr(tag, RSTART, RLENGTH)
gsub(tag, escape_special_chars(data[key]), txt)
return bind_data(txt)
} else {
return txt
}
}
# Returns the text from a partial
#
# It will load the partial from the cache if possible.
# Otherwise it will open the partial file and load each
# line.
#
# Nothing is returned if the file doesn't exist.
function load_partial(key, partial, partial_file, line) {
partial = partials[key]
if (partial) {
return partial
} else {
pwd = ENVIRON["PWD"]
partial_file = pwd "/" proj "/" key ".partial"
if (is_file(partial_file)) {
while((getline line < partial_file) > 0) {
if (partial_txt) {
partial_txt = partial_txt "\n" line
} else {
partial_txt = line
}
}
close(partial_file)
partials[key] = partial_txt
return partial_txt
}
}
}
# Check if a file exists
function is_file(file) {
check = "[ -f " file " ] && echo yes"
check | getline response
close(check)
if (response == "yes") {
return "yes"
}
}
function render_content(type, ext_key, filter_ext, filter_cmd, txt) {
ext_key = type "_ext"