Further cleanup and code reorganization, move generic cgi related functions to cgilib.rc, start to add code to be used for handing comments and wiki editing.

This commit is contained in:
uriel 2008-09-26 10:35:12 +02:00
parent b11c5d52b2
commit e3a70a73fc
3 changed files with 51 additions and 52 deletions

41
bin/cgilib.rc Normal file
View File

@ -0,0 +1,41 @@
# Useful functions
fn dprint { echo $* >[1=2] }
fn perm_redirect {
echo 'Status: 301 Moved Permanantly
Location: '^$1^'
'
exit
}
fn setvars {
ifs='&
' for(pair in `{cat}) {
pair = `{echo -n $pair | sed 's/=/\&/'} \
ifs=() \
if(~ $pair(1) $*)
eval $pair(1)'=`{urldecode $pair(2)}'
}
}
# Is this really useful?
fn awk_buffer {
awk '{
buf = buf $0"\n"
if(length(buf) > 8192) {
printf "%s", buf
buf = ""
}
}
END{ printf "%s", buf }'
}
fn template { template.awk $* | rc $rcargs }
# .rec parsing
fn parse_rec {
sed 's/% *//; /^$/q' < $1
sed -n '/^$/,$p' < $1
}

View File

@ -1,23 +1,10 @@
#!/usr/local/plan9/bin/rc #!/usr/local/plan9/bin/rc
. cgilib.rc
cd .. cd ..
forbidden_uri_chars='[^a-zA-Z0-9_+\-\/\.]' forbidden_uri_chars='[^a-zA-Z0-9_+\-\/\.]'
response_format=html response_format=html
# Useful functions
fn dprint {
echo $* >[1=2]
}
fn perm_redirect {
echo 'Status: 301 Moved Permanantly
Location: '^$1^'
'
exit
}
fn get_lib_file { fn get_lib_file {
wantedfile = $1 wantedfile = $1
if (test -f $sitedir/_werc/lib/$wantedfile) if (test -f $sitedir/_werc/lib/$wantedfile)
@ -133,13 +120,9 @@ fn set_handler {
handler_args = $* handler_args = $*
} }
fn md_handler { fn md_handler { cat $* | $formatter }
cat $* | $formatter
}
fn tpl_handler { fn tpl_handler { template $1 }
template.awk $1 | rc $rcargs
}
fn html_handler { fn html_handler {
cat $1 | /bin/sed '0,/<[Bb][Oo][Dd][Yy][^>]*>/d; /<\/[Bb][Oo][Dd][Yy]>/,$d' cat $1 | /bin/sed '0,/<[Bb][Oo][Dd][Yy][^>]*>/d; /<\/[Bb][Oo][Dd][Yy]>/,$d'
@ -161,9 +144,7 @@ fn dir_listing_handler {
echo '</ul>' echo '</ul>'
} }
fn 404_handler { fn 404_handler { template `{get_lib_file 404.tpl } }
template.awk `{get_lib_file 404.tpl } | rc $rcargs
}
fn blog_dir_handler { fn blog_dir_handler {
blogDirs = $* blogDirs = $*
@ -320,7 +301,7 @@ for (i in ('' $args)) {
} }
if (test -f $fpath/_config) if (test -f $fpath/_config)
. $fpath/_config . $fpath/_config # DEPRECATED
if (test -f $fpath/_werc/config) if (test -f $fpath/_werc/config)
. $fpath/_werc/config . $fpath/_werc/config
@ -335,9 +316,9 @@ if (~ $#redirectPermanent 1)
if not if (~ $#redirectPermanent 2 && {echo $SERVER_NAME^$REQUEST_URI|grep -s $redirectPermanent(1) }) { if not if (~ $#redirectPermanent 2 && {echo $SERVER_NAME^$REQUEST_URI|grep -s $redirectPermanent(1) }) {
# Experimental regexp sub-based redirect, probably should find a nicer interface # Experimental regexp sub-based redirect, probably should find a nicer interface
# For now only used at sites/harmful.cat-v.org/software/OO_programming/_config # For now only used at sites/harmful.cat-v.org/software/OO_programming/_config
to=`{echo $SERVER_NAME^$REQUEST_URI|sed 's|'^$redirectPermanent(1)^'|'^$redirectPermanent(2)^'|'} to=`{echo $SERVER_NAME^$REQUEST_URI|sed 's@'^$redirectPermanent(1)^'@'^$redirectPermanent(2)^'@'}
if(! ~ $to $REQUEST_URI) if(! ~ $to $REQUEST_URI)
perm_redirect $to perm_redirect $to
} }
# Set Page title # Set Page title
@ -348,13 +329,10 @@ if not
body=$sitedir/$body body=$sitedir/$body
rssuri=$uri
if (test -d $body) { if (test -d $body) {
if(! ~ $body */) if(! ~ $body */)
perm_redirect $REQUEST_URI^'/' perm_redirect $REQUEST_URI^'/'
body=$body/index body=$body/index
rssuri=$rssuri/
} }
select_handler select_handler
@ -367,30 +345,10 @@ if(~ $master_template 0) {
master_template=$sitedir/_default.tpl master_template=$sitedir/_default.tpl
} }
# Is this really useful?
fn postfil_awk_buf {
awk '{
buf = buf $0"\n"
if(length(buf) > 8192) {
printf "%s", buf
buf = ""
}
}
END{ printf "%s", buf }'
}
post_filter=postfil_awk_buf
# This probably should merge with tpl_handler
fn template {
if(~ $#post_filter 0)
template.awk | rc $rcargs
if not
template.awk | rc $rcargs | $post_filter
}
if(~ $response_format html) { if(~ $response_format html) {
cat $headers $master_template | template # Is awk_buffer really useful?
template $headers $master_template | awk_buffer
echo '</body></html>' echo '</body></html>'
} }
if not if (~ $response_format raw) { if not if (~ $response_format raw) {

View File

@ -34,8 +34,8 @@ fn statpost {
<description>%($blogDesc%)</description> <description>%($blogDesc%)</description>
<language>en-us</language> <language>en-us</language>
<generator>Tom Duff's rc, and Kris Maglione's clever hackery</generator> <generator>Tom Duff's rc, and Kris Maglione's clever hackery</generator>
<webMaster>uriel99+rss@gmail.com (Uriel)</webMaster>
%{ %{
# <webMaster>uriel99+rss@gmail.com (Uriel)</webMaster>
for(f in `{sortedBlogPostList $blogDirs}) { for(f in `{sortedBlogPostList $blogDirs}) {
statpost $f statpost $f
# Hack to aproximate the last build date # Hack to aproximate the last build date