diff MoinMoin/formatter/text_html.py @ 4476:f8e4e84525e8

streamline attachment_drawing formatter behaviour Make it behave more similar to attachment_image: - show grey clip image when there is no drawing - support transclusion params (like height, width, ...) Fix creole parser's usage (use given description as alt text).
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 05 Jan 2009 23:23:54 +0100
parents cb22f4c4a0fd
children 4d95fa3b074d af211e2f132c
line wrap: on
line diff
--- a/MoinMoin/formatter/text_html.py	Mon Dec 29 17:07:15 2008 +0100
+++ b/MoinMoin/formatter/text_html.py	Mon Jan 05 23:23:54 2009 +0100
@@ -652,6 +652,7 @@
             return self.url(1, target, css=css, title=title) + img + self.url(0)
 
     def attachment_drawing(self, url, text, **kw):
+        # XXX text arg is unused!
         _ = self.request.getText
         pagename, filename = AttachFile.absoluteName(url, self.page.page_name)
         fname = wikiutil.taintfilename(filename)
@@ -670,42 +671,42 @@
         # check whether attachment exists, possibly point to upload form
         drawing_url = AttachFile.getAttachUrl(pagename, fname, self.request, drawing=drawing, upload=True)
         if not exists:
-            linktext = _('Create new drawing "%(filename)s (opens in new window)"')
-            return (self.url(1, drawing_url) +
-                    self.text(linktext % {'filename': fname}) +
-                    self.url(0))
+            title = _('Create new drawing "%(filename)s (opens in new window)"') % {'filename': fname}
+            img = self.icon('attachimg')  # TODO: we need a new "drawimg" in similar grey style and size
+            css = 'nonexistent'
+            return self.url(1, drawing_url, css=css, title=title) + img + self.url(0)
+
+        title = _('Edit drawing %(filename)s (opens in new window)') % {'filename': self.text(fname)}
+        kw['src'] = AttachFile.getAttachUrl(pagename, filename, self.request, addts=1)
+        kw['css'] = 'drawing'
 
         mappath = AttachFile.getFilename(self.request, pagename, drawing + u'.map')
-
-        # check for map file
-        if os.path.exists(mappath):
+        try:
+            map = file(mappath, 'r').read()
+        except (IOError, OSError):
+            map = ''
+        if map:
             # we have a image map. inline it and add a map ref to the img tag
-            try:
-                map = file(mappath, 'r').read()
-            except IOError:
-                pass
-            except OSError:
-                pass
-            else:
-                mapid = 'ImageMapOf' + drawing
-                # replace MAPNAME
-                map = map.replace('%MAPNAME%', mapid)
-                # add alt and title tags to areas
-                map = re.sub('href\s*=\s*"((?!%TWIKIDRAW%).+?)"', r'href="\1" alt="\1" title="\1"', map)
-                # add in edit links plus alt and title attributes
-                alt = title = _('Edit drawing %(filename)s (opens in new window)') % {'filename': self.text(fname)}
-                map = map.replace('%TWIKIDRAW%"', '%s" alt="%s" title="%s"' % (drawing_url, alt, title))
-                # unxml, because 4.01 concrete will not validate />
-                map = map.replace('/>', '>')
-                alt = title = _('Clickable drawing: %(filename)s') % {'filename': self.text(fname)}
-                src = AttachFile.getAttachUrl(pagename, filename, self.request, addts=1)
-                return (map + self.image(alt=alt, title=title, src=src, usemap='#'+mapid, css="drawing"))
+            mapid = 'ImageMapOf' + drawing
+            map = map.replace('%MAPNAME%', mapid)
+            # add alt and title tags to areas
+            map = re.sub('href\s*=\s*"((?!%TWIKIDRAW%).+?)"', r'href="\1" alt="\1" title="\1"', map)
+            map = map.replace('%TWIKIDRAW%"', '%s" alt="%s" title="%s"' % (drawing_url, title, title))
+            # unxml, because 4.01 concrete will not validate />
+            map = map.replace('/>', '>')
+            title = _('Clickable drawing: %(filename)s') % {'filename': self.text(fname)}
+            if 'title' not in kw:
+                kw['title'] = title
+            if 'alt' not in kw:
+                kw['alt'] = kw['title']
+            kw['usemap'] = '#'+mapid
+            return map + self.image(**kw)
         else:
-            alt = title = _('Edit drawing %(filename)s (opens in new window)') % {'filename': self.text(fname)}
-            src = AttachFile.getAttachUrl(pagename, filename, self.request, addts=1)
-            return (self.url(1, drawing_url) +
-                    self.image(alt=alt, title=title, src=src, css="drawing") +
-                    self.url(0))
+            if 'title' not in kw:
+                kw['title'] = title
+            if 'alt' not in kw:
+                kw['alt'] = kw['title']
+            return self.url(1, drawing_url) + self.image(**kw) + self.url(0)
 
 
     # Text ##############################################################