shithub: werc

Download patch

ref: d0d649b95217c64484d11affd69ea5b73a8e61cd
parent: 17a3f5b9819a92da54ef0b949c82e9f716bea42c
author: uriel <[email protected]>
date: Sun Dec 28 18:40:57 EST 2008

Remove blog-related code from werc.rc
Make variable use more consistent:
    * $REQUEST_URI and $uri are replaced with $req_path
    * $body is replaced with $local_path
Other simplifications and cleanups.

--- a/bin/werc.rc
+++ b/bin/werc.rc
@@ -54,7 +54,7 @@
         path = $0 d
         gsub("_", " ", bname)
 
-        if(index(ENVIRON["REQUEST_URI"] "/", path) == 1)
+        if(index(ENVIRON["req_path"] "/", path) == 1)
             print "<li><a href=\"" path "\" class=\"thisPage\">&raquo;<i> " bname "</i></a>"
         else 
             print "<li><a href=\"" path "\">&rsaquo; " bname "</a></li>"
@@ -99,28 +99,14 @@
     echo '</ul>'
 }
 
-fn blog_dir_handler {
-    blogDirs=$*
-    tpl_handler lib/feeds/html.tpl
-}
-
-fn blog_post_handler {
-    gen_blog_post_title $1 | $formatter
-    $formatter < $1
-}
-
-
 fn select_handler {
 
-    if(test -f $body.md) {
-        if(! ~ $#inBlog 0)
-            set_handler blog_post_handler $body.md
-        if not
-            set_handler md_handler $body.md
+    if(test -f $local_path.md) {
+        set_handler md_handler $local_path.md
         if(! ~ $#allowComments 0 && ~ $REQUEST_METHOD POST) {
             get_post_args comment_text
             d=`{date -n} # FIXME Obvious race
-            d=$body.md_werc/comments/$d/
+            d=$local_path.md_werc/comments/$d/
 
             u=$logged_user
             if(~ $#logged_user 0) {
@@ -141,48 +127,22 @@
             echo $comment_text > $d/body
         }
     }
-    if not if(test -f $body.tpl)
-        set_handler tpl_handler $body.tpl
+    if not if(test -f $local_path.tpl)
+        set_handler tpl_handler $local_path.tpl
 
-    if not if(test -f $body.html)
-        set_handler html_handler $body.html
+    if not if(test -f $local_path.html)
+        set_handler html_handler $local_path.html
 
     # Explicit .html urls, unfortunately usually the web server will handle this as static files
-    if not if(~ $body *.html && test -f $body)
-        perm_redirect `{ echo $REQUEST_URI|sed 's/.html$//' }
+    if not if(~ $local_path *.html && test -f $local_path)
+        perm_redirect `{ echo $req_path|sed 's/.html$//' }
 
-    # Rss feeds. TODO: we should check that the request is for a real blog dir
-    if not if(~ $REQUEST_URI */index.rss */index.atom) {
-        response_format=raw
-        uri=`{echo $uri | sed 's/index.(rss|atom)$//'}
-        if(~ $#blogDirs 0)
-            blogDirs=$sitedir^'/'$uri
-        uri=$base_url$"uri
-
-        if(~ $REQUEST_URI */index.rss)
-            master_template=feeds/rss20.tpl
-        if not if(~ $REQUEST_URI */index.atom)
-            master_template=feeds/atom.tpl
-    }
-
-    # Blog handler
-    if not if(~ $body */[bB]log/index */[bB]log//index || ! ~ $#blogDirs 0) {
-        if(~ $#blogDirs 0)
-            blogDirs=`{basename -d $body}
-
-        u=`{cleanname $base_url^`{basename -d '/'^$uri}|sed 's,:/,://,'} # Sed recovers '/' in 'http:/' stripped by cleanname
-        extraHeaders=$"extraHeaders ^ \
-    	'<link rel="alternate" type="application/rss+xml" title="RSS" href="'$"u'/index.rss" />
-<link rel="alternate" type="application/atom+xml" title="ATOM" href="'$"u'/index.atom" />'
-        set_handler tpl_handler lib/feeds/html.tpl
-    }
-
     # Global tpl (eg sitemap.tpl)
-    if not if(test -f lib/^$uri^.tpl)
-        set_handler tpl_handler lib/^$uri^.tpl
+    if not if(test -f lib^$req_path^.tpl)
+        set_handler tpl_handler lib^$req_path^.tpl
 
-    if not if(test -f $body.txt)
-        set_handler txt_handler $body.txt
+    if not if(test -f $local_path.txt)
+        set_handler txt_handler $local_path.txt
 
     # Apps
     if not if(! ~ $#enabled_apps 0 && select_apps)
@@ -189,13 +149,13 @@
         set_handler app_handler
 
     # Dir listing
-    if not if(~ $body */index)
-        set_handler dir_listing_handler $body
+    if not if(~ $local_path */index)
+        set_handler dir_listing_handler $local_path
 
     # File not found
     if not {
         set_handler tpl_handler `{get_lib_file 404.tpl}
-        dprint 'NOT FOUND: '$SERVER_NAME^$REQUEST_URI^' - '^$"HTTP_REFERER^' - '^$"HTTP_USER_AGENT
+        dprint 'NOT FOUND: '$SERVER_NAME^$"REQUEST_URI^' - '^$"HTTP_REFERER^' - '^$"HTTP_USER_AGENT
         echo 'Status: 404 Not Found'
     }
 
@@ -223,8 +183,9 @@
 werc_root=`{pwd}
 
 # Parse request URL
-uri=`{echo -n $REQUEST_URI | sed 's/\?.*//; s/'^$forbidden_uri_chars^'//g; s/\.\.*/./g; 1q'}
-ifs='/' { args=`{echo -n $uri} }
+# NOTE: $REQUEST_URI is not officially in CGI 1.1, but seems to be de-facto
+req_path=`{echo -n $REQUEST_URI | sed 's/\?.*//; s/'^$forbidden_uri_chars^'//g; s/\.\.*/./g; 1q'}
+ifs='/' { args=`{echo -n $req_path} }
 
 # Hack: preload post data so we can access it from templates where cgi's stdin is not accesible
 if(~ $REQUEST_METHOD POST) {
@@ -234,14 +195,10 @@
 
 if(! ~ $args '') {
     if(~ $args($#args) 'index')
-        perm_redirect `{echo $REQUEST_URI | sed 's,/index$,/,'}
+        perm_redirect `{echo $req_path | sed 's,/index$,/,'}
         
     pageTitle=`{echo $args | sed -e 's/ / - /g' -e 's/_/ /g'}
-    req_path=$uri
 }
-if not {
-    req_path='/'
-}
 
 p=()
 cd $sitedir
@@ -248,19 +205,11 @@
 for(i in ('' $args)) {
     p=($"p^/^$i)
     req_paths_list=($req_paths_list $p)
-    # We don't want blog settings to cascade into posts, note that we are inBlog instead
-    if(! ~ $#blogDirs 0 && ! ~ $req_path */index.rss */index.atom */[bB]log */[bB]log/) {
-        inBlog = $blogDirs
-        blogDirs = () 
-    }
     if(test -d $i) {
         cd $i
         if(test -f _werc/config)
             . _werc/config
     }
-
-    if(~ $#blogDirs 0 && ~ $#inBlog 0 && ~ $i [Bb]log)
-        inBlog = 'yes'
 }
 cd $werc_root
 
@@ -268,10 +217,10 @@
 if(~ $#redirectPermanent 1)
     perm_redirect $"redirectPermanent
 
-if not if(~ $#redirectPermanent 2 && {echo $SERVER_NAME^$REQUEST_URI|grep -s $redirectPermanent(1) }) {
+if not if(~ $#redirectPermanent 2 && {echo $SERVER_NAME^$req_path|grep -s $redirectPermanent(1) }) {
     # Experimental regexp sub-based redirect, probably should find a nicer interface
-    to=`{echo $SERVER_NAME^$REQUEST_URI|sed 's@'^$redirectPermanent(1)^'@'^$redirectPermanent(2)^'@'}
-    if(! ~ $to $REQUEST_URI)
+    to=`{echo $SERVER_NAME^$req_path|sed 's@'^$redirectPermanent(1)^'@'^$redirectPermanent(2)^'@'}
+    if(! ~ $to $req_path)
         perm_redirect $to
 }
 
@@ -282,11 +231,11 @@
 	pageTitle=$"pageTitle^' | '^$"siteTitle^' '^$"siteSubTitle
 
 
-body=$sitedir/$req_path
-if(test -d $body) {
-    if(! ~ $body */)
-    	perm_redirect $REQUEST_URI^'/'
-    body=$body/index
+local_path=$sitedir$req_path
+if(test -d $local_path) {
+    if(! ~ $local_path */)
+    	perm_redirect $req_path^'/'
+    local_path=$local_path^'index'
 }
 
 select_handler