Remove ugly ternary expression
[geekigeeki.git] / geekigeeki.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright 1999, 2000 Martin Pool <mbp@humbug.org.au>
5 # Copyright 2002 Gerardo Poggiali
6 # Copyright 2007, 2008, 2009 Bernie Innocenti <bernie@codewiz.org>
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.  You should have received a copy
12 # of the GNU General Public License along with this program.
13 # If not, see <http://www.gnu.org/licenses/>.
14
15 __version__ = '4.0-' + '$Id$'[4:11]
16
17 from time import clock, localtime, gmtime, strftime
18 start_time = clock()
19 title_done = False
20
21 import cgi, sys, os, re, errno, stat, glob
22
23 image_ext = 'png|gif|jpg|jpeg|bmp|ico'
24 video_ext = "ogg|ogv|oga" # Not supported by Firefox 3.5: mkv|mpg|mpeg|mp4|avi|asf|flv|wmv|qt
25 image_re = re.compile(r".*\.(" + image_ext + "|" +  video_ext + ")", re.IGNORECASE)
26 video_re = re.compile(r".*\.(" + video_ext + ")", re.IGNORECASE)
27 # FIXME: we accept stuff like foo/../bar and we shouldn't
28 file_re  = re.compile(r"([A-Za-z0-9_\-][A-Za-z0-9_\.\-/]*)")
29 url_re   = re.compile(r"[a-z]{3,8}://[^\s'\"]+\S")
30 ext_re   = re.compile(r"\.([^\./]+)$")
31
32 # CGI stuff ---------------------------------------------------------
33 def config_get(key, default=None):
34     return globals().get(key, default)
35
36 def script_name():
37     return os.environ.get('SCRIPT_NAME', '')
38
39 def script_path():
40     return os.path.split(os.environ.get('SCRIPT_FILENAME', ''))[0]
41
42 def query_string():
43     path_info = os.environ.get('PATH_INFO', '')
44     if len(path_info) and path_info[0] == '/':
45         return path_info[1:] or 'FrontPage'
46     else:
47         return os.environ.get('QUERY_STRING', '') or 'FrontPage'
48
49 def privileged_path():
50     return config_get('privileged_url') or script_name()
51
52 def remote_user():
53     user = os.environ.get('REMOTE_USER', '')
54     if user is None or user == '' or user == 'anonymous':
55         user = 'AnonymousCoward'
56     return user
57
58 def remote_host():
59     return os.environ.get('REMOTE_ADDR', '')
60
61 def get_hostname(addr):
62     try:
63         from socket import gethostbyaddr
64         return gethostbyaddr(addr)[0] + ' (' + addr + ')'
65     except Exception:
66         return addr
67
68 def is_external_url(pathname):
69     return (url_re.match(pathname) or pathname.startswith('/'))
70
71 def relative_url(pathname, privileged=False):
72     if not is_external_url(pathname):
73         if privileged:
74             url = privileged_path()
75         else:
76             url = script_name()
77         pathname = url + '/' + pathname
78     return cgi.escape(pathname, quote=True)
79
80 def permalink(s):
81     return re.sub(' ', '-', re.sub('[^a-z0-9_ ]', '', s.lower()).strip())
82
83 def humanlink(s):
84     return re.sub(r'(?:.*[/:]|)([^:/\.]+)(?:\.[^/:]+|)$', r'\1', s.replace('_', ' '))
85
86 # Split arg lists like "blah|blah blah| width=100 | align = center",
87 # return a list containing anonymous arguments and a map containing the named arguments
88 def parse_args(s):
89     args = []
90     kvargs = {}
91     for arg in s.strip('<[{}]>').split('|'):
92         m = re.match('\s*(\w+)\s*=\s*(.+)\s*', arg)
93         if m is not None:
94             kvargs[m.group(1)] = m.group(2)
95         else:
96             args.append(arg.strip())
97     return (args, kvargs)
98
99 def url_args(kvargs):
100     argv = []
101     for k, v in kvargs.items():
102         argv.append(k + '=' + v)
103     if argv:
104         return '?' + '&amp;'.join(argv)
105     return ''
106
107 # Formatting stuff --------------------------------------------------
108 def emit_header(mtime=None, mime_type="text/html"):
109     if mtime:
110         print("Last-Modified: " + strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime(mtime)))
111     print("Content-type: " + mime_type + "; charset=utf-8\n")
112
113 def send_guru(msg_text, msg_type):
114     if not msg_text: return
115     print('<pre id="guru" onclick="this.style.display = \'none\'" class="' + msg_type + '">')
116     if msg_type == 'error':
117         print('    Software Failure.  Press left mouse button to continue.\n')
118     print(cgi.escape(msg_text))
119     if msg_type == 'error':
120         print '\n           Guru Meditation #DEADBEEF.ABADC0DE'
121     print('</pre><script language="JavaScript" type="text/javascript" src="%s" defer="defer"></script>' \
122         % relative_url('sys/GuruMeditation.js'))
123
124 def send_title(name, text="Limbo", msg_text=None, msg_type='error', writable=False, mtime=None):
125     global title_done
126     if title_done: return
127
128     # Head
129     emit_header(mtime)
130     print('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"')
131     print('  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">')
132     print('<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">')
133
134     print("<head><title>%s: %s</title>" % (config_get('site_name', "Unconfigured Wiki"), text))
135     print(' <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />')
136     if not name:
137         print(' <meta name="robots" content="noindex,nofollow" />')
138
139     for http_equiv, content in config_get('meta_urls', {}):
140         print(' <meta http-equiv="%s" content="%s" />' % (http_equiv, relative_url(content)))
141
142     for link in config_get('link_urls', {}):
143         rel, href = link
144         print(' <link rel="%s" href="%s" />' % (rel, relative_url(href)))
145
146     editable = name and writable and config_get('privileged_url') is not None
147     if editable:
148         print(' <link rel="alternate" type="application/x-wiki" title="Edit this page" href="%s" />' \
149             % relative_url('?a=edit&q=' + name, privileged=True))
150
151     history = config_get('history_url')
152     if history is not None:
153         print(' <link rel="alternate" type="application/rss+xml" title="RSS" href="%s" />' \
154             % relative_url(history + '?a=rss'))
155
156     print('</head>')
157
158     # Body
159     if editable:
160         print('<body ondblclick="location.href=\'' + relative_url('?a=edit&q=' + name, privileged=True) + '\'">')
161     else:
162         print('<body>')
163
164     title_done = True
165     send_guru(msg_text, msg_type)
166
167     # Navbar
168     print('<div class="nav">')
169     print link_tag('FrontPage', config_get('site_icon', 'Home'), cssclass='navlink')
170     if name:
171         print('  <b>' + link_tag('?fullsearch=' + name, text, cssclass='navlink') + '</b> ')
172     else:
173         print('  <b>' + text + '</b> ')
174     print(' | ' + link_tag('FindPage', 'Find Page', cssclass='navlink'))
175     if history:
176         print(' | <a href="' + relative_url(history) + '" class="navlink">Recent Changes</a>')
177         if name:
178             print(' | <a href="' + relative_url(history + '?a=history;f=' + name) + '" class="navlink">Page History</a>')
179
180     if name:
181         print(' | ' + link_tag(name + '?a=raw', 'Raw Text', cssclass='navlink'))
182         if config_get('privileged_url') is not None:
183             if writable:
184                 print(' | ' + link_tag('?a=edit&q=' + name, 'Edit', cssclass='navlink', privileged=True))
185             else:
186                 print(' | ' + link_tag(name, 'Login', cssclass='navlink', privileged=True))
187
188     else:
189         print(' | <i>Immutable Page</i>')
190
191     user = remote_user()
192     if user != 'AnonymousCoward':
193         print(' | <span class="login"><i><b>' + link_tag('User/' + user, user) + '</b></i></span>')
194
195     print('<hr /></div>')
196
197 def send_httperror(status="403 Not Found", query=""):
198     print("Status: %s" % status)
199     send_title(None, msg_text=("%s: on query '%s'" % (status, query)))
200     send_footer()
201
202 def link_tag(dest, text=None, privileged=False, **kvargs):
203     if text is None:
204         text = humanlink(dest)
205     elif image_re.match(text):
206         text = '<img border="0" src="' + relative_url(text) + '" alt="' + text + '" />'
207
208     link_class = kvargs.get('class', kvargs.get('cssclass', None))
209     if not link_class:
210         if is_external_url(dest):
211             link_class = 'external'
212         elif file_re.match(dest) and Page(dest).exists():
213             link_class = 'wikilink'
214         else:
215             text = config_get('nonexist_pfx', '') + text
216             link_class = 'nonexistent'
217
218     # Prevent crawlers from following links potentially added by spammers or to generated pages
219     nofollow = ''
220     if link_class == 'external' or link_class == 'navlink':
221         nofollow = 'rel="nofollow" '
222
223     return '<a class="%s" %shref="%s">%s</a>' % (link_class, nofollow, relative_url(dest, privileged=privileged), text)
224
225 def link_inline(name, descr=None, kvargs={}):
226     if not descr: descr = humanlink(name)
227     url = relative_url(name)
228     if video_re.match(name):
229         return '<video controls="1" src="%s">Your browser does not support HTML5 video</video>' % url
230     elif image_re.match(name):
231         return '<a href="%s"><img border="0" src="%s" alt="%s" /></a>' % (url, url + url_args(kvargs), descr)
232     elif file_re.match(name) and not ext_re.search(name): # FIXME: this guesses a wiki page
233         Page(name).send_naked(kvargs) # FIXME: we should return the page as a string rather than print it
234         return ''
235     else:
236         return '<iframe width="100%%" scrolling="auto" frameborder="0" src="%s"><a href="%s">%s</a></iframe>' \
237             % (url, url, name)
238
239 def link_inline_glob(pattern, descr=None, kvargs={}):
240     if not url_re.match(pattern) and bool(set(pattern) & set('?*[')):
241         s = ''
242         for name in glob.glob(pattern):
243             s += link_inline(name, descr, kvargs)
244         return s
245     else:
246         return link_inline(pattern, descr, kvargs)
247
248 # Search ---------------------------------------------------
249
250 def print_search_stats(hits, searched):
251     print("<p>%d hits out of %d pages searched.</p>" % (hits, searched))
252
253 def handle_fullsearch(query, form):
254     needle = form['q'].value
255     send_title(None, 'Full text search for "' + needle + '"')
256
257     needle_re = re.compile(needle, re.IGNORECASE)
258     hits = []
259     all_pages = page_list()
260     for page_name in all_pages:
261         body = Page(page_name).get_raw_body()
262         count = len(needle_re.findall(body))
263         if count:
264             hits.append((count, page_name))
265
266     # The default comparison for tuples compares elements in order,
267     # so this sorts by number of hits
268     hits.sort()
269     hits.reverse()
270
271     print("<ul>")
272     for (count, page_name) in hits:
273         print('<li><p>' + link_tag(page_name))
274         print(' . . . . ' + `count`)
275         print(['match', 'matches'][count != 1])
276         print('</p></li>')
277     print("</ul>")
278
279     print_search_stats(len(hits), len(all_pages))
280
281 def handle_titlesearch(query, form):
282     needle = form['q'].value
283     send_title(None, 'Title search for "' + needle + '"')
284
285     needle_re = re.compile(needle, re.IGNORECASE)
286     all_pages = page_list()
287     hits = list(filter(needle_re.search, all_pages))
288
289     print("<ul>")
290     for filename in hits:
291         print('<li><p>' + link_tag(filename) + "</p></li>")
292     print("</ul>")
293
294     print_search_stats(len(hits), len(all_pages))
295
296 def handle_raw(pagename, form):
297     if not file_re.match(pagename):
298         send_httperror("403 Forbidden", pagename)
299         return
300
301     Page(pagename).send_raw()
302
303 def handle_edit(pagename, form):
304     if not file_re.match(pagename):
305         send_httperror("403 Forbidden", pagename)
306         return
307
308     pg = Page(form['q'].value)
309     if 'save' in form:
310         if form['file'].value:
311             pg.save(form['file'].file.read(), form['changelog'].value)
312         else:
313             pg.save(form['savetext'].value.replace('\r\n', '\n'), form['changelog'].value)
314         pg.format()
315     elif 'cancel' in form:
316         pg.msg_text = 'Editing canceled'
317         pg.msg_type = 'notice'
318         pg.format()
319     else: # preview or edit
320         text = None
321         if 'preview' in form:
322             text = form['savetext'].value
323         pg.send_editor(text)
324
325 def handle_get(pagename, form):
326         if file_re.match(pagename):
327             # FIMXE: this is all bullshit, MimeTypes bases its guess on the extension!
328             from mimetypes import MimeTypes
329             mimetype, encoding = MimeTypes().guess_type(pagename)
330             if mimetype:
331                 Page(pagename).send_raw(mimetype=mimetype, args=form)
332             else:
333                 Page(pagename).format()
334         else:
335             send_httperror("403 Forbidden", pagename)
336
337 # Used by sys/macros/WordIndex and sys/macros/TitleIndex
338 def make_index_key():
339     links = ['<a href="#%s">%s</a>' % (ch, ch) for ch in 'abcdefghijklmnopqrstuvwxyz']
340     return '<p style="text-align: center">' + ' | '.join(links) + '</p>'
341
342 def page_list(dirname=None, search_re=None):
343     if search_re is None:
344         # FIXME: WikiWord is too restrictive now!
345         search_re = re.compile(r"^\b((([A-Z][a-z0-9]+){2,}/)*([A-Z][a-z0-9]+){2,})\b$")
346     return sorted(filter(search_re.match, os.listdir(dirname or '.')))
347
348 def send_footer(mtime=None):
349     if config_get('debug_cgi', False):
350         cgi.print_arguments()
351         cgi.print_form(form)
352         cgi.print_environ()
353     link_inline("sys/footer", kvargs = {
354         'LAST_MODIFIED': strftime(config_get('datetime_fmt', '%a %d %b %Y %I:%M %p'), localtime(mtime))
355     })
356     print("</body></html>")
357
358 def _macro_ELAPSED_TIME(*args, **kvargs):
359     return "%03f" % (clock() - start_time)
360
361 def _macro_VERSION(*args, **kvargs):
362     return __version__
363
364 class WikiFormatter:
365     """Object that turns Wiki markup into HTML.
366
367     All formatting commands can be parsed one line at a time, though
368     some state is carried over between lines.
369     """
370     def __init__(self, raw, kvargs=None):
371         self.raw = raw
372         self.kvargs = kvargs or {}
373         self.h_level = 0
374         self.in_pre = self.in_html = self.in_table = self.in_li = False
375         self.in_header = True
376         self.list_indents = [] # a list of pairs (indent_level, list_type) to track nested lists
377         self.tr_cnt = 0
378         self.styles = {
379             #wiki   html   enabled?
380             "//":  ["em",  False],
381             "**":  ["b",   False],
382             "##":  ["tt",  False],
383             "__":  ["u",   False],
384             "--":  ["del", False],
385             "^^":  ["sup", False],
386             ",,":  ["sub", False],
387             "''":  ["em",  False], # LEGACY
388             "'''": ["b",   False], # LEGACY
389             "``":  ["tt",  False], # LEGACY
390         }
391
392     def _b_repl(self, word):
393         style = self.styles[word]
394         style[1] = not style[1]
395         return ['</', '<'][style[1]] + style[0] + '>'
396
397     def _glyph_repl(self, word):
398         return '&mdash;'
399
400     def _tit_repl(self, word):
401         if self.h_level:
402             result = '</h%d><p>\n' % self.h_level
403             self.h_level = 0
404         else:
405             self.h_level = len(word) - 1
406             link = permalink(self.line)
407             result = '\n</p><h%d id="%s"><a class="heading" href="#%s">¶</a> ' % (self.h_level, link, link)
408         return result
409
410     def _br_repl(self, word):
411         return '<br />'
412
413     def _rule_repl(self, word):
414         return '\n<hr size="%d" noshade="noshade" />\n' % (len(word) - 2)
415
416     def _macro_repl(self, word):
417         try:
418             args, kvargs = parse_args(word)
419             if args[0] in self.kvargs:
420                 return self.kvargs[args[0]]
421             macro = globals().get('_macro_' + args[0])
422             if not macro:
423                 exec(open("sys/macros/" + args[0] + ".py").read(), globals())
424                 macro = globals().get('_macro_' + args[0])
425             return macro(*args, **kvargs)
426         except Exception, e:
427             msg = cgi.escape(word) + ": " + cgi.escape(str(e))
428             if not self.in_html:
429                 msg = '<strong class="error">' + msg + '</strong>'
430             return msg
431
432     def _hurl_repl(self, word):
433         args, kvargs = parse_args(word)
434         return link_tag(*args, **kvargs)
435
436     def _inl_repl(self, word):
437         args, kvargs = parse_args(word)
438         name = args.pop(0)
439         if len(args):
440             descr = args.pop(0)
441             # This double div nonsense works around a limitation of the HTML block model
442             return '<div class="' + kvargs.get('class', 'thumb') + '">' \
443                 + '<div class="innerthumb">' \
444                 + link_inline_glob(name, descr, kvargs) \
445                 + '<div class="caption">' + descr + '</div></div></div>'
446         else:
447             return link_inline_glob(name, None, kvargs)
448
449     def _html_repl(self, word):
450         if not self.in_html and word.startswith('<div'): word = '</p>' + word
451         self.in_html += 1
452         return word; # Pass through
453
454     def _htmle_repl(self, word):
455         self.in_html -= 1
456         if not self.in_html and word.startswith('</div'): word += '<p>'
457         return word; # Pass through
458
459     def _ent_repl(self, s):
460         if self.in_html:
461             return s; # Pass through
462         return {'&': '&amp;',
463                 '<': '&lt;',
464                 '>': '&gt;'}[s]
465
466     def _img_repl(self, word): # LEGACY
467         return self._inl_repl('{{' + word + '}}')
468
469     def _word_repl(self, word): # LEGACY
470         if self.in_html: return word # pass through
471         return link_tag(word)
472
473     def _url_repl(self, word): # LEGACY
474         if self.in_html: return word # pass through
475         return link_tag(word)
476
477     def _email_repl(self, word): # LEGACY
478         if self.in_html: return word # pass through
479         return '<a href="mailto:%s">%s</a>' % (word, word)
480
481     def _li_repl(self, match):
482         if self.in_li:
483             return '</li><li>'
484         else:
485             self.in_li = True
486             return '<li>'
487
488     def _pre_repl(self, word):
489         if word == '{{{' and not self.in_pre:
490             self.in_pre = True
491             return '<pre>'
492         elif self.in_pre:
493             self.in_pre = False
494             return '</pre>'
495         return ''
496
497     def _hi_repl(self, word):
498         return '<strong class="highlight ' + word + '">' + word + '</strong>'
499
500     def _tr_repl(self, word):
501         out = ''
502         if not self.in_table:
503             self.in_table = True
504             self.tr_cnt = 0
505             out = '</p><table><tbody>\n'
506         self.tr_cnt += 1
507         out = out + '<tr class="' + ['even', 'odd'][self.tr_cnt % 2] + '">'
508         return out + ['<td>', '<th>'][word.strip() == '||=']
509
510     def _td_repl(self, word):
511         if self.in_table:
512             return ['</td><td>', '</th><th>'][word.strip() == '||=']
513         return ''
514
515     def _tre_repl(self, word):
516         if self.in_table:
517             return ['</td></tr>', '</th></tr>'][word.strip() == '||=']
518         return ''
519
520     def _indent_level(self):
521         return len(self.list_indents) and self.list_indents[-1][0]
522
523     def _indent_to(self, new_level, list_type=''):
524         if self._indent_level() == new_level:
525             return ''
526         s = '</p>'
527         while self._indent_level() > new_level:
528             if self.in_li:
529                 s += '</li>'
530                 self.in_li = False # FIXME
531             s += '</' + self.list_indents[-1][1] + '>\n'
532             del(self.list_indents[-1])
533
534         list_type = ('ul', 'ol')[list_type == '#']
535         while self._indent_level() < new_level:
536             self.list_indents.append((new_level, list_type))
537             s += '<' + list_type + '>\n'
538         s += '<p>'
539         return s
540
541     def replace(self, match):
542         for rule, hit in list(match.groupdict().items()):
543             if hit:
544                 return getattr(self, '_' + rule + '_repl')(hit)
545         else:
546             raise Exception("Can't handle match " + repr(match))
547
548     def print_html(self):
549         print('<div class="wiki"><p>')
550
551         scan_re = re.compile(r"""(?:
552             # Styles and formatting ("--" must cling to a word to disambiguate it from the dash)
553               (?P<b>     \*\* | // | \#\# | __ | --\b | \b-- | \^\^ | ,, | ''' | '' | `` )
554             | (?P<tit>   \={2,6})
555             | (?P<br>    \\\\)
556             | (?P<rule>  ^-{3,})
557             | (?P<hi>    \b( FIXME | TODO | DONE )\b )
558             | (?P<glyph> --)
559
560             # Links
561             | (?P<macro> \<\<([^\s\|\>]+)(?:\s*\|\s*([^\>]+)|)\>\>)
562             | (?P<hurl>  \[\[([^\s\|]+)(?:\s*\|\s*([^\]]+)|)\]\])
563
564             # Inline HTML
565             | (?P<html>             <(br|hr|div|span|form|iframe|input|textarea|a|img|h[1-5])\b )
566             | (?P<htmle> ( /\s*> | </(br|hr|div|span|form|iframe|input|textarea|a|img|h[1-5])> ) )
567             | (?P<ent>   [<>&] )
568
569             # Auto links (LEGACY)
570             | (?P<img>   \b[a-zA-Z0-9_/-]+\.(""" + image_ext + "|" + video_ext + r"""))
571             | (?P<word>  \b(?:[A-Z][a-z]+){2,}\b)
572             | (?P<url>   (http|https|ftp|mailto)\:[^\s'\"]+\S)
573             | (?P<email> [-\w._+]+\@[\w.-]+)
574
575             # Lists, divs, spans and inline objects
576             | (?P<li>    ^\s+[\*\#]\s+)
577             | (?P<pre>   \{\{\{|\s*\}\}\})
578             | (?P<inl>   \{\{([^\s\|]+)(?:\s*\|\s*([^\]]+)|)\}\})
579
580             # Tables
581             | (?P<tr>    ^\s*\|\|(=|)\s*)
582             | (?P<tre>   \s*\|\|(=|)\s*$)
583             | (?P<td>    \s*\|\|(=|)\s*)
584
585             # TODO: highlight search words (look at referrer)
586           )""", re.VERBOSE)
587         pre_re = re.compile("""(?:
588               (?P<pre>\s*\}\}\})
589             | (?P<ent>[<>&])"
590             )""", re.VERBOSE)
591         blank_re = re.compile(r"^\s*$")
592         indent_re = re.compile(r"^(\s*)(\*|\#|)")
593         tr_re = re.compile(r"^\s*\|\|")
594         eol_re = re.compile(r"\r?\n")
595         # For each line, we scan through looking for magic strings, outputting verbatim any intervening text
596         #3.0: for self.line in eol_re.split(str(self.raw.expandtabs(), 'utf-8')):
597         for self.line in eol_re.split(str(self.raw.expandtabs())):
598             # Skip pragmas
599             if self.in_header:
600                 if self.line.startswith('#'):
601                     continue
602                 self.in_header = False
603
604             if self.in_pre:
605                 print(re.sub(pre_re, self.replace, self.line))
606             else:
607                 if self.in_table and not tr_re.match(self.line):
608                     self.in_table = False
609                     print('</tbody></table><p>')
610
611                 if blank_re.match(self.line):
612                     print('</p><p>')
613                 else:
614                     indent = indent_re.match(self.line)
615                     print(self._indent_to(len(indent.group(1)), indent.group(2)))
616                     # Stand back! Here we apply the monster regex that does all the parsing
617                     print(re.sub(scan_re, self.replace, self.line))
618
619         if self.in_pre: print('</pre>')
620         if self.in_table: print('</tbody></table><p>')
621         print(self._indent_to(0))
622         print('</p></div>')
623
624 class Page:
625     def __init__(self, page_name):
626         self.page_name = page_name
627         self.msg_text = ''
628         self.msg_type = 'error'
629
630     def split_title(self):
631         # look for the end of words and the start of a new word and insert a space there
632         return re.sub('([a-z])([A-Z])', r'\1 \2', self.page_name)
633
634     def _filename(self):
635         return self.page_name
636
637     def _tmp_filename(self):
638         return self.page_name + '.tmp' + str(os.getpid()) + '#'
639
640     def _mtime(self):
641         try:
642             return os.stat(self._filename()).st_mtime
643         except OSError, err:
644             if err.errno == errno.ENOENT:
645                 return None
646             raise err
647
648     def exists(self):
649         if self._mtime():
650             return True
651         return False
652
653     def get_raw_body(self, default=None):
654         try:
655             return open(self._filename(), 'rb').read()
656         except IOError, err:
657             if err.errno == errno.ENOENT:
658                 if default is None:
659                     default = '//[[?a=edit&q=%s|Describe %s]]//' % (self.page_name, self.page_name)
660                 return default
661             if err.errno == errno.EISDIR:
662                 return self.format_dir()
663             raise err
664
665     def format_dir(self):
666         out = '== '
667         pathname = ''
668         for dirname in self.page_name.strip('/').split('/'):
669             pathname = (pathname and pathname + '/' ) + dirname
670             out += '[[' + pathname + '|' + dirname + ']]/'
671         out += ' ==\n'
672  
673         for filename in page_list(self._filename(), file_re):
674             if image_re.match(filename):
675                 maxwidth = config_get('image_maxwidth', '400')
676                 if maxwidth:
677                     maxwidth = ' | maxwidth=' + str(maxwidth)
678                 out += '{{' + self.page_name + '/' + filename + ' | ' + humanlink(filename) + maxwidth + ' | class=thumbleft}}\n'
679             else:
680                 out += ' * [[' + self.page_name + '/' + filename + ']]\n'
681         return out
682
683     def pragmas(self):
684         if not '_pragmas' in self.__dict__:
685             self._pragmas = {}
686             try:
687                 file = open(self._filename(), 'rt')
688                 attr_re = re.compile(r"^#(\S*)(.*)$")
689                 for line in file:
690                     m = attr_re.match(line)
691                     if not m:
692                         break
693                     self._pragmas[m.group(1)] = m.group(2).strip()
694                     #print "bernie: pragmas[" + m.group(1) + "] = " + m.group(2) + "<br>\n"
695             except IOError, err:
696                 if err.errno != errno.ENOENT and err.errno != errno.EISDIR:
697                     raise er
698         return self._pragmas
699
700     def pragma(self, name, default):
701         return self.pragmas().get(name, default)
702
703     def can(self, action, default=True):
704         acl = None
705         try:
706             #acl SomeUser:read,write All:read
707             acl = self.pragma("acl", None)
708             for rule in acl.split():
709                 (user, perms) = rule.split(':')
710                 if user == remote_user() or user == "All":
711                     return action in perms.split(',')
712             return False
713         except Exception:
714             if acl:
715                 self.msg_text = 'Illegal acl line: ' + acl
716         return default
717
718     def can_write(self):
719         return self.can("write", True)
720
721     def can_read(self):
722         return self.can("read", True)
723
724     def send_naked(self, kvargs=None):
725         if self.can_read():
726             WikiFormatter(self.get_raw_body(), kvargs).print_html()
727         else:
728             send_guru("Read access denied by ACLs", "notice")
729
730     def format(self):
731         #css foo.css
732         value = self.pragma("css", None)
733         if value:
734             global link_urls
735             link_urls += [ [ "stylesheet", value ] ]
736
737         send_title(self.page_name, self.split_title(),
738             msg_text=self.msg_text, msg_type=self.msg_type, writable=self.can_write(), mtime=self._mtime())
739         self.send_naked()
740         send_footer(mtime=self._mtime())
741
742     def send_editor(self, preview=None):
743         send_title(None, 'Edit ' + self.split_title(), msg_text=self.msg_text, msg_type=self.msg_type)
744         if not self.can_write():
745             send_guru("Write access denied by ACLs", "error")
746             return
747
748         if preview is None:
749             preview = self.get_raw_body(default='')
750
751         link_inline("sys/EditPage", kvargs = {
752             'EDIT_BODY': cgi.escape(preview),
753             #'EDIT_PREVIEW': WikiFormatter(preview).print_html(),
754         })
755
756         if preview:
757             print("<div class='preview'>")
758             WikiFormatter(preview).print_html()
759             print("</div>")
760         send_footer()
761
762     def send_raw(self, mimetype='text/plain', args=[]):
763         if not self.can_read():
764             send_title(None, msg_text='Read access denied by ACLs', msg_type='notice', mtime=self._mtime())
765             return
766
767         emit_header(self._mtime(), mimetype)
768         if 'maxwidth' in args:
769             import subprocess
770             sys.stdout.flush()
771             subprocess.check_call(['gm', 'convert', self._filename(),
772                 '-scale', args['maxwidth'].value + ' >', '-'])
773         else:
774             body = self.get_raw_body()
775             print(body)
776
777     def _write_file(self, data):
778         tmp_filename = self._tmp_filename()
779         open(tmp_filename, 'wb').write(data)
780         name = self._filename()
781         if os.name == 'nt':
782             # Bad Bill!  POSIX rename ought to replace. :-(
783             try:
784                 os.remove(name)
785             except OSError, err:
786                 if err.errno != errno.ENOENT: raise err
787         path = os.path.split(name)[0]
788         if path and not os.path.exists(path):
789             os.makedirs(path)
790         os.rename(tmp_filename, name)
791
792     def save(self, newdata, changelog):
793         if not self.can_write():
794             self.msg_text = 'Write access denied by ACLs'
795             self.msg_type = 'error'
796             return
797
798         self._write_file(newdata)
799         rc = 0
800         if config_get('post_edit_hook'):
801             import subprocess
802             cmd = [
803                 config_get('post_edit_hook'),
804                 self.page_name, remote_user(),
805                 remote_host(), changelog ]
806             child = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True)
807             output = child.stdout.read()
808             rc = child.wait()
809         if rc:
810             self.msg_text += "Post-editing hook returned %d. Command was:\n'%s'\n" % (rc, "' '".join(cmd))
811             if output:
812                 self.msg_text += 'Output follows:\n' + output
813         else:
814             self.msg_text = 'Thank you for your contribution.  Your attention to detail is appreciated.'
815             self.msg_type = 'success'
816
817 try:
818     exec(open("geekigeeki.conf.py").read())
819     os.chdir(config_get('data_dir', 'data'))
820     form = cgi.FieldStorage()
821     action = form.getvalue('a', 'get')
822     handler = globals().get('handle_' + action)
823     if handler:
824         handler(query_string(), form)
825     else:
826         send_httperror("403 Forbidden", query_string())
827
828 except Exception:
829     import traceback
830     msg_text = traceback.format_exc()
831     if title_done:
832         send_guru(msg_text, "error")
833     else:
834         send_title(None, msg_text=msg_text)
835     send_footer()
836
837 sys.stdout.flush()