swerc

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

util.rc (2299B)


      1 ##############################################################################
      2 # Useful CGI stuff
      3 
      4 fn dprint { echo $* >[1=2] }
      5 
      6 fn http_redirect {
      7     if(~ $1 http://* https://*)
      8         t=$1
      9     if not if(~ $1 /*)
     10         t=$"base_url^$1
     11     if not
     12         t=$"base_url^$"req_path^$1
     13     echo 'Status: '^$2^'
     14 Location: '^$t^'
     15 
     16 '
     17     exit
     18 }
     19 fn perm_redirect { http_redirect $1 '301 Moved Permanantly' }
     20 
     21 # This seems slightly improve performance, but might depend on httpd buffering behavior.
     22 fn awk_buffer {
     23     awk '{
     24         buf = buf $0"\n"
     25         if(length(buf) > 1400) {
     26             printf "%s", buf
     27             buf = ""
     28         }
     29     }
     30     END { printf "%s", buf }'
     31 }
     32 
     33 fn static_file {
     34     echo 'Content-Type: '`{select_mime $1}
     35     echo
     36     cat $1
     37     exit
     38 }
     39 
     40 fn select_mime {
     41     m='text/plain'
     42     if(~ $1 *.css)
     43         m='text/css'
     44     if not if(~ $1 *.ico)
     45         m='image/x-icon'
     46     if not if(~ $1 *.png)
     47         m='image/png'
     48     if not if(~ $1 *.jpg *.jpeg)
     49         m='image/jpeg'
     50     if not if(~ $1 *.gif)
     51         m='image/gif'
     52     if not if(~ $1 *.pdf)
     53         m='application/pdf'
     54     echo $m
     55 }
     56 
     57 ##############################################################################
     58 # Generic rc programming helpers
     59 
     60 # Manage nested lists
     61 fn ll_add {
     62     _l=$1^_^$#$1
     63     $_l=$*(2-)
     64     $1=( $$1 $_l )
     65 }
     66 # Add to the head: dangerous if you shrink list by hand!
     67 fn ll_addh {
     68     _l=$1^_^$#$1
     69     $_l=$*(2-)
     70     $1=( $_l $$1 )
     71 }
     72 
     73 
     74 NEW_LINE='
     75 '
     76 
     77 ##############################################################################
     78 # To be used from config files
     79 fn conf_perm_redirect {
     80     if(~ $#* 1)
     81         perm_redir_to=$1
     82     if not
     83         ll_addh perm_redir_patterns $1 $2
     84 }
     85 
     86 fn get_tpl_file {
     87     if(test -f $sitedir/_werc/tpl/$1)
     88         echo -n $sitedir/_werc/tpl/$1
     89     if not if(test -f tpl/$1.local)
     90         echo -n tpl/$1.local
     91     if not if(test -f tpl/$1)
     92         echo -n tpl/$1
     93     if not
     94         status='Can''t find tpl file: '$1
     95 }
     96 
     97 fn template { awk -f bin/template.awk $* | rc $rcargs }
     98 
     99 # File title extraction
    100 fn get_md_title {
    101     sed -n -e '1N; /^.*\n===*$/N; /.*\n===*\n *$/!b' -e 's/\n==*\n//p' < $1
    102 }
    103 
    104 fn get_file_title {
    105     if (~ $1 *.md)
    106         get_md_title $1
    107     if not if(~ $1 */) {
    108         if(test -f $1/index.md)
    109             get_md_title $1/index.md
    110     }
    111 }