changelog shortlog tags branches changeset files revisions annotate raw help

Mercurial > hg > werc / bin/corehandlers.rc

changeset 643: e7176fc9e323
parent: fd97c22d3538
child: dfe8d94928c1
author: Uriel <u@berlinblue.org>
date: Sat, 26 Jun 2010 03:54:41 +0000
permissions: -rw-r--r--
description: Fix typo in comment, thanks btdn for reporting it.
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\">&raquo;<i> " bname "</i></a></li>"
35  else
36  print "<li><a href=\"" path "\">&rsaquo; " 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 { $formatter $1 }
55 
56 fn tpl_handler { template $* }
57 
58 fn html_handler {
59  # body states: 0 = no <body> found, 2 = after <body>, 1 = after <body></body>, -1 = after </body>
60  awk 'gsub(".*<[Bb][Oo][Dd][Yy][^>]*>", "") > 0 {body=2}
61  gsub("</ *[Bb][Oo][Dd][Yy][^>]*>.*", "") > 0 {print; body=body-1}
62  body==2 {print}
63  body==0 {buf=buf "\n" $0}
64  END {if(body<=0) {print buf}}' < $1
65 }
66 
67 fn txt_handler {
68  # Note: Words are not broken, even if they are way beyond 82 chars long
69  echo '<pre>'
70  sed 's/</\&lt;/g; s/>/\&gt;/g' < $1 | fmt -l 82 -j
71  echo '</pre>'
72 }
73 
74 fn dir_listing_handler {
75  d=`{basename -d $1}
76  if(~ $#d 0)
77  d='/'
78  echo $d|sed 's,.*//,,g; s,/$,,; s,/, / ,g; s,.*,<h1 class="dir-list-head">&</h1> <ul class="dir-list">,'
79  # Symlinks suck: '/.' forces ls to list the linked dir if $d is a symlink.
80  ls -F $dir_listing_ls_opts $sitedir$d/. | sed $dirfilter$dirclean' s,.*/([^/]+/?)$,<li><a href="\1">\1</a></li>,'
81  echo '</ul>'
82 }
83 
84 fn notices_handler {
85  for(type in notify_errors notify_notes notify_success)
86  for(n in $$type)
87  echo '<div class="'$type'"><b>'$"n'</b></div>'
88 }
89 
90 fn setup_handlers {
91 
92  if(test -f $local_path.md) {
93  local_file=$local_path.md
94  handler_body_main=(md_handler $local_file)
95  }
96  if not if(test -f $local_path.tpl) {
97  local_file=$local_path.tpl
98  handler_body_main=(tpl_handler $local_file)
99  }
100  if not if(test -f $local_path.html) {
101  local_file=$local_path.html
102  handler_body_main=(html_handler $local_file)
103  }
104  # Global tpl (eg sitemap.tpl), should take precedence over txt handler!
105  if not if(test -f tpl^$req_path^.tpl)
106  # XXX Should we set $local_file for global .tpls?
107  handler_body_main=(tpl_handler tpl^$req_path^.tpl)
108  if not if(test -f $local_path.txt) {
109  local_file=$local_path.txt
110  handler_body_main=(txt_handler $local_file)
111  }
112 
113  # XXX Should check that $enabled_apps exist in $werc_apps?
114  # XXX Should split init of apps that provide main handler (eg., blog) and apps that don't (eg., comments)?
115  if(! ~ $#enabled_apps 0)
116  for(a in $enabled_apps)
117  $a^'_init'
118 
119  if(! ~ $#handler_body_main 0)
120  { } # We are done
121  # Dir listing
122  if not if(~ $local_path */index) {
123  handler_body_main=(dir_listing_handler $req_path)
124  if(test -f $sitedir$req_path'_header.md') {
125  t=`{get_file_title $sitedir$req_path'_header.md'}
126  if(! ~ $"t '')
127  pageTitle=$t
128 
129  ll_add handlers_body_head md_handler $sitedir$req_path'_header.md'
130  }
131  if(test -f $sitedir$req_path'_footer.md')
132  ll_add handlers_body_foot md_handler $sitedir$req_path'_footer.md'
133  }
134  # Canonize explicit .html urls, the web server might handle this first!
135  if not if(~ $local_path *.html && test -f $local_path)
136  perm_redirect `{ echo $req_path|sed 's/.html$//' }
137  # Fallback static file handler
138  if not if(test -f $local_path)
139  static_file $local_path
140  if not if(~ $req_path /pub/* && test -f .$req_path)
141  static_file .$req_path
142  # File not found
143  if not
144  setup_404_handler
145 }
146 
147 # This function allows config files to define their own 404 handlers.
148 fn setup_404_handler {
149  handler_body_main=(tpl_handler `{get_lib_file 404.tpl})
150  echo 'Status: 404 Not Found'
151  dprint 'NOT FOUND: '$SERVER_NAME^$"REQUEST_URI^' - '^$"HTTP_REFERER^' - '^$"HTTP_USER_AGENT
152 }
153 
154 fn run_handlers { for(h in $*) run_handler $$h }
155 fn run_handler { $*(1) $*(2-) }
156 
157