Drop old image embedding syntax.
[geekigeeki.git] / geekigeeki.py
index dba75a28afe2493b8cf42f06ac92b5614a630d85..d7e1a5a9e0d28a466e8f40b5704df5d4ea77148f 100755 (executable)
@@ -345,27 +345,34 @@ class WikiFormatter:
     def _hurl_repl(self, word):
         m = link_re.match(word)
         name = m.group(1)
-        descr = m.group(2) or name
-
-        if img_re.match(name):
-            # DEPRECATED SYNTAX: use {{foo.jpg|descr}} instead
-            _inl_repl(self, word)
+        if m.group(2) is None:
+            descr = name
+        elif img_re.match(m.group(2)):
+            descr = '<img border="0" src="' + descr + '" />'
         else:
-            if img_re.match(descr):
-                descr = '<img border="0" src="' + descr + '" />'
+            descr = m.group(2)
 
-            return link_tag(name, descr, 'wikilink')
+        return link_tag(name, descr, 'wikilink')
 
     def _inl_repl(self, word):
         m = link_re.match(word)
         name = m.group(1)
         descr = m.group(2) or name
         name = relative_url(name)
+        argv = descr.split('|')
+        descr = argv.pop(0)
+
+        if argv:
+            args = '?' + '&amp;'.join(argv)
+        else:
+            args = ''
+
         if descr:
             # The "extthumb" nonsense works around a limitation of the HTML block model
-            return '<div class="extthumb"><div class="thumb"><a href="%s"><img border="0" src="%s" alt="%s" /></a><div class="caption">%s</div></div></div>' % (name, name, descr, descr)
+            return '<div class="extthumb"><div class="thumb"><a href="%s"><img border="0" src="%s" alt="%s" /></a><div class="caption">%s</div></div></div>' \
+                    % (name, name + args, descr, descr)
         else:
-            return '<a href="%s"><img border="0" src="%s" /></a>' % (name, name)
+            return '<a href="%s"><img border="0" src="%s" /></a>' % (name, name + args)
 
     def _email_repl(self, word):
         return '<a href="mailto:%s">%s</a>' % (word, word)
@@ -571,10 +578,18 @@ class Page:
             raise er
 
     def format_dir(self):
-        out = ''
+        out = '== '
+        path = ''
+        for dir in self.page_name.split('/'):
+            path = (path + '/' + dir) if path else dir
+            out += '[[' + path + '|' + dir + ']]/'
+        out += ' ==\n'
         for file in page_list(self._filename(), file_re):
             if img_re.match(file):
-                out += ' * {{' + self.page_name + '/' + file + '}}\n'
+                if image_maxwidth:
+                    maxwidth_arg = '|maxwidth=' + str(image_maxwidth)
+                out += '{{' + self.page_name + '/' + file + '|' + file + maxwidth_arg + '}}\n'
             else:
                 out += ' * [[' + self.page_name + '/' + file + ']]\n'
         return out
@@ -690,6 +705,16 @@ class Page:
         else:
             send_title(None, msg_text='Read access denied by ACLs', msg_type='notice')
 
+    def send_image(self, mimetype, args=[]):
+        if 'maxwidth' in args:
+            import subprocess
+            emit_header(mimetype)
+            sys.stdout.flush()
+            subprocess.check_call(['gm', 'convert', self._filename(),
+                '-scale', args['maxwidth'].value + ' >', '-'])
+        else:
+            self.send_raw(mimetype)
+
     def _write_file(self, data):
         tmp_filename = self._tmp_filename()
         open(tmp_filename, 'wb').write(data)
@@ -752,8 +777,15 @@ try:
             else:
                 from mimetypes import MimeTypes
                 type, encoding = MimeTypes().guess_type(query)
-                type = type or 'text/plain'
-                Page(query).send_raw(mimetype=type)
+                #type = type or 'text/plain'
+                #Page(query).send_raw(mimetype=type)
+                if type:
+                    if type.startswith('image/'):
+                        Page(query).send_image(mimetype=type,args=form)
+                    else:
+                        Page(query).send_raw(mimetype=type)
+                else:
+                    Page(query).format()
         else:
             print "Status: 404 Not Found"
             send_title(None, msg_text='Can\'t work out query: ' + query)