1 # -*- coding: iso-8859-1 -*-
3 MoinMoin - ImageMap Parser
5 This parser is used to create clickable image maps.
10 picsrc[;width=".."][;height=".."][;alt=".."][;title=".."]
11 link_area1;shape="rect|circle|poly";coords="..";alt=".."[;title=".."]
12 link_area2;shape="rect|circle|poly";coords="..";alt=".."[;title=".."]
15 For a detailed explanation of the syntax see:
16 http://moinmoin.wikiwikiweb.de/ParserMarket/ImageMap
18 Please note: Image maps are not accessible by blind people. Do also have a
19 look at the best-practise-examples for using image maps
20 on http://moinmoin.wikiwikiweb.de/ParserMarket/ImageMap.
25 picture.jpg;width="345";height="312";alt="Clickable Organizational Chart"
26 FrontPage;shape="rect";coords="11,10,59,29";alt="Area1"
27 http://www.xyz.com/;shape="circle";coords="42,36,96";alt="Area2"
28 FrontPage/SubPage;shape="poly";coords="48,311,105,248,96,210";alt="Area3"
29 Another Site in This Wiki;shape="rect";coords="88,10,59,29";alt="Area4"
30 InterWiki:RemotePage;shape="rect";coords="181,120,59,29";alt="Area5"
33 ImageMap Parser is partly based on ImageLink Macro
35 @copyright: 2001 by Jeff Kunce,
36 2004 by Marcin Zalewski,
37 2004-2006 by Reimar Bauer,
38 2006 by Thomas Waldmann
39 @license: GNU GPL, see COPYING for details.
42 @copyright: 2006,2007 by Oliver Siemoneit
43 @license: GNU GPL, see COPYING for details.
48 * Made code PEP8 compatible.
49 * Parameter checking and stripping added to prevent inserting of malicious
50 code in html page via parser call.
53 * Fixed ouput abstraction violations: on other formatters than html-formatter
54 just the specified image is output via formatter.image and map information
56 * In case of missing alt texts: "alt" ist set to alt="" (for the whole map)
57 and to alt="area_url" (for the different clickable areas).
58 * Now also "title" supported to generate tooltips for the map areas.
59 * Interwiki links can also be specified in "wiki:MoinMoin/Page" syntax now.
64 from MoinMoin import wikiutil, config
65 from MoinMoin.action import AttachFile
71 def _is_InterWiki(text):
74 def _is_allowed_Para(para, allowed_paras):
76 for p in allowed_paras:
77 if para.startswith(p):
81 def _strip_Para(para):
82 _para = wikiutil.escape(para)
83 if para.count('"') < 2:
85 shortend_para = _para[0:_para.find('"')+1]
86 cut_para = _para[_para.find('"')+1:len(_para)]
87 shortend_para += cut_para[0:cut_para.find('"')+1]
93 def __init__(self, raw, request, **kw):
95 self.request = request
97 def format(self, formatter):
98 request = self.request
100 row = self.raw.split('\n')
102 # Produce <img ...> html-code stuff
103 paras = row[0].split(';')
104 image = wikiutil.escape(paras[0])
105 mapname = '%s_%s' % ([image, image[:15]][(len(image) > 14)], str(random.randint(1, 999999)))
109 elif _is_InterWiki(image):
110 if image.startswith('wiki:'):
112 wikitag, wikiurl, wikitail, err = wikiutil.resolve_wiki(request, image)
113 imgurl = wikiutil.join_wiki(wikiurl, wikitail)
115 pagename, attname = AttachFile.absoluteName(image, formatter.page.page_name)
116 imgurl = AttachFile.getAttachUrl(pagename, attname, request)
117 attachment_fname = AttachFile.getFilename(request, pagename, attname)
119 if not os.path.exists(attachment_fname):
120 linktext = _('Upload new attachment "%(filename)s"')
121 output = wikiutil.link_tag(request,
122 ('%s?action=AttachFile&rename=%s' % (
123 wikiutil.quoteWikinameURL(pagename),
124 wikiutil.url_quote_plus(attname))),
125 text=linktext % {'filename': attname},
127 request.write(output)
131 <img src="%s"''' % imgurl
137 # Prevent attacks like: pic.png;height="10" onmouseover="ExecuteBadCode()";alt="..";
138 # and: pic.png;height="10" onmouseover="ExecuteBadCode()";alt="..";
139 # and: pic.png;height="10" onmouseover="ExecuteBadCode()";alt="..";
141 if _is_allowed_Para(p, ['width="', 'height="', 'alt="', 'title="']):
143 # Prepare dict for formatter.image if formatter.rawHTML call fails
144 key, value = p.split('=', 1)
145 kw[str(key.lower())] = value.strip('"')
147 # If there is no alt provided, create one
152 html += ' usemap="#%s"> ' % mapname
155 # Produce <map ..> html-code stuff
157 <map name="%s">''' % mapname
161 paras[0] = wikiutil.escape(paras[0])
163 if _is_URL(paras[0]):
165 elif _is_InterWiki(paras[0]):
166 if paras[0].startswith('wiki:'):
167 paras[0] = paras[0][5:]
168 wikitag, wikiurl, wikitail, err = wikiutil.resolve_wiki(request, paras[0])
169 area_url = wikiutil.join_wiki(wikiurl, wikitail)
171 area_url = wikiutil.quoteWikinameURL(paras[0])
175 <area href="%s"''' % area_url
178 # Prevent attacks like: FrontPage;shape="rect" onmouseover="ExecuteBadCode()";coords="..";
179 # and: FrontPage;shape="rect" onmouseover="ExecuteBadCode()";coords="..";
180 # and: FrontPage;shape="rect" onmouseover="ExecuteBadCode()";coords="..";
182 if _is_allowed_Para(i, ['shape="', 'coords="', 'alt="', 'title="']):
184 # If there is no alt provided at all, set alt to area_url
185 if p.lower().find('alt="') == -1:
186 html += ' alt="%s"' % area_url
193 # If current formatter is a HTML formatter, output image map with formatter.rawHTML().
194 # Otherwise just output image with formatter.image()
196 request.write(formatter.rawHTML(html))
198 request.write(formatter.image(**kw))