swerc

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

handlers.rc (3462B)


      1 # Werc builtin handlers
      2 
      3 fn nav_tree {
      4     echo '<ul>'
      5     if(! ~ $#menuTitle 0) {
      6         if(~ $"req_paths_list /)
      7             echo '<li><a href="/" class="thisPage">'$"menuTitle'</a></li>'
      8         if not
      9             echo '<li><a href="/">'$"menuTitle'</a></li>'
     10     }
     11     # Ignore stderr, last path element might be a file that doesn't exist (eg., foo for foo.md)
     12     # /./ to deal with p9p's ls failure to follow dir symlinks otherwise
     13     ls -F $sitedir/./$req_paths_list >[2]/dev/null \
     14         | { 
     15             sed $dirfilter'/\/[^_.\/][^\/]*(\.(md|1)|\/)$/!d; s!^'$sitedir'!!; '$dirclean 
     16             if(! ~ $#synth_paths 0) echo $synth_paths | tr ' ' $NEW_LINE 
     17         } | sort -u | awk -F/ ' 
     18     function p(x, y, s) { for(i=0; i < x-y; i+=1) print s }
     19     BEGIN { lNF=2; }
     20     { 
     21         d = ""
     22         if(match($0, "/$"))
     23             d = "/"
     24         sub("/$", "") # Strip trailing / for dirs so NF is consistent 
     25 
     26         p(NF, lNF, "<li><ul>")
     27         p(lNF, NF, "</ul></li>")
     28         lNF = NF
     29 
     30         bname = $NF d
     31         path = $0 d
     32         gsub(/[\-_]/, " ", bname)
     33 
     34         # To avoid false matches add trailing / even for plain files to act as delimiter
     35         pa = path
     36         gsub(/[^\/]$/, "&/", pa) 
     37 
     38         if(index(ENVIRON["req_path"] "/", pa) == 1)
     39             print "<li><a href=\"" path "\" class=\"thisPage\"> " bname "</a></li>"
     40         else 
     41             print "<li><a href=\"" path "\">" bname "</a></li>"
     42     }
     43     END { p(lNF, 2, "</ul></li>"); }'
     44     echo '</ul>'
     45 }
     46 
     47 fn md_handler { $formatter $1 }
     48 
     49 fn tpl_handler { template $* }
     50 
     51 fn man_handler {
     52     echo '<pre>'
     53     $man_formatter $1
     54     echo '</pre>'
     55 }
     56 
     57 fn dir_listing_handler {
     58     d=`{basename -d $1}
     59     if(~ $#d 0)
     60         d='/'
     61     echo $d|sed 's,.*//,,g; s,/$,,; s,/, / ,g; s/[\-_]/ /g; s,.*,<h1 class="dir-list-head">&</h1> <ul class="dir-list">,'
     62     # Symlinks suck: '/.' forces ls to list the linked dir if $d is a symlink.
     63     ls -F $dir_listing_ls_opts $sitedir$d/. | sed $dirfilter$dirclean | awk '{match($0, "/[^/]*/?$"); l=substr($0, RSTART+1, RLENGTH-1);n=l; gsub(/[\-_]/, " ", n); print "<li><a href=\""l"\">"n"</a></li>"; }' | uniq
     64     echo '</ul>'
     65 }
     66 
     67 fn setup_handlers {
     68     if(test -f $local_path.md) {
     69         local_file=$local_path.md
     70         handler_body_main=(md_handler $local_file)
     71     }
     72     if not if(test -f $local_path.1) {
     73         local_file=$local_path.1
     74         handler_body_main=(man_handler $local_file)
     75     }
     76     if not if(test -f $local_path.tpl) {
     77         local_file=$local_path.tpl
     78         handler_body_main=(tpl_handler $local_file)
     79     }
     80     if not if(test -f tpl^$req_path^.tpl)
     81         handler_body_main=(tpl_handler tpl^$req_path^.tpl)
     82     if(! ~ $#handler_body_main 0)
     83         { } # We are done
     84     # Dir listing
     85     if not if(~ $local_path */index)
     86         handler_body_main=(dir_listing_handler $req_path)
     87     # Fallback static file handler
     88     if not if(test -f $local_path)
     89         static_file $local_path
     90     if not if(~ $req_path /pub/* && test -f .$req_path)
     91         static_file .$req_path
     92     # File not found
     93     if not
     94         setup_404_handler
     95 }
     96 
     97 # This function allows config files to define their own 404 handlers.
     98 fn setup_404_handler {
     99     handler_body_main=(tpl_handler `{get_tpl_file 404.tpl})
    100     echo 'Status: 404 Not Found'
    101     dprint 'NOT FOUND: '$SERVER_NAME^$"REQUEST_URI^' - '^$"HTTP_REFERER^' - '^$"HTTP_USER_AGENT
    102 }
    103 
    104 fn run_handlers { for(h in $*) run_handler $$h }
    105 fn run_handler { $*(1) $*(2-) }
    106