annotate MoinMoin/formatter/__init__.py @ 3270:e06e15e90ba7

fix some wrong wikiutil.escape usage causing double-escaping
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 16 Mar 2008 21:04:16 +0100
parents a48929a5036c
children bfd3f600650b
rev   line source
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
2 """
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
3 MoinMoin - Formatter Package and FormatterBase
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
4
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
5 See "base.py" for the formatter interface.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
6
1918
bb2e053067fb fixing copyright headers: remove umlauts (encoding troubles), make epydoc compatible, reformat
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1889
diff changeset
7 @copyright: 2000-2004 by Juergen Hermann <jh@web.de>
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
8 @license: GNU GPL, see COPYING for details.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
9 """
1181
a7f8dceb4410 remove types module usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 999
diff changeset
10 import re
3107
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3054
diff changeset
11
3110
a48929a5036c logging: make it work correctly by doing logging configuration very early
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3107
diff changeset
12 from MoinMoin import log
a48929a5036c logging: make it work correctly by doing logging configuration very early
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3107
diff changeset
13 logging = log.getLogger(__name__)
1181
a7f8dceb4410 remove types module usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 999
diff changeset
14
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
15 from MoinMoin.util import pysupport
1181
a7f8dceb4410 remove types module usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 999
diff changeset
16 from MoinMoin import wikiutil
2761
3a6a25169f55 use rsplit for splitting off anchor
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2754
diff changeset
17 from MoinMoin.support.python_compatibility import rsplit
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
18
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
19 modules = pysupport.getPackageModules(__file__)
407
bea43f99cc27 star-merged moin--refactor--1.5 (formatter, parser, converter fixes, see docs/CHANGES.refactor)
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
20
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
21
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
22 class FormatterBase:
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
23 """ This defines the output interface used all over the rest of the code.
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
24
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
25 Note that no other means should be used to generate _content_ output,
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
26 while navigational elements (HTML page header/footer) and the like
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
27 can be printed directly without violating output abstraction.
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
28 """
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
29
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
30 hardspace = ' '
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
31
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
32 def __init__(self, request, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
33 self.request = request
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
34 self._ = request.getText
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
35
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
36 self._store_pagelinks = kw.get('store_pagelinks', 0)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
37 self._terse = kw.get('terse', 0)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
38 self.pagelinks = []
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
39 self.in_p = 0
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
40 self.in_pre = 0
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
41 self._highlight_re = None
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
42 self._base_depth = 0
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
43
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
44 def set_highlight_re(self, hi_re=None):
1181
a7f8dceb4410 remove types module usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 999
diff changeset
45 if isinstance(hi_re, (str, unicode)):
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
46 try:
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
47 self._highlight_re = re.compile(hi_re, re.U + re.IGNORECASE)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
48 except re.error:
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
49 hi_re = re.escape(hi_re)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
50 self._highlight_re = re.compile(hi_re, re.U + re.IGNORECASE)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
51 else:
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
52 self._highlight_re = hi_re
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
53
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
54 def lang(self, on, lang_name):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
55 return ""
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
56
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
57 def setPage(self, page):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
58 self.page = page
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
59
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
60 def sysmsg(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
61 """ Emit a system message (embed it into the page).
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
62
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
63 Normally used to indicate disabled options, or invalid markup.
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
64 """
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
65 return ""
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
66
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
67 # Document Level #####################################################
999
97ce857bdebf whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 907
diff changeset
68
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
69 def startDocument(self, pagename):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
70 return ""
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
71
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
72 def endDocument(self):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
73 return ""
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
74
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
75 def startContent(self, content_id="content", **kw):
2572
ce0787373150 introduce ID cache in request, make everybody generate valid IDs even inside includes
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
76 if self.page:
ce0787373150 introduce ID cache in request, make everybody generate valid IDs even inside includes
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
77 self.request.begin_include(self.page.page_name)
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
78 return ""
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
79
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
80 def endContent(self):
2572
ce0787373150 introduce ID cache in request, make everybody generate valid IDs even inside includes
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
81 if self.page:
ce0787373150 introduce ID cache in request, make everybody generate valid IDs even inside includes
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
82 self.request.end_include()
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
83 return ""
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
84
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
85 # Links ##############################################################
999
97ce857bdebf whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 907
diff changeset
86
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
87 def pagelink(self, on, pagename='', page=None, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
88 """ make a link to page <pagename>. Instead of supplying a pagename,
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
89 it is also possible to give a live Page object, then page.page_name
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
90 will be used.
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
91 """
999
97ce857bdebf whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 907
diff changeset
92 if not self._store_pagelinks or not on or kw.get('generated'):
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
93 return ''
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
94 if not pagename and page:
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
95 pagename = page.page_name
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
96 pagename = self.request.normalizePagename(pagename)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
97 if pagename and pagename not in self.pagelinks:
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
98 self.pagelinks.append(pagename)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
99
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
100 def interwikilink(self, on, interwiki='', pagename='', **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
101 """ calls pagelink() for internal interwikilinks
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
102 to make sure they get counted for self.pagelinks.
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
103 IMPORTANT: on and off must be called with same parameters, see
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
104 also the text_html formatter.
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
105 """
2728
59b3d8b8971f remove (un)quoteName, new functions split_interwiki() and resolve_interwiki(), cleanup interwiki mess, fix some link markup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2576
diff changeset
106 wikitag, wikiurl, wikitail, wikitag_bad = wikiutil.resolve_interwiki(self.request, interwiki, pagename)
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
107 if wikitag == 'Self' or wikitag == self.request.cfg.interwikiname:
1183
bc84eae93f8c replace usage of find by 'in' operator
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1181
diff changeset
108 if '#' in wikitail:
2761
3a6a25169f55 use rsplit for splitting off anchor
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2754
diff changeset
109 wikitail, kw['anchor'] = rsplit(wikitail, '#', 1)
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
110 wikitail = wikiutil.url_unquote(wikitail)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
111 return self.pagelink(on, wikitail, **kw)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
112 return ''
999
97ce857bdebf whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 907
diff changeset
113
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
114 def url(self, on, url=None, css=None, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
115 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
116
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
117 # Attachments ######################################################
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
118
2701
398af77c7ede fixed attachment_link api - it now has a 'on' parameter like most of the other methods
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2576
diff changeset
119 def attachment_link(self, on, url=None, **kw):
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
120 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
121 def attachment_image(self, url, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
122 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
123 def attachment_drawing(self, url, text, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
124 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
125
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
126 def attachment_inlined(self, url, text, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
127 from MoinMoin.action import AttachFile
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
128 import os
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
129 _ = self.request.getText
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
130 pagename, filename = AttachFile.absoluteName(url, self.page.page_name)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
131 fname = wikiutil.taintfilename(filename)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
132 fpath = AttachFile.getFilename(self.request, pagename, fname)
1805
ebcebba1afb3 removed some unused attributes, used 'dummy' for dummies
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1520
diff changeset
133 ext = os.path.splitext(filename)[1]
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
134 Parser = wikiutil.getParserForExtension(self.request.cfg, ext)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
135 if Parser is not None:
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
136 try:
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
137 content = file(fpath, 'r').read()
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
138 # Try to decode text. It might return junk, but we don't
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
139 # have enough information with attachments.
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
140 content = wikiutil.decodeUnknownInput(content)
1889
1405a38ae848 give the attachment filename to a colorizing parser
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1868
diff changeset
141 colorizer = Parser(content, self.request, filename=filename)
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
142 colorizer.format(self)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
143 except IOError:
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
144 pass
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
145
2702
458e23c1349b fix another place calling attachment_link
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2701
diff changeset
146 return (self.attachment_link(1, url) +
458e23c1349b fix another place calling attachment_link
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2701
diff changeset
147 self.text(text) +
458e23c1349b fix another place calling attachment_link
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2701
diff changeset
148 self.attachment_link(0))
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
149
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
150 def anchordef(self, name):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
151 return ""
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
152
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
153 def line_anchordef(self, lineno):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
154 return ""
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
155
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
156 def anchorlink(self, on, name='', **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
157 return ""
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
158
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
159 def line_anchorlink(self, on, lineno=0):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
160 return ""
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
161
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
162 def image(self, src=None, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
163 """An inline image.
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
164
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
165 Extra keyword arguments are according to the HTML <img> tag attributes.
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
166 In particular an 'alt' or 'title' argument should give a description
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
167 of the image.
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
168 """
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
169 title = src
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
170 for titleattr in ('title', 'html__title', 'alt', 'html__alt'):
1868
64507f46beb2 reduce usage of has_key()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1805
diff changeset
171 if titleattr in kw:
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
172 title = kw[titleattr]
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
173 break
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
174 if title:
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
175 return '[Image:%s]' % title
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
176 return '[Image]'
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
177
2713
ddb2d19684d7 add transclusion / transclusion_param formatter methods (for html: <object> / <param>)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2702
diff changeset
178 # generic transclude/include:
ddb2d19684d7 add transclusion / transclusion_param formatter methods (for html: <object> / <param>)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2702
diff changeset
179 def transclusion(self, on, **kw):
ddb2d19684d7 add transclusion / transclusion_param formatter methods (for html: <object> / <param>)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2702
diff changeset
180 raise NotImplementedError
ddb2d19684d7 add transclusion / transclusion_param formatter methods (for html: <object> / <param>)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2702
diff changeset
181 def transclusion_param(self, **kw):
ddb2d19684d7 add transclusion / transclusion_param formatter methods (for html: <object> / <param>)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2702
diff changeset
182 raise NotImplementedError
ddb2d19684d7 add transclusion / transclusion_param formatter methods (for html: <object> / <param>)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2702
diff changeset
183
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
184 def smiley(self, text):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
185 return text
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
186
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
187 def nowikiword(self, text):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
188 return self.text(text)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
189
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1920
diff changeset
190 # Text and Text Attributes ###########################################
999
97ce857bdebf whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 907
diff changeset
191
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
192 def text(self, text, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
193 if not self._highlight_re:
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
194 return self._text(text)
999
97ce857bdebf whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 907
diff changeset
195
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
196 result = []
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
197 lastpos = 0
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
198 match = self._highlight_re.search(text)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
199 while match and lastpos < len(text):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
200 # add the match we found
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
201 result.append(self._text(text[lastpos:match.start()]))
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
202 result.append(self.highlight(1))
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
203 result.append(self._text(match.group(0)))
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
204 result.append(self.highlight(0))
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
205
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
206 # search for the next one
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
207 lastpos = match.end() + (match.end() == lastpos)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
208 match = self._highlight_re.search(text, lastpos)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
209
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
210 result.append(self._text(text[lastpos:]))
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
211 return ''.join(result)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
212
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
213 def _text(self, text):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
214 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
215
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
216 def strong(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
217 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
218
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
219 def emphasis(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
220 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
221
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
222 def underline(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
223 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
224
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
225 def highlight(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
226 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
227
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
228 def sup(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
229 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
230
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
231 def sub(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
232 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
233
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
234 def strike(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
235 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
236
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
237 def code(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
238 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
239
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
240 def preformatted(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
241 self.in_pre = on != 0
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
242
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
243 def small(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
244 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
245
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
246 def big(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
247 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
248
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
249 # special markup for syntax highlighting #############################
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
250
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
251 def code_area(self, on, code_id, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
252 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
253
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
254 def code_line(self, on):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
255 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
256
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
257 def code_token(self, tok_text, tok_type):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
258 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
259
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
260 # Paragraphs, Lines, Rules ###########################################
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
261
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
262 def linebreak(self, preformatted=1):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
263 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
264
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
265 def paragraph(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
266 self.in_p = on != 0
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
267
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
268 def rule(self, size=0, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
269 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
270
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
271 def icon(self, type):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
272 return type
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
273
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
274 # Lists ##############################################################
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
275
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
276 def number_list(self, on, type=None, start=None, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
277 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
278
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
279 def bullet_list(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
280 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
281
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
282 def listitem(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
283 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
284
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
285 def definition_list(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
286 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
287
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
288 def definition_term(self, on, compact=0, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
289 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
290
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
291 def definition_desc(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
292 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
293
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
294 def heading(self, on, depth, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
295 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
296
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
297 # Tables #############################################################
999
97ce857bdebf whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 907
diff changeset
298
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
299 def table(self, on, attrs={}, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
300 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
301
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
302 def table_row(self, on, attrs={}, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
303 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
304
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
305 def table_cell(self, on, attrs={}, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
306 raise NotImplementedError
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
307
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
308 # Dynamic stuff / Plugins ############################################
999
97ce857bdebf whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 907
diff changeset
309
2779
c9dd12befda7 wiki parser: match everything looking like a macro and either execute it (macro imports ok) or render the macro markup as text (macro import fails, error msg is in title). This makes scan_rules independent of wiki, so we can move all re.compile time to Parser class (doing it only once on module import time).
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2761
diff changeset
310 def macro(self, macro_obj, name, args, markup=None):
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
311 # call the macro
2779
c9dd12befda7 wiki parser: match everything looking like a macro and either execute it (macro imports ok) or render the macro markup as text (macro import fails, error msg is in title). This makes scan_rules independent of wiki, so we can move all re.compile time to Parser class (doing it only once on module import time).
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2761
diff changeset
312 try:
c9dd12befda7 wiki parser: match everything looking like a macro and either execute it (macro imports ok) or render the macro markup as text (macro import fails, error msg is in title). This makes scan_rules independent of wiki, so we can move all re.compile time to Parser class (doing it only once on module import time).
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2761
diff changeset
313 return macro_obj.execute(name, args)
c9dd12befda7 wiki parser: match everything looking like a macro and either execute it (macro imports ok) or render the macro markup as text (macro import fails, error msg is in title). This makes scan_rules independent of wiki, so we can move all re.compile time to Parser class (doing it only once on module import time).
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2761
diff changeset
314 except ImportError, err:
c9dd12befda7 wiki parser: match everything looking like a macro and either execute it (macro imports ok) or render the macro markup as text (macro import fails, error msg is in title). This makes scan_rules independent of wiki, so we can move all re.compile time to Parser class (doing it only once on module import time).
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2761
diff changeset
315 errmsg = unicode(err)
c9dd12befda7 wiki parser: match everything looking like a macro and either execute it (macro imports ok) or render the macro markup as text (macro import fails, error msg is in title). This makes scan_rules independent of wiki, so we can move all re.compile time to Parser class (doing it only once on module import time).
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2761
diff changeset
316 if markup:
c9dd12befda7 wiki parser: match everything looking like a macro and either execute it (macro imports ok) or render the macro markup as text (macro import fails, error msg is in title). This makes scan_rules independent of wiki, so we can move all re.compile time to Parser class (doing it only once on module import time).
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2761
diff changeset
317 return (self.span(1, title=errmsg) +
c9dd12befda7 wiki parser: match everything looking like a macro and either execute it (macro imports ok) or render the macro markup as text (macro import fails, error msg is in title). This makes scan_rules independent of wiki, so we can move all re.compile time to Parser class (doing it only once on module import time).
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2761
diff changeset
318 self.text(markup) +
c9dd12befda7 wiki parser: match everything looking like a macro and either execute it (macro imports ok) or render the macro markup as text (macro import fails, error msg is in title). This makes scan_rules independent of wiki, so we can move all re.compile time to Parser class (doing it only once on module import time).
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2761
diff changeset
319 self.span(0))
c9dd12befda7 wiki parser: match everything looking like a macro and either execute it (macro imports ok) or render the macro markup as text (macro import fails, error msg is in title). This makes scan_rules independent of wiki, so we can move all re.compile time to Parser class (doing it only once on module import time).
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2761
diff changeset
320 else:
c9dd12befda7 wiki parser: match everything looking like a macro and either execute it (macro imports ok) or render the macro markup as text (macro import fails, error msg is in title). This makes scan_rules independent of wiki, so we can move all re.compile time to Parser class (doing it only once on module import time).
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2761
diff changeset
321 return self.text(errmsg)
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
322 def _get_bang_args(self, line):
1184
b1deb9496f41 use startswith/endswith
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1183
diff changeset
323 if line.startswith('#!'):
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
324 try:
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
325 name, args = line[2:].split(None, 1)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
326 except ValueError:
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
327 return ''
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
328 else:
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
329 return args
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
330 return None
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
331
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
332 def parser(self, parser_name, lines):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
333 """ parser_name MUST be valid!
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
334 writes out the result instead of returning it!
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
335 """
2572
ce0787373150 introduce ID cache in request, make everybody generate valid IDs even inside includes
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
336 # attention: this is copied into text_python!
1520
563bfe02b04f Removed a lot of ugly MimeType() calls, refactored it into a helper, fixed page rendering for missing parsers.
Alexander Schremmer <alex AT alexanderweb DOT de>
parents: 1184
diff changeset
337 parser = wikiutil.searchAndImportPlugin(self.request.cfg, "parser", parser_name)
3054
d2e9afd64276 get formatters in sync with 1.6 cs 2558:3e3a549b49f0, add attachimg icon (needed by html formatter)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2779
diff changeset
338 args = None
d2e9afd64276 get formatters in sync with 1.6 cs 2558:3e3a549b49f0, add attachimg icon (needed by html formatter)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2779
diff changeset
339 if lines:
d2e9afd64276 get formatters in sync with 1.6 cs 2558:3e3a549b49f0, add attachimg icon (needed by html formatter)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2779
diff changeset
340 args = self._get_bang_args(lines[0])
3107
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3054
diff changeset
341 logging.debug("formatter.parser: parser args %r" % args)
3054
d2e9afd64276 get formatters in sync with 1.6 cs 2558:3e3a549b49f0, add attachimg icon (needed by html formatter)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2779
diff changeset
342 if args is not None:
d2e9afd64276 get formatters in sync with 1.6 cs 2558:3e3a549b49f0, add attachimg icon (needed by html formatter)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2779
diff changeset
343 lines = lines[1:]
d2e9afd64276 get formatters in sync with 1.6 cs 2558:3e3a549b49f0, add attachimg icon (needed by html formatter)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2779
diff changeset
344 if lines and not lines[0]:
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
345 lines = lines[1:]
3054
d2e9afd64276 get formatters in sync with 1.6 cs 2558:3e3a549b49f0, add attachimg icon (needed by html formatter)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2779
diff changeset
346 if lines and not lines[-1].strip():
d2e9afd64276 get formatters in sync with 1.6 cs 2558:3e3a549b49f0, add attachimg icon (needed by html formatter)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2779
diff changeset
347 lines = lines[:-1]
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
348 p = parser('\n'.join(lines), self.request, format_args=args)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
349 p.format(self)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
350 del p
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
351 return ''
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
352
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
353 # Other ##############################################################
999
97ce857bdebf whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 907
diff changeset
354
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
355 def div(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
356 """ open/close a blocklevel division """
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
357 return ""
999
97ce857bdebf whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 907
diff changeset
358
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
359 def span(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
360 """ open/close a inline span """
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
361 return ""
999
97ce857bdebf whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 907
diff changeset
362
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
363 def rawHTML(self, markup):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
364 """ This allows emitting pre-formatted HTML markup, and should be
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
365 used wisely (i.e. very seldom).
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
366
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
367 Using this event while generating content results in unwanted
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
368 effects, like loss of markup or insertion of CDATA sections
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
369 when output goes to XML formats.
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
370 """
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
371
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
372 import formatter, htmllib
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
373 from MoinMoin.util import simpleIO
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
374
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
375 # Regenerate plain text
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
376 f = simpleIO()
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
377 h = htmllib.HTMLParser(formatter.AbstractFormatter(formatter.DumbWriter(f)))
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
378 h.feed(markup)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
379 h.close()
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
380
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
381 return self.text(f.getvalue())
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
382
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
383 def escapedText(self, on, **kw):
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
384 """ This allows emitting text as-is, anything special will
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
385 be escaped (at least in HTML, some text output format
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
386 would possibly do nothing here)
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
387 """
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
388 return ""
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
389
1920
b06ef2a53efa 'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
390 def comment(self, text, **kw):
661
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
391 return ""
19ef5f9fff84 moved formatter.base to formatter.__init__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 407
diff changeset
392
2576
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
393 # ID handling #################################################
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
394
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
395 def sanitize_to_id(self, text):
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
396 '''
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
397 Take 'text' and return something that is a valid ID
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
398 for this formatter.
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
399 The default returns the first non-space character of the string.
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
400
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
401 Because of the way this is used, it must be idempotent,
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
402 i.e. calling it on an already sanitized id must yield the
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
403 original id.
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
404 '''
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
405 return text.strip()[:1]
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
406
2572
ce0787373150 introduce ID cache in request, make everybody generate valid IDs even inside includes
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
407 def make_id_unique(self, id):
2576
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
408 '''
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
409 Take an ID and make it unique in the current namespace.
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
410 '''
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
411 ns = self.request.include_id
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
412 if not ns is None:
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
413 ns = self.sanitize_to_id(ns)
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
414 id = self.sanitize_to_id(id)
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
415 id = self.request.make_unique_id(id, ns)
2572
ce0787373150 introduce ID cache in request, make everybody generate valid IDs even inside includes
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
416 return id
2576
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
417
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
418 def qualify_id(self, id):
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
419 '''
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
420 Take an ID and return a string that is qualified by
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
421 the current namespace; this default implementation
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
422 is suitable if the dot ('.') is valid in IDs for your
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
423 formatter.
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
424 '''
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
425 ns = self.request.include_id
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
426 if not ns is None:
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
427 ns = self.sanitize_to_id(ns)
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
428 return '%s.%s' % (ns, id)
f4c371b00fec refactor the ID generation/writing code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2572
diff changeset
429 return id