~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/macro/ImageLink.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mfrom: (0.9.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080622211713-fpo2zrq3s5dfecxg
Tags: 1.7.0-3
Simplify /etc/moin/wikilist format: "USER URL" (drop unneeded middle
CONFIG_DIR that was wrongly advertised as DATA_DIR).  Make
moin-mass-migrate handle both formats and warn about deprecation of
the old one.

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
 
      Reimar Bauer
84
 
             2006-09-22 bug fix of image linked to attachment and inline
85
 
             2006-10-08 patch of DavidLinke added and keys now only lowercase used
86
 
 
87
 
    @copyright: 2001 by Jeff Kunce,
88
 
                2004 by Marcin Zalewski,
89
 
                2004-2006 by Reimar Bauer (R.Bauer@fz-juelich.de),
90
 
                2006 by Thomas Waldmann
91
 
    @license: GNU GPL, see COPYING for details.
92
 
"""
93
 
 
94
 
import os
95
 
from MoinMoin import wikiutil, config
96
 
from MoinMoin.action import AttachFile
97
 
 
98
 
kwAllowed = ['width', 'height', 'alt']
99
 
 
100
 
def _is_URL(text):
101
 
    """ Answer true if text is an URL.
102
 
        The method used here is pretty dumb. Improvements are welcome.
103
 
    """
104
 
    return '://' in text
105
 
 
106
 
def execute(macro, args):
107
 
    request = macro.request
108
 
    _ = request.getText
109
 
    formatter = macro.formatter
110
 
    if args:
111
 
        args = args.split(',')
112
 
        args = [arg.strip() for arg in args]
113
 
    else:
114
 
        args = []
115
 
 
116
 
    argc = len(args)
117
 
    kw_count = 0
118
 
    kw = {} # create a dictionary for the formatter.image call
119
 
    for arg in args :
120
 
        if '=' in arg:
121
 
            key, value = arg.split('=', 1)
122
 
            # avoid that urls with "=" are interpreted as keyword
123
 
            if key.lower() not in kwAllowed: continue
124
 
            kw_count += 1
125
 
            kw[str(key.lower())] = wikiutil.escape(value, quote=1)
126
 
 
127
 
    argc -= kw_count
128
 
    if not argc or argc and not args[0]:
129
 
        msg = 'Not enough arguments to ImageLink macro! e.g. [[ImageLink(example.png, WikiName, width=200)]].'
130
 
        return "%s%s%s" % (formatter.sysmsg(1), formatter.text(msg), formatter.sysmsg(0))
131
 
 
132
 
    image = args[0]
133
 
    if argc >= 2 and args[1]:
134
 
        target = args[1]
135
 
        if target.startswith('attachment:') or target.startswith('inline:'):
136
 
            if target.startswith('attachment:'):
137
 
                target = (target.split('attachment:'))[1]
138
 
                pagename, attname = AttachFile.absoluteName(target, formatter.page.page_name)
139
 
                target = AttachFile.getAttachUrl(pagename, target, request)
140
 
            elif target.startswith('inline:'):
141
 
                target = (target.split('inline:'))[1]
142
 
                pagename, attname = AttachFile.absoluteName(target, formatter.page.page_name)
143
 
                target = AttachFile.getAttachUrl(pagename, target, request, do='view')
144
 
 
145
 
            attachment_fname = AttachFile.getFilename(request, pagename, attname)
146
 
            if not os.path.exists(attachment_fname):
147
 
                linktext = _('Upload new attachment "%(filename)s"')
148
 
                return wikiutil.link_tag(request,
149
 
                                         ('%s?action=AttachFile&rename=%s' % (
150
 
                                         wikiutil.quoteWikinameURL(pagename),
151
 
                                         wikiutil.url_quote_plus(attname))),
152
 
                                         linktext % {'filename': attname})
153
 
 
154
 
            kw['src'] = AttachFile.getAttachUrl(pagename, image, request)
155
 
 
156
 
    elif argc == 1:
157
 
        pagename, attname = AttachFile.absoluteName(image, formatter.page.page_name)
158
 
        target = AttachFile.getAttachUrl(pagename, image, request)
159
 
    else:
160
 
        target = None
161
 
        
162
 
    if _is_URL(image):
163
 
        kw['src'] = image
164
 
    else:
165
 
        pagename, attname = AttachFile.absoluteName(image, formatter.page.page_name)
166
 
        kw['src'] = AttachFile.getAttachUrl(pagename, attname, request)
167
 
        attachment_fname = AttachFile.getFilename(request, pagename, attname)
168
 
        if not os.path.exists(attachment_fname):
169
 
            linktext = _('Upload new attachment "%(filename)s"')
170
 
            return wikiutil.link_tag(request,
171
 
                                     ('%s?action=AttachFile&rename=%s' % (
172
 
                                         wikiutil.quoteWikinameURL(pagename),
173
 
                                         wikiutil.url_quote_plus(attname))),
174
 
                                         linktext % {'filename': attname})
175
 
 
176
 
    if not kw.has_key('alt'):
177
 
        if target is None or _is_URL(target):
178
 
            if _is_URL(image):
179
 
                # Get image name http://here.com/dir/image.png -> image.png
180
 
                kw['alt'] = wikiutil.taintfilename(formatter.text(image.split('/')[-1])) # XXX
181
 
            else:
182
 
                kw['alt'] = attname
183
 
        else:
184
 
            kw['alt'] = target
185
 
 
186
 
    if target is None:
187
 
        target = kw['src']
188
 
       
189
 
    if argc == 1:
190
 
        return "%s%s%s" % (formatter.url(1, kw['src']),
191
 
                           formatter.image(**kw),
192
 
                           formatter.url(0))    
193
 
 
194
 
    if _is_URL(target) or 'action=AttachFile&do=get&target=' in target or 'action=AttachFile&do=view&target=' in target:
195
 
        return "%s%s%s" % (formatter.url(1, target),
196
 
                           formatter.image(**kw),
197
 
                           formatter.url(0))
198
 
    else:
199
 
        return "%s%s%s" % (formatter.pagelink(1, target),
200
 
                           formatter.image(**kw),
201
 
                           formatter.pagelink(0))
202