changelog shortlog tags branches changeset files revisions annotate raw help

Mercurial > hg > werc / bin/werc.rc

changeset 316: a8c8620f7ff7
parent: 58e2d7be52c0
child: c062cb8f9c32
author: uriel@engel.se.cat-v.org
date: Mon, 12 Jan 2009 02:07:31 +0100
permissions: -rwxr-xr-x
description: Various small cleanups: remove unused code, clarity and reliability fixes here and here, move code to more appropriate locations, improve some comments.
1 #!/usr/local/plan9/bin/rc
2 . ./cgilib.rc
3 cd ..
4 
5 forbidden_uri_chars='[^a-zA-Z0-9_+\-\/\.]'
6 response_format=html
7 
8 fn get_lib_file {
9  if(test -f $sitedir/_werc/lib/$1)
10  echo -n $sitedir/_werc/lib/$1
11  if not if(! ~ $#masterSite 0 && test -f $sitesdir/$masterSite/_werc/lib/$1)
12  echo -n $sitesdir/$masterSite/_werc/lib/$1
13  if not if(test -f lib/$1)
14  echo -n lib/$1
15 }
16 
17 # Don't change var name or trailing ';', dirfilter might be changed from _werc/config!
18 # ls -F style input of the form: $sitedir/path/to/files/
19 # <ls -F+x><symlink hack><Useless?><hiden files >
20 dirfilter='s/\*$//; s,/+\./+,/,g; s,^\./,,; /\/[._][^\/]/d; /'^$forbidden_uri_chars^'/d; /^\/(robots|sitemap)\.txt$|\/index\.(md|html|txt|tpl)$/d; /_werc\/?$/d; '
21 dirclean=' s/\.(md|html|txt)$//; '
22 
23 # To be used from config files
24 fn hide_paths {
25  for(i in $*)
26  dirfilter=$dirfilter^'/^'$i'$/d; '
27 }
28 
29 # Sidebar
30 fn gensidebar {
31  # Ignore stderr, last path element might be a file that doesn't exist (eg., foo for foo.md)
32  # /./ to deal with p9p's ls failure to follow dir symlinks otherwise
33  ls -F $sitedir/./$req_paths_list >[2]/dev/null \
34  | sed 's!^'$sitedir'!!; '^$dirfilter^'/\/[^_.\/][^\/]*(\.(md|txt|html)|\/)$/!d; '^$dirclean \
35  | sort -u | awk -F/ '
36  function p(x, y, s) { for(i=0; i < x-y; i+=1) print s }
37  {
38  d = ""
39  if(match($0, "/$"))
40  d = "/"
41  sub("/$", "") # Strip trailing / for dirs so NF is consistent
42 
43  p(NF, lNF, "<ul class=\"side-bar\">")
44  p(lNF, NF, "</ul>")
45  lNF = NF
46 
47  bname = $NF d
48  path = $0 d
49  gsub("_", " ", bname)
50 
51  if(index(ENVIRON["req_path"] "/", path) == 1)
52  print "<li><a href=\"" path "\" class=\"thisPage\">&raquo;<i> " bname "</i></a>"
53  else
54  print "<li><a href=\"" path "\">&rsaquo; " bname "</a></li>"
55  }
56  END { p(lNF, 0, "</ul>") }'
57 }
58 
59 
60 # Handlers
61 fn md_handler { cat $* | $formatter }
62 
63 fn tpl_handler { template $1 }
64 
65 fn html_handler {
66  # body states: 0 = no <body> found, 2 = after <body>, 1 = after <body></body>, -1 = after </body>
67  awk 'gsub(".*<[Bb][Oo][Dd][Yy][^>]*>", "") > 0 {body=2}
68  gsub("</ *[Bb][Oo][Dd][Yy][^>]*>.*", "") > 0 {print; body=body-1}
69  body==2 {print}
70  body==0 {buf=buf "\n" $0}
71  END {if(body<=0) {print buf}}' < $1
72 }
73 
74 fn txt_handler {
75  # Note: Words are not broken, even if they are way beyond 82 chars long
76  echo '<pre>' `{ sed 's/</\&lt;/g; s/>/\&gt;/g' < $1 | fmt -l 82 -j } '</pre>'
77 }
78 
79 fn dir_listing_handler {
80  d=`{basename -d $1}
81  echo $d|sed 's,.*//,,g; s,/$,,; s,/, / ,g; s,.*,<h1 class="dir-list-head">&</h1> <ul class="dir-list">,'
82  # Symlinks suck: '/.' forces ls to list the linked dir if $d is a symlink.
83  ls -F $d/. | sed $dirfilter$dirclean' s,.*/([^/]+/?)$,<li><a href="\1">\1</a></li>,'
84  echo '</ul>'
85 }
86 
87 fn setup_handlers {
88 
89  if(test -f $local_path.md)
90  handler_body_main=(md_handler $local_path.md)
91  if not if(test -f $local_path.tpl)
92  handler_body_main=(tpl_handler $local_path.tpl)
93  if not if(test -f $local_path.html)
94  handler_body_main=(html_handler $local_path.html)
95  # Global tpl (eg sitemap.tpl), should take precedence over txt handler!
96  if not if(test -f lib^$req_path^.tpl)
97  handler_body_main=(tpl_handler lib^$req_path^.tpl)
98  if not if(test -f $local_path.txt)
99  handler_body_main=(txt_handler $local_path.txt)
100 
101  # Apps
102  if(! ~ $#enabled_apps 0) {
103  for(a in $enabled_apps) {
104  . ./apps/$a/app.rc
105  $a^'_init'
106  }
107  }
108 
109  if(! ~ $#handler_body_main 0)
110  { } # We are done
111  # Dir listing
112  if not if(~ $local_path */index)
113  handler_body_main=(dir_listing_handler $local_path)
114  # Canonize explicit .html urls, the web server might handle this first!
115  if not if(~ $local_path *.html && test -f $local_path)
116  perm_redirect `{ echo $req_path|sed 's/.html$//' }
117  # Fallback static file handler
118  if not if(test -f $local_path)
119  static_file $local_path
120  # File not found
121  if not {
122  handler_body_main=(tpl_handler `{get_lib_file 404.tpl})
123  echo 'Status: 404 Not Found'
124  dprint 'NOT FOUND: '$SERVER_NAME^$"REQUEST_URI^' - '^$"HTTP_REFERER^' - '^$"HTTP_USER_AGENT
125  }
126 }
127 
128 fn run_handlers { for(h in $*) run_handler $$h }
129 fn run_handler { $*(1) $*(2-) }
130 
131 # Careful, the proper p9p path might not be set until initrc.local is sourced
132 path=(. $PLAN9/bin ./bin/ /bin/ /usr/bin)
133 
134 headers=lib/headers.tpl
135 master_template=default_master.tpl
136 sidebar=sidebar
137 werc_root=`{pwd}
138 for(i in siteTitle siteSubTitle pageTitle extraHeaders)
139  $i = ''
140 
141 # TODO: Per-req variables should move after initrc loading.
142 site=$SERVER_NAME
143 base_url=http://$site/
144 sitesdir=sites
145 sitedir=$sitesdir/$site
146 current_date_time=`{date}
147 
148 . ./etc/initrc
149 
150 if(test -f etc/initrc.local)
151  . ./etc/initrc.local
152 
153 # Parse request URL
154 # NOTE: $REQUEST_URI is not officially in CGI 1.1, but seems to be de-facto
155 req_path=`{echo -n $REQUEST_URI | sed 's/\?.*//; s/'^$forbidden_uri_chars^'//g; s/\.\.*/./g; 1q'}
156 local_path=$sitedir$req_path
157 ifs='/' { args=`{echo -n $req_path} }
158 
159 # Hack: preload post data so we can access it from templates where cgi's stdin is not accesible
160 if(~ $REQUEST_METHOD POST) {
161  get_post_args
162  login_user
163 }
164 
165 if(! ~ $#args 0)
166  pageTitle=`{ echo $args|sed -e 's/ / - /g' -e 's/_/ /g' }
167 
168 if(~ $req_path */index)
169  perm_redirect `{echo $req_path | sed 's,/index$,/,'}
170 
171 if(~ $local_path */)
172  local_path=$local_path^'index'
173 if not if(test -d $local_path)
174  perm_redirect $req_path^'/'
175 
176 p=()
177 cd $sitedir
178 if(test -f _werc/config)
179  . _werc/config
180 for(i in ('' $args)) {
181  p=($"p^/^$i)
182  req_paths_list=($req_paths_list $p)
183  if(test -d $i) {
184  cd $i
185  if(test -f _werc/config)
186  . _werc/config
187  }
188 }
189 cd $werc_root
190 
191 # Redirections and other preprocessing
192 if(~ $#redirectPermanent 1) {
193  perm_redirect $"redirectPermanent
194 }
195 if not if(~ $#redirectPermanent 2) {
196  from='http://'^$SERVER_NAME^$req_path
197  to=`{echo $from|sed 's@'^$redirectPermanent(1)^'@'^$redirectPermanent(2)^'@'}
198  if(! ~ $to $from)
199  perm_redirect $to
200 }
201 
202 # Set Page title
203 if(~ $pageTitle '')
204  pageTitle=$siteTitle^' '^$siteSubTitle
205 if not
206  pageTitle=$"pageTitle^' | '^$"siteTitle^' '^$"siteSubTitle
207 
208 setup_handlers
209 
210 if(! ~ $#debug 0)
211  dprint ' '$"SERVER_NAME^$"REQUEST_URI' - '$"HTTP_USER_AGENT' - '$"REQUEST_METHOD' - '$"handler
212 
213 # Template/body selection
214 master_template=`{get_lib_file $master_template}
215 
216 if(~ $response_format html) {
217  template $headers $master_template | awk_buffer
218  echo '</body></html>'
219 }
220 if not if(~ $response_format raw)
221  template < $master_template