>From 309ffdb318e67014b8565335cc1d95e4ff5d506c Mon Sep 17 00:00:00 2001 From: Strake <strake...@gmail.com> Date: Wed, 3 Jul 2013 07:26:16 -0500 Subject: [PATCH 1/2] bin/handlers: roll up repeated code
--- bin/handlers.rc | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/bin/handlers.rc b/bin/handlers.rc index cca6495..2055ed4 100644 --- a/bin/handlers.rc +++ b/bin/handlers.rc @@ -54,6 +54,10 @@ fn man_handler { echo '</pre>' } +fn 1_handler { + man_handler $* +} + fn dir_listing_handler { d=`{basename -d $1} if(~ $#d 0) @@ -65,19 +69,12 @@ fn dir_listing_handler { } fn setup_handlers { - 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.1) { - local_file=$local_path.1 - handler_body_main=(man_handler $local_file) - } - if not if(test -f $local_path.tpl) { - local_file=$local_path.tpl - handler_body_main=(tpl_handler $local_file) + local_file=() + for(suffix in md 1 tpl) if(test -f $local_path.$suffix) { + local_file=$local_path.$suffix + handler_body_main=($suffix^_handler $local_file) } - if not if(test -f tpl^$req_path^.tpl) + if(~ $#local_file 0 && test -f tpl^$req_path^.tpl) handler_body_main=(tpl_handler tpl^$req_path^.tpl) if(! ~ $#handler_body_main 0) { } # We are done -- 1.7.11.1 >From de7dfb66068fe2feddcb7d30e95db24712bfe176 Mon Sep 17 00:00:00 2001 From: Strake <strake...@gmail.com> Date: Sun, 18 May 2003 15:11:04 -0500 Subject: [PATCH 2/2] factorize out suffixes --- bin/handlers.rc | 8 ++++++-- bin/werc.rc | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/bin/handlers.rc b/bin/handlers.rc index 2055ed4..b68cff6 100644 --- a/bin/handlers.rc +++ b/bin/handlers.rc @@ -1,5 +1,9 @@ # Werc builtin handlers +suffixes=(md 1 tpl) + +suffix_alternation='('^`{echo $"suffixes | tr ' ' '|'}^')' + fn nav_tree { echo '<ul>' if(! ~ $#menuTitle 0) { @@ -12,7 +16,7 @@ fn nav_tree { # /./ to deal with p9p's ls failure to follow dir symlinks otherwise ls -F $sitedir/./$req_paths_list >[2]/dev/null \ | { - sed $dirfilter'/\/[^_.\/][^\/]*(\.(md|1)|\/)$/!d; s!^'$sitedir'!!; '$dirclean + sed $dirfilter'/\/[^_.\/][^\/]*(\.'^$suffix_alternation^'|\/)$/!d; s!^'$sitedir'!!; '$dirclean if(! ~ $#synth_paths 0) echo $synth_paths | tr ' ' $NEW_LINE } | sort -u | awk -F/ ' function p(x, y, s) { for(i=0; i < x-y; i+=1) print s } @@ -70,7 +74,7 @@ fn dir_listing_handler { fn setup_handlers { local_file=() - for(suffix in md 1 tpl) if(test -f $local_path.$suffix) { + for(suffix in $suffixes) if(test -f $local_path.$suffix) { local_file=$local_path.$suffix handler_body_main=($suffix^_handler $local_file) } diff --git a/bin/werc.rc b/bin/werc.rc index 01f4714..dd77207 100755 --- a/bin/werc.rc +++ b/bin/werc.rc @@ -9,8 +9,8 @@ difs=$ifs # Used to restore default ifs when needed # Expected input: ls -F style, $sitedir/path/to/files/ # <ls -F+x><symlink hack><Useless?><hiden files > -dirfilter='s/\*$//; s,/+\./+,/,g; s,^\./,,; /\/[._][^\/]/d; /'$forbidden_uri_chars'/d; /\/sitemap\.xml$/d; /\/index\.(md|tpl)$/d; /\/(robots|sitemap)\.txt$/d; /_werc\/?$/d; ' -dirclean=' s/\.(md|1)$//; ' +dirfilter='s/\*$//; s,/+\./+,/,g; s,^\./,,; /\/[._][^\/]/d; /'$forbidden_uri_chars'/d; /\/sitemap\.xml$/d; /\/index(\.'^$suffix_alternation^')+$/d; /\/(robots|sitemap)\.txt$/d; /_werc\/?$/d; ' +dirclean=' s/\.'^$suffix_alternation^'$//; ' # Careful, the proper p9p path might not be set until initrc.local is sourced path=(. $PLAN9/bin ./bin /bin /usr/bin) -- 1.7.11.1