From c30a36916e5f37aa97d581cf31565103f804b3eb Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 27 Jan 2011 22:10:51 +0000 Subject: initial import from svn.buetow.org/repos/awksite --- awksite.conf | 14 +++++++++++ index.cgi | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ some-text.txt | 6 +++++ template.html | 20 ++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 awksite.conf create mode 100755 index.cgi create mode 100644 some-text.txt create mode 100644 template.html diff --git a/awksite.conf b/awksite.conf new file mode 100644 index 0000000..f35e3d9 --- /dev/null +++ b/awksite.conf @@ -0,0 +1,14 @@ +# AWK Site configuration file +# +# AWK Site is Copyright by Paul C. Buetow (2005, 2006) +# http://awksite.buetow.org +# +# The syntax is very easy: Use.. +# key=value to make %%key%% being replaced by value in the template +# key=!command to make %%key%% being replaced by the output of the sh command + +template=template.html +title=Title of the website +subtitle=Subtitle of the website +date=!date +uptime=!uptime diff --git a/index.cgi b/index.cgi new file mode 100755 index 0000000..3e4f034 --- /dev/null +++ b/index.cgi @@ -0,0 +1,76 @@ +#!/usr/bin/awk -f +# AWK CGI website main program file +# +# AWK CGI is Copyright by Paul C. Buetow (2005) +# http://buetow.org +# + +BEGIN { + config_file = "awksite.conf" + + read_config_values(config_file) + print_http_header() + process_foreach_line() +} + +function print_http_header() { + print "Content-type: text/html\n\n" +} + +function read_config_values(config_file) { + while ((getline < config_file) > 0) { + position = index($0,"=") + if (position == 0 || /^#/) + continue + + key = substr($0, 0, position-1) + val = substr($0, position+1, 100) + + if (val ~ /^!/) + substr(val, 2, 100) | getline val + + values[key] = val + } + + close(config_file) +} + +function process_foreach_line() { + template_file = values["template"] + while ((getline < template_file) > 0) + print process_line($0) + + close(template_file) +} + +function process_line(line) { + if (line ~ /%%.+%%/) + return insert_template_value(line) + return line +} + +function insert_template_value(line) { + position1 = index(line, "%%") + 2 + temp = substr(line, position1, 100) + + if ((position2 = index(temp, "%%") - 1) == 0 ) return line + + key = substr(temp, 0, position2) + + if (key ~ /^!sort /) + values[key] = read_file_sorted(substr(key, 7, 100)) + + gsub("%%" key "%%", values[key], line) + + if (line ~ /%%/) return insert_template_value(line) + + return line +} + +function read_file_sorted(file) { + retval = ""; command = "cat " file " | sort" + while (( command | getline ) > 0 ) + retval = retval $0 "
\n" + return retval +} + diff --git a/some-text.txt b/some-text.txt new file mode 100644 index 0000000..16bdc89 --- /dev/null +++ b/some-text.txt @@ -0,0 +1,6 @@ +Foo +Bar +Baz +One +Two +Three diff --git a/template.html b/template.html new file mode 100644 index 0000000..bb2bbc1 --- /dev/null +++ b/template.html @@ -0,0 +1,20 @@ + + + + +%%title%% + + +

%%title%%

+

%%subtitle%%

+

The date:

+%%date%% +

The server uptime:

+%%uptime%% +

The content of some-text.txt sorted in alphanumeric order:

+%%!sort some-text.txt%% + + + + + -- cgit v1.2.3