Attachment 'Link.py'
Download 1 # -*- coding: utf-8 -*-
2 """
3 MoinMoin - Link macro
4
5 This macro create localized links with extra html attributes. This could
6 be useful to add access keys to your main wiki pages for better
7 usability and accessability.
8
9 Creating a link to localized page:
10
11 [[Link(FrontPage)]]
12
13 Same with accesskey:
14
15 [[Link(FrontPage, accesskey="1")]]
16
17 Same with special class:
18
19 [[Link(FrontPage, accesskey="1" class="special")]]
20
21 The second argument is rendered as is in the a tag. You can add any
22 html attribtues in any order.
23
24 @copyright: 2005 by Nir Soffer <nirs@freeshell.org>
25 @license: GNU GPL, see COPYING for details.
26 """
27
28 Dependencies = ["language", "namespace"]
29
30 class Link:
31 """ Localized link with html attributes """
32
33 arguments = ['name', 'attributes']
34
35 def __init__(self, macro, args):
36 self.macro = macro
37 self.request = macro.request
38 self.args = self.parseArgs(args)
39
40 def parseArgs(self, string):
41 """ Temporary function until Oliver Graf args parser is finished
42
43 @param string: string from the wiki markup [[NewPage(string)]]
44 @rtype: dict
45 @return: dictionary with macro options
46 """
47 if not string:
48 return {}
49 args = [s.strip() for s in string.split(',')]
50 args = dict(zip(self.arguments, args))
51 return args
52
53 def errors(self):
54 """ Validate arguments and return error message
55
56 @rtype: unicode
57 @return: error message for bad argument, or None if ok.
58 """
59 _ = self.request.getText
60
61 # Must have non empty page name
62 if not self.args.get('name'):
63 error = _('Missing page name.')
64 # Should use abstract formatter.wikiMarkupError() call,
65 # but there is no such call, so just use html.
66 return u'<span class="error">%s</span>' % error
67
68 return None
69
70 def name(self):
71 """ Return localized name safe for rendering """
72 name = self.args['name']
73 localized = self.macro.request.getText(name, formatted=False)
74
75 # If we got same text, it means there was no translation, and this
76 # is unsafe user text that must be escaped.
77 if name == localized:
78 localized = self.macro.formatter.text(localized)
79
80 return localized
81
82 def attributes(self):
83 """ Return optional link attributes, safe for rendering """
84 attributes = self.args.get('attributes', u'')
85 if attributes:
86 attributes = self.macro.formatter.text(attributes)
87
88 return attributes
89
90 def renderInText(self):
91 """ Render macro in paragraph context
92
93 The parser should decide what to do if this macro is placed in a
94 page context.
95
96 @rtype: unicode
97 @return rendered output
98 """
99 errors = self.errors()
100 if errors:
101 return errors
102
103 attributes = self.attributes()
104 name = self.name()
105
106 # TODO: should use the formatter to create output that works on
107 # any format, but formatter.url ignore 'attrs' keyword sent by
108 # wikiutil.link_tag. Untill it is fixed, use duplicate code that
109 # mostly works.
110 # Note: this page link will not be cached.
111 from MoinMoin.Page import Page
112 if not Page(self.request, name).exists():
113 attributes += ' class="nonexistent"'
114
115 out = u'<a href="%(script)s/%(name)s"%(attr)s>%(name)s</a>' % {
116 'attr': attributes,
117 'script': self.request.getScriptname(),
118 'name': name,
119 }
120 return out
121
122 def execute(macro, args):
123 """ Temporary glue code to use with moin current macro system """
124 return Link(macro, args).renderInText()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.