summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2011-01-27 22:10:51 +0000
committerPaul Buetow <paul@buetow.org>2011-01-27 22:10:51 +0000
commitc30a36916e5f37aa97d581cf31565103f804b3eb (patch)
tree6744562b301d41dc407b5e8d4dbb37594aeabce7
initial import from svn.buetow.org/repos/awksitev0.1
-rw-r--r--awksite.conf14
-rwxr-xr-xindex.cgi76
-rw-r--r--some-text.txt6
-rw-r--r--template.html20
4 files changed, 116 insertions, 0 deletions
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 "<br>\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 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
+<title>%%title%%</title>
+</head>
+<body>
+<h1>%%title%%</h1>
+<h3>%%subtitle%%</h3>
+<h2>The date:</h2>
+%%date%%
+<h2>The server uptime:</h2>
+%%uptime%%
+<h2>The content of some-text.txt sorted in alphanumeric order:</h2>
+%%!sort some-text.txt%%
+</body>
+</html>
+
+
+