swerc

anselm's simpler werc fork
git clone git://git.suckless.org/swerc
Log | Files | Refs | README

commit 4aab6f989554856982eaff990139104719451f39
parent 386a7c80d8ef69845b058b8d5315580ab43901bf
Author: uriel@vm41.cat-v.org <unknown>
Date:   Tue, 28 Jul 2009 20:28:35 +0200

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.
Diffstat:
Mbin/corehandlers.rc | 25+++++++++++++++++--------
Mbin/werc.rc | 17++++++++++++++---
Mbin/werclib.rc | 30++++++++++++++++++++++++++++++
Mtpl/sitemap.tpl | 29-----------------------------
4 files changed, 61 insertions(+), 40 deletions(-)

diff --git 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 @@ -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 @@ -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 @@ -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