~ubuntu-branches/ubuntu/intrepid/moin/intrepid-updates

« back to all changes in this revision

Viewing changes to MoinMoin/macro/ImageLink.py

  • Committer: Bazaar Package Importer
  • Author(s): Sivan Greenberg
  • Date: 2006-07-09 19:28:02 UTC
  • Revision ID: james.westby@ubuntu.com-20060709192802-oaeuvt4v3e9300uj
Tags: 1.5.3-1ubuntu1
* Merge new debian version.
* Reapply Ubuntu changes:
    + debian/rules:
      - Comment out usage of control.ubuntu.in (doesn't fit!).
    + debian/control.in:
      - Dropped python2.3 binary package.
    + debian/control:
      - Dropped python2.3 binary, again.
      - Dropped python2.3-dev from Build-Depends-Indep.
    + debian/patches/001-attachment-xss-fix.patch:
      - Dropped this patch. It's now in upstream's distribution.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: iso-8859-1 -*-
 
2
"""
 
3
    MoinMoin - ImageLink Macro
 
4
 
 
5
    This macro is used to make a link that displays an image (can be given
 
6
    as either attachment or URL) and links to either an URL or a wiki page.
 
7
    Optionally the size of the image can be adjusted.
 
8
    If no target is given the link will point to the image itself.
 
9
    
 
10
    Syntax:
 
11
        [[ImageLink(image, [target,] [width=width, [height=height]])]]
 
12
 
 
13
    Parameters:
 
14
        image: image attachment file name or the URL of an image
 
15
        target: link target wiki page or URL (optional)
 
16
 
 
17
    Keyword Parameters:
 
18
        width: rendered image width (optional)
 
19
        height: rendered image heigth (optional)
 
20
        alt: text for img tag "alt" attribute
 
21
 
 
22
    Examples:
 
23
        [[ImageLink(M�nchen.png,M�nchen,height=100)]]
 
24
        [[ImageLink(Images/M�nchen.png,M�nchen,height=100)]]
 
25
        [[ImageLink(http://webcam.portalmuc.de/images/webcam/webcam_marienplatz.jpg,M�nchen Marienplatz)]]
 
26
        [[ImageLink(plot.png,width=200)]]
 
27
        [[ImageLink(plot.png,height=200)]]
 
28
        [[ImageLink(plot.png)]]
 
29
        [[ImageLink(http://webcam.portalmuc.de/images/webcam/webcam_marienplatz.jpg,http://www.muenchen.de,width=150)]]
 
30
        [[ImageLink(m�nchen.png,http://www.muenchen.de,width=50)]]
 
31
        [[ImageLink(http://webcam.portalmuc.de/images/webcam/webcam_marienplatz.jpg)]]
 
32
        [[ImageLink(example.png,alt=whateveryouwant(���))]]
 
33
 
 
34
    History:
 
35
        Jeff Kunce: 
 
36
            wrote the first published version of this macro in 2001.
 
37
        
 
38
        Reimar Bauer:
 
39
            2004 intitial new version for MoinMoin 1.2
 
40
        
 
41
        Marcin Zalewski:   
 
42
            Implemented wikiutil.link_tag and macro.formatter.pagelink.
 
43
            Added title attribute to the created link. One could generalize that to
 
44
            add arbitrary attributes.
 
45
 
 
46
            One could also add class attributes to <a> and/or <img> elements.
 
47
            I do not see the need for such modifications. If however this is
 
48
            to be done in the future one would need to add 'html_class' key to the kw dictionary
 
49
            with a corresponding value to add class to <img> element. To add class to <a> element
 
50
            one needs to add 'css_class' key with a corresponding value to the dictionary passed to
 
51
            pagelink call.
 
52
        
 
53
       Reimar Bauer:    
 
54
            2004-12-23 Adapted to MoinMoin Version 1.3.1-1
 
55
            2004-12-23 Syntax change Version 1.3.1-2
 
56
                   width and height and probably other keywords must be given as keywords (e.g. height=20)
 
57
            2004-12-31 Version 1.3.1-3 code clean up
 
58
            2005-01-16 Bug fixed in the errorhandler - found and patched by Malte Helmert
 
59
            2005-03-05 Version 1.3.3-5 Bug fixed found by cypress ("If I put [[ImageLink(moinmoin.png)]] it bombs")
 
60
            2005-03-28 Version 1.3.3-6 feature request added by CDPark:
 
61
                       "Can we use an external image? And an external target?"
 
62
            2005-04-16 Version 1.3.3-7 no default alt tag definition as requested by CDPark and AlexanderSchremmer
 
63
       
 
64
       Chong-Dae Park:
 
65
            2005-04-17 Version 1.3.3-8 code refactored
 
66
                       IMG with no alt tag is not recommended in the HTML standard.
 
67
                       It keeps a user specified alt tag. If there is none, it tries to make on using the WikiName
 
68
                       or image name instead.
 
69
       
 
70
       Reimar Bauer:
 
71
            2005-04-21 Version 1.3.3-9 bug fixed
 
72
                       When the image does not exist yet, it gives you a "Upload Image" link, this link does not
 
73
                       work. I suspect that this only is a problem on sub-pages, caused by incorrect escaping of
 
74
                        "/". -- CraigJohnson
 
75
 
 
76
            2005-12-19 Versiom 1.5.0-10 feature added to link to images on different wiki pages
 
77
            2006-02-14 Version 1.5.2-11 bug fixed for filename of attached image is Chinese (encode added)
 
78
            2006-02-22 Version 1.5.2-12 code refactored
 
79
 
 
80
      Thomas Waldmann
 
81
            2006-03-10 code refactored
 
82
 
 
83
    @copyright: 2001 by Jeff Kunce,
 
84
                2004 by Marcin Zalewski,
 
85
                2004-2006 by Reimar Bauer (R.Bauer@fz-juelich.de),
 
86
                2006 by Thomas Waldmann
 
87
    @license: GNU GPL, see COPYING for details.
 
88
"""
 
89
 
 
90
import os
 
91
from MoinMoin import wikiutil, config
 
92
from MoinMoin.action import AttachFile
 
93
 
 
94
def _is_URL(text):
 
95
    """ Answer true if text is an URL.
 
96
        The method used here is pretty dumb. Improvements are welcome.
 
97
    """
 
98
    return '://' in text
 
99
 
 
100
def execute(macro, args):
 
101
    request = macro.request
 
102
    _ = request.getText
 
103
    formatter = macro.formatter
 
104
    if args:
 
105
        args = args.split(',')
 
106
        args = [arg.strip() for arg in args]
 
107
    else:
 
108
        args = []
 
109
 
 
110
    argc = len(args)
 
111
    kw_count = 0
 
112
    kw = {} # create a dictionary for the formatter.image call
 
113
    for arg in args :
 
114
        if '=' in arg:
 
115
            kw_count += 1
 
116
            key, value = arg.split('=', 1)
 
117
            kw[str(key)] = wikiutil.escape(value, quote=1)
 
118
 
 
119
    argc -= kw_count
 
120
    if not argc or argc and not args[0]:
 
121
        msg = 'Not enough arguments to ImageLink macro! e.g. [[ImageLink(example.png, WikiName, width=200)]].'
 
122
        return "%s%s%s" % (formatter.sysmsg(1), formatter.text(msg), formatter.sysmsg(0))
 
123
 
 
124
    image = args[0]
 
125
    if argc >= 2 and args[1]:
 
126
        target = args[1]
 
127
    else:
 
128
        target = None
 
129
        
 
130
    if _is_URL(image):
 
131
        kw['src'] = image
 
132
    else:
 
133
        pagename, attname = AttachFile.absoluteName(image, formatter.page.page_name)
 
134
        kw['src'] = AttachFile.getAttachUrl(pagename, attname, request)
 
135
        attachment_fname = AttachFile.getFilename(request, pagename, attname)
 
136
        if not os.path.exists(attachment_fname):
 
137
            linktext = _('Upload new attachment "%(filename)s"')
 
138
            return wikiutil.link_tag(request,
 
139
                                     ('%s?action=AttachFile&rename=%s' % (
 
140
                                         wikiutil.quoteWikinameURL(pagename),
 
141
                                         wikiutil.url_quote_plus(attname))),
 
142
                                     linktext % {'filename': attname})
 
143
 
 
144
    if not kw.has_key('alt'):
 
145
        if target is None or _is_URL(target):
 
146
            if _is_URL(image):
 
147
                # Get image name http://here.com/dir/image.png -> image.png
 
148
                kw['alt'] = wikiutil.taintfilename(formatter.text(image.split('/')[-1])) # XXX
 
149
            else:
 
150
                kw['alt'] = attname
 
151
        else:
 
152
            kw['alt'] = target
 
153
 
 
154
    if target is None:
 
155
        target = kw['src']
 
156
 
 
157
    if _is_URL(target):
 
158
        return "%s%s%s" % (formatter.url(1, target),
 
159
                           formatter.image(**kw),
 
160
                           formatter.url(0))
 
161
    else:
 
162
        return "%s%s%s" % (formatter.pagelink(1, target),
 
163
                           formatter.image(**kw),
 
164
                           formatter.pagelink(0))
 
165