corehandlers.rc (5160B)
1 # Werc builtin handlers 2 3 fn nav_tree { 4 if(! ~ $#sideBarNavTitle 0) 5 echo '<p class="sideBarTitle">'$"sideBarNavTitle':</p>' 6 # Ignore stderr, last path element might be a file that doesn't exist (eg., foo for foo.md) 7 # /./ to deal with p9p's ls failure to follow dir symlinks otherwise 8 ls -F $sitedir/./$req_paths_list >[2]/dev/null \ 9 | { 10 sed $dirfilter'/\/[^_.\/][^\/]*(\.(md|txt|html)|\/)$/!d; s!^'$sitedir'!!; '$dirclean 11 if(! ~ $#synth_paths 0) echo $synth_paths | tr ' ' $NEW_LINE 12 } | sort -u | awk -F/ ' 13 function p(x, y, s) { for(i=0; i < x-y; i+=1) print s } 14 BEGIN { lNF=2; print "<ul>" } 15 { 16 d = "" 17 if(match($0, "/$")) 18 d = "/" 19 sub("/$", "") # Strip trailing / for dirs so NF is consistent 20 21 p(NF, lNF, "<li><ul>") 22 p(lNF, NF, "</ul></li>") 23 lNF = NF 24 25 bname = $NF d 26 path = $0 d 27 gsub(/[\-_]/, " ", bname) 28 29 # To avoid false matches add trailing / even for plain files to act as delimiter 30 pa = path 31 gsub(/[^\/]$/, "&/", pa) 32 33 if(index(ENVIRON["req_path"] "/", pa) == 1) 34 print "<li><a href=\"" path "\" class=\"thisPage\">»<i> " bname "</i></a></li>" 35 else 36 print "<li><a href=\"" path "\">› " bname "</a></li>" 37 } 38 END { p(lNF, 2, "</ul></li>"); print "</ul>" }' 39 } 40 41 fn link_bar { 42 if(~ $1 -t) { 43 echo '<p class="sideBarTitle">'$2'</p>' 44 shift; shift 45 } 46 echo '<ul>' 47 while(! ~ $#* 0) { 48 echo '<li><a href="'$2'">- '$1'</a></li>' 49 shift; shift 50 } 51 echo '</ul>' 52 } 53 54 fn md_handler { 55 $formatter $1 56 } 57 58 fn tpl_handler { template $* } 59 60 fn html_handler { 61 # body states: 0 = no <body> found, 2 = after <body>, 1 = after <body></body>, -1 = after </body> 62 awk 'gsub(".*<[Bb][Oo][Dd][Yy][^>]*>", "") > 0 {body=2} 63 gsub("</ *[Bb][Oo][Dd][Yy][^>]*>.*", "") > 0 {print; body=body-1} 64 body==2 {print} 65 body==0 {buf=buf "\n" $0} 66 END {if(body<=0) {print buf}}' < $1 67 } 68 69 fn txt_handler { 70 # Note: Words are not broken, even if they are way beyond 82 chars long 71 echo '<pre>' 72 sed 's/</\</g; s/>/\>/g' < $1 | fmt -l 82 -j 73 echo '</pre>' 74 } 75 76 fn dir_listing_handler { 77 d=`{basename -d $1} 78 if(~ $#d 0) 79 d='/' 80 echo $d|sed 's,.*//,,g; s,/$,,; s,/, / ,g; s,.*,<h1 class="dir-list-head">&</h1> <ul class="dir-list">,' 81 # Symlinks suck: '/.' forces ls to list the linked dir if $d is a symlink. 82 ls -F $dir_listing_ls_opts $sitedir$d/. | sed $dirfilter$dirclean' s,.*/([^/]+/?)$,<li><a href="\1">\1</a></li>,' 83 echo '</ul>' 84 } 85 86 fn notices_handler { 87 for(type in notify_errors notify_notes notify_success) 88 for(n in $$type) 89 echo '<div class="'$type'"><b>'$"n'</b></div>' 90 } 91 92 fn setup_handlers { 93 94 if(test -f $local_path.md) { 95 local_file=$local_path.md 96 handler_body_main=(md_handler $local_file) 97 } 98 if not if(test -f $local_path.tpl) { 99 local_file=$local_path.tpl 100 handler_body_main=(tpl_handler $local_file) 101 } 102 if not if(test -f $local_path.html) { 103 local_file=$local_path.html 104 handler_body_main=(html_handler $local_file) 105 } 106 # Global tpl (eg sitemap.tpl), should take precedence over txt handler! 107 if not if(test -f tpl^$req_path^.tpl) 108 # XXX Should we set $local_file for global .tpls? 109 handler_body_main=(tpl_handler tpl^$req_path^.tpl) 110 if not if(test -f $local_path.txt) { 111 local_file=$local_path.txt 112 handler_body_main=(txt_handler $local_file) 113 } 114 115 # XXX Should check that $enabled_apps exist in $werc_apps? 116 # XXX Should split init of apps that provide main handler (eg., blog) and apps that don't (eg., comments)? 117 if(! ~ $#enabled_apps 0) 118 for(a in $enabled_apps) 119 $a^'_init' 120 121 if(! ~ $#handler_body_main 0) 122 { } # We are done 123 # Dir listing 124 if not if(~ $local_path */index) { 125 handler_body_main=(dir_listing_handler $req_path) 126 if(test -f $sitedir$req_path'_header.md') 127 ll_add handlers_body_head md_handler $sitedir$req_path'_header.md' 128 if(test -f $sitedir$req_path'_footer.md') 129 ll_add handlers_body_foot md_handler $sitedir$req_path'_footer.md' 130 } 131 # Canonize explicit .html urls, the web server might handle this first! 132 if not if(~ $local_path *.html && test -f $local_path) 133 perm_redirect `{ echo $req_path|sed 's/.html$//' } 134 # Fallback static file handler 135 if not if(test -f $local_path) 136 static_file $local_path 137 if not if(~ $req_path /pub/* && test -f .$req_path) 138 static_file .$req_path 139 # File not found 140 if not 141 setup_404_handler 142 } 143 144 # This function allows config files to define their own 404 handlers. 145 fn setup_404_handler { 146 handler_body_main=(tpl_handler `{get_lib_file 404.tpl}) 147 echo 'Status: 404 Not Found' 148 dprint 'NOT FOUND: '$SERVER_NAME^$"REQUEST_URI^' - '^$"HTTP_REFERER^' - '^$"HTTP_USER_AGENT 149 } 150 151 fn run_handlers { for(h in $*) run_handler $$h } 152 fn run_handler { $*(1) $*(2-) } 153 154