From 2d8ce103893a1ef8371c3c33588469e55b6d42b4 Mon Sep 17 00:00:00 2001 From: Uriel Date: Tue, 28 Jul 2009 20:28:35 +0200 Subject: [PATCH] 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. --- bin/corehandlers.rc | 25 +++++++++++++++++-------- bin/werc.rc | 17 ++++++++++++++--- bin/werclib.rc | 30 ++++++++++++++++++++++++++++++ tpl/sitemap.tpl | 29 ----------------------------- 4 files changed, 61 insertions(+), 40 deletions(-) diff --git a/bin/corehandlers.rc b/bin/corehandlers.rc index e6ea61d..f1052ac 100644 --- a/bin/corehandlers.rc +++ b/bin/corehandlers.rc @@ -85,17 +85,26 @@ fn notices_handler { 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)? diff --git a/bin/werc.rc b/bin/werc.rc index 2c6aa88..28f63b6 100755 --- a/bin/werc.rc +++ b/bin/werc.rc @@ -46,6 +46,7 @@ fn werc_exec_request { 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,14 +104,24 @@ fn werc_exec_request { 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 echo 'Content-Type: '^$http_content_type diff --git a/bin/werclib.rc b/bin/werclib.rc index 2367dbe..0c43417 100644 --- a/bin/werclib.rc +++ b/bin/werclib.rc @@ -90,6 +90,36 @@ fn get_md_file_attr { 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 ) diff --git a/tpl/sitemap.tpl b/tpl/sitemap.tpl index 8f0feeb..334f7e5 100644 --- 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