shithub: werc

Download patch

ref: 71799facad2cdc7a2709ce2e3ef0c80b055bdb96
parent: 70dbd6be889621dc1b07b732d757c3dddd6dc164
author: uriel <[email protected]>
date: Tue Jul 28 16:28:35 EDT 2009

New context var 'local_file' that will point to the on-disk-file that represents the main content of a given page request (if such exists, otherwise it is '' or ()).

Move title extracting functions from sitemap.tpl to werclib.rc.
Rewrite .md title extraction code to only match first lines when 'underlines' with =.

Use the changes above to generate better page titles when possible.

--- a/bin/corehandlers.rc
+++ b/bin/corehandlers.rc
@@ -85,17 +85,26 @@
 
 fn setup_handlers {
 
-    if(test -f $local_path.md)
-        handler_body_main=(md_handler $local_path.md)
-    if not if(test -f $local_path.tpl)
-        handler_body_main=(tpl_handler $local_path.tpl)
-    if not if(test -f $local_path.html)
-        handler_body_main=(html_handler $local_path.html)
+    if(test -f $local_path.md) {
+        local_file=$local_path.md
+        handler_body_main=(md_handler $local_file)
+    }
+    if not if(test -f $local_path.tpl) {
+        local_file=$local_path.tpl
+        handler_body_main=(tpl_handler $local_file)
+    }
+    if not if(test -f $local_path.html) {
+        local_file=$local_path.html
+        handler_body_main=(html_handler $local_file)
+    }
     # Global tpl (eg sitemap.tpl), should take precedence over txt handler!
     if not if(test -f tpl^$req_path^.tpl)
+        # XXX Should we set $local_file for global .tpls?
         handler_body_main=(tpl_handler tpl^$req_path^.tpl)
-    if not if(test -f $local_path.txt)
-        handler_body_main=(txt_handler $local_path.txt)
+    if not if(test -f $local_path.txt) {
+        local_file=$local_path.txt
+        handler_body_main=(txt_handler $local_file)
+    }
 
     # XXX Should check that $enabled_apps exist in $werc_apps?
     # XXX Should split init of apps that provide main handler (eg., blog) and apps that don't (eg., comments)?
--- a/bin/werc.rc
+++ b/bin/werc.rc
@@ -46,6 +46,7 @@
     req_path=`{echo -n $REQUEST_URI | sed 's/\?.*//; s!//+!/!g; s/%5[Ff]/_/g; s/'^$forbidden_uri_chars^'//g; s/\.\.*/./g; 1q'}
     req_url=$base_url^$req_path
     local_path=$sitedir$req_path
+    local_file=''
     ifs='/' { args=`{echo -n $req_path} }
 
     # Preload post args for templates where cgi's stdin is not accessible
@@ -103,13 +104,23 @@
             perm_redirect $t
     }
 
+    setup_handlers
+
+
     # Set Page title
-    if(~ $"pageTitle '')
+    if(! ~ $local_file '') {
+        t=`{get_file_title $local_file}
+        if(! ~ $"t '')
+            pageTitle=$t
+    }
+
+    dprint X^$"pageTitle^X
+
+    # XXX Is this never true? because we set pageTitle earlier based on url.
+    if(~ $"pageTitle '') 
         pageTitle=$"siteTitle' '$"siteSubTitle
     if not
         pageTitle=$"pageTitle' | '$"siteTitle' '$"siteSubTitle
-
-    setup_handlers
 
     for(h in $extraHttpHeaders)
         echo $h
--- a/bin/werclib.rc
+++ b/bin/werclib.rc
@@ -90,6 +90,36 @@
     sed -n '/^\* '$2': /p; /^\* '$2': /q; /^$/q' < $1
 }
 
+# File title extraction
+
+fn get_md_title {
+    #sed 's/^(................................................................[^ ]*).*$/\1/g; 1q' < $1 
+    sed -n -e '1N; /^.*\n===*$/N; /.*\n===*\n$/!b' -e 's/\n==*\n//p' < $1
+}
+
+fn get_html_title {
+    # H1 is not reliable because htmlroff doesn't use it :(
+    #desc=`{cat $1 | sed 32q | grep '<[Hh]1>' | sed 's/<[Hh]1>(.*)(<\/[Hh]1>|$)/\1/;s/<[^>]*>//g;1q'}
+    # Pick the first line of body  instead
+    desc=`{sed -n '/<[Bb][Oo][Dd][Yy]/,/./s/(<[^>]*>|$)//gp' < $1}
+    if(~ $#desc 0)
+        desc=`{sed 's/<[^>]*>//g; 1q' < $1}
+}
+
+fn get_file_title {
+    if (~ $1 *.md)
+        get_md_title $1
+    if not if(~ $1 *.html)
+        get_html_title $1
+    if not if(~ $1 */) {
+        if(test -f $1/index.md)
+            get_md_title $1/index.md
+        if not if(test -f $1/index.html)
+            get_html_title $1/index.html
+    }
+}
+
+
 ##########################################################################
 ##########################################################################
 #app_blog_methods = ( _post index.rss )
--- a/tpl/sitemap.tpl
+++ b/tpl/sitemap.tpl
@@ -4,35 +4,6 @@
 tmpfile=/tmp/werc_sitemap_$pid.txt
 saveddf=$dirfilter
 
-fn get_md_title {
-    sed 's/^(................................................................[^ ]*).*$/\1/g; 1q' < $1 
-}
-
-fn get_html_title {
-    # H1 is not reliable because htmlroff doesn't use it :(
-    #desc=`{cat $1 | sed 32q | grep '<[Hh]1>' | sed 's/<[Hh]1>(.*)(<\/[Hh]1>|$)/\1/;s/<[^>]*>//g;1q'}
-    # Pick the first line of body  instead
-    desc=`{sed -n '/<[Bb][Oo][Dd][Yy]/,/./s/(<[^>]*>|$)//gp' < $1}
-    if(~ $#desc 0)
-        desc=`{sed 's/<[^>]*>//g; 1q' < $1}
-}
-
-fn get_file_title {
-        
-    if(~ $1 */) {
-        if(test -f $1/index.md)
-            get_md_title $1/index.md
-        if not if(test -f $1/index.html)
-            get_html_title $1/index.html
-    }
-    if not if(~ $1 *.md)
-        get_md_title $1
-    if not if(~ $1 *.html)
-        get_html_title $1
-    if not
-        echo ''
-}
-
 fn listDir {
     d=$1
     dirfilter=$saveddf