From 8bfb01a3223d8ab205a28968703831fc17fba04a Mon Sep 17 00:00:00 2001 From: Bernie Innocenti Date: Mon, 28 Jul 2008 12:30:44 +0545 Subject: [PATCH] Implement directory listing --- geekigeeki.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/geekigeeki.py b/geekigeeki.py index a81ac72..854c715 100755 --- a/geekigeeki.py +++ b/geekigeeki.py @@ -3,7 +3,7 @@ # # Copyright 1999, 2000 Martin Pool # Copyright 2002 Gerardo Poggiali -# Copyright 2007, 2008 Bernardo Innocenti +# Copyright 2007, 2008 Bernie Innocenti # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -248,8 +248,8 @@ def make_index_key(): links = map(lambda ch: '%s' % (ch, ch), 'abcdefghijklmnopqrstuvwxyz') return '

'+ ' | '.join(links) + '

' -def page_list(): - return filter(word_re.match, os.listdir(data_dir)) +def page_list(dir = None, re = word_re): + return filter(re.match, os.listdir(dir or data_dir)) def send_footer(name, mod_string=None): if globals().get('debug_cgi', False): @@ -467,7 +467,7 @@ class WikiFormatter: + r"|(?P[<>&])" # Auto links - + r"|(?P\b[a-zA-Z0-9_-]+\.(png|gif|jpg|jpeg|bmp))" # LEGACY + + r"|(?P\b[a-zA-Z0-9_/-]+\.(png|gif|jpg|jpeg|bmp))" # LEGACY + r"|(?P\b(?:[A-Z][a-z]+){2,}\b)" # LEGACY + r"|(?P(http|https|ftp|mailto)\:[^\s'\"]+\S)" # LEGACY + r"|(?P[-\w._+]+\@[\w.-]+)" # LEGACY @@ -527,7 +527,7 @@ class Page: # look for the end of words and the start of a new word and insert a space there return re.sub('([a-z])([A-Z])', r'\1 \2', self.page_name) - def _text_filename(self): + def _filename(self): return path.join(data_dir, self.page_name) def _tmp_filename(self): @@ -535,7 +535,7 @@ class Page: def exists(self): try: - os.stat(self._text_filename()) + os.stat(self._filename()) return True except OSError, er: if er.errno == errno.ENOENT: @@ -551,20 +551,28 @@ class Page: def get_raw_body(self): try: - return open(self._text_filename(), 'rb').read() + return open(self._filename(), 'rb').read() except IOError, er: if er.errno == errno.ENOENT: return '' # just doesn't exist, use default if er.errno == errno.EISDIR: - return 'DIR' + return self.format_dir() raise er + def format_dir(self): + out = '' + for file in page_list(self._filename(), file_re): + if img_re.match(file): + out += ' * {{' + self.page_name + '/' + file + '}}\n' + else: + out += ' * [[' + self.page_name + '/' + file + ']]\n' + return out def get_attrs(self): if 'attrs' in self.__dict__: return self.attrs self.attrs = {} try: - file = open(self._text_filename(), 'rt') + file = open(self._filename(), 'rt') attr_re = re.compile(r"^#(\S*)(.*)$") for line in file: m = attr_re.match(line) @@ -623,7 +631,7 @@ class Page: def _last_modified(self): try: from time import localtime, strftime - modtime = localtime(os.stat(self._text_filename())[stat.ST_MTIME]) + modtime = localtime(os.stat(self._filename())[stat.ST_MTIME]) except OSError, er: if er.errno != errno.ENOENT: raise er @@ -674,7 +682,7 @@ class Page: def _write_file(self, data): tmp_filename = self._tmp_filename() open(tmp_filename, 'wb').write(data) - name = self._text_filename() + name = self._filename() if os.name == 'nt': # Bad Bill! POSIX rename ought to replace. :-( try: -- 2.25.1