Back to index

moin  1.9.0~rc2
text_xml.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - "text/xml" Formatter
00004 
00005     @copyright: 2000-2002 Juergen Hermann <jh@web.de>
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 
00009 from xml.sax import saxutils
00010 from MoinMoin.formatter import FormatterBase
00011 from MoinMoin import config
00012 from MoinMoin.Page import Page
00013 
00014 class Formatter(FormatterBase):
00015     """
00016         Send XML data.
00017     """
00018 
00019     hardspace = '&nbsp;'
00020 
00021     def __init__(self, request, **kw):
00022         FormatterBase.__init__(self, request, **kw)
00023         self._current_depth = 1
00024         self._base_depth = 0
00025         self.in_pre = 0
00026 
00027     def _escape(self, text, extra_mapping={"'": "&apos;", '"': "&quot;"}):
00028         return saxutils.escape(text, extra_mapping)
00029 
00030     def startDocument(self, pagename):
00031         encoding = config.charset
00032         return '<?xml version="1.0" encoding="%s"?>\n<s1 title="%s">' % (
00033             encoding, self._escape(pagename))
00034 
00035     def endDocument(self):
00036         result = ""
00037         while self._current_depth > 1:
00038             result += "</s%d>" % self._current_depth
00039             self._current_depth -= 1
00040         return result + '</s1>'
00041 
00042     def lang(self, on, lang_name):
00043         return ('<div lang="">' % lang_name, '</div>')[not on]
00044 
00045     def sysmsg(self, on, **kw):
00046         return ('<sysmsg>', '</sysmsg>')[not on]
00047 
00048     def rawHTML(self, markup):
00049         return '<![CDATA[' + markup.replace(']]>', ']]>]]&gt;<![CDATA[') + ']]>'
00050 
00051     def pagelink(self, on, pagename='', page=None, **kw):
00052         FormatterBase.pagelink(self, on, pagename, page, **kw)
00053         if page is None:
00054             page = Page(self.request, pagename, formatter=self)
00055         return page.link_to(self.request, on=on, **kw)
00056 
00057     def interwikilink(self, on, interwiki='', pagename='', **kw):
00058         if on:
00059             return '<interwiki wiki="%s" pagename="%s">' % (interwiki, pagename)
00060         else:
00061             return '</interwiki>'
00062 
00063     def url(self, on, url='', css=None, **kw):
00064         if css:
00065             str = ' class="%s"' % css
00066         else:
00067             str = ''
00068         return ('<jump href="%s"%s>' % (self._escape(url), str), '</jump>') [not on]
00069 
00070     def attachment_link(self, on, url=None, **kw):
00071         if on:
00072             return '<attachment href="%s">' % (url, )
00073         else:
00074             return '</attachment>'
00075 
00076     def attachment_image(self, url, **kw):
00077         return '<attachmentimage href="%s"></attachmentimage>' % (url, )
00078 
00079     def attachment_drawing(self, url, text, **kw):
00080         return '<attachmentdrawing href="%s">%s</attachmentdrawing>' % (url, text)
00081 
00082     def text(self, text, **kw):
00083         if self.in_pre:
00084             return text.replace(']]>', ']]>]]&gt;<![CDATA[')
00085         return self._escape(text)
00086 
00087     def rule(self, size=0, **kw):
00088         return "\n<br/>%s<br/>\n" % ("-" * 78, ) # <hr/> not supported in stylebook
00089 #        if size:
00090 #            return '<hr size="%d"/>\n' % (size, )
00091 #        else:
00092 #            return '<hr/>\n'
00093 
00094     def icon(self, type):
00095         return '<icon type="%s" />' % type
00096 
00097     def strong(self, on, **kw):
00098         return ['<strong>', '</strong>'][not on]
00099 
00100     def emphasis(self, on, **kw):
00101         return ['<em>', '</em>'][not on]
00102 
00103     def highlight(self, on, **kw):
00104         return ['<strong>', '</strong>'][not on]
00105 
00106     def number_list(self, on, type=None, start=None, **kw):
00107         result = ''
00108         if self.in_p:
00109             result = self.paragraph(0)
00110         return result + ['<ol>', '</ol>\n'][not on]
00111 
00112     def bullet_list(self, on, **kw):
00113         result = ''
00114         if self.in_p:
00115             result = self.paragraph(0)
00116         return result + ['<ul>', '</ul>\n'][not on]
00117 
00118     def listitem(self, on, **kw):
00119         return ['<li>', '</li>\n'][not on]
00120 
00121     def code(self, on, **kw):
00122         return ['<code>', '</code>'][not on]
00123 
00124     def small(self, on, **kw):
00125         return ['<small>', '</small>'][not on]
00126 
00127     def big(self, on, **kw):
00128         return ['<big>', '</big>'][not on]
00129 
00130     def sup(self, on, **kw):
00131         return ['<sup>', '</sup>'][not on]
00132 
00133     def sub(self, on, **kw):
00134         return ['<sub>', '</sub>'][not on]
00135 
00136     def strike(self, on, **kw):
00137         return ['<strike>', '</strike>'][not on]
00138 
00139     def preformatted(self, on, **kw):
00140         FormatterBase.preformatted(self, on)
00141         result = ''
00142         if self.in_p:
00143             result = self.paragraph(0)
00144         return result + ['<source><![CDATA[', ']]></source>'][not on]
00145 
00146     def paragraph(self, on, **kw):
00147         FormatterBase.paragraph(self, on)
00148         return ['<p>', '</p>\n'][not on]
00149 
00150     def linebreak(self, preformatted=1):
00151         return ['\n', '<br/>\n'][not preformatted]
00152 
00153     def heading(self, on, depth, id=None, **kw):
00154         if not on:
00155             return '">\n'
00156         # remember depth of first heading, and adapt current depth accordingly
00157         if not self._base_depth:
00158             self._base_depth = depth
00159         depth = max(depth + (2 - self._base_depth), 2)
00160 
00161         # close open sections
00162         result = ""
00163         while self._current_depth >= depth:
00164             result = result + "</s%d>\n" % self._current_depth
00165             self._current_depth -= 1
00166         self._current_depth = depth
00167 
00168         id_text = ''
00169         if id:
00170             id_text = ' id="%s"' % id
00171 
00172         return result + '<s%d%s title="' % (depth, id_text)
00173 
00174     def table(self, on, attrs={}, **kw):
00175         return ['<table>', '</table>'][not on]
00176 
00177     def table_row(self, on, attrs={}, **kw):
00178         return ['<tr>', '</tr>'][not on]
00179 
00180     def table_cell(self, on, attrs={}, **kw):
00181         return ['<td>', '</td>'][not on]
00182 
00183     def anchordef(self, id):
00184         return '<anchor id="%s"/>' % id
00185 
00186     def anchorlink(self, on, name='', **kw):
00187         id = kw.get('id', None)
00188         extra = ''
00189         if id:
00190             extra = ' id="%s"' % id
00191         return ('<link anchor="%s"%s>' % (name, extra), '</link>') [not on]
00192 
00193     def underline(self, on, **kw):
00194         return self.strong(on) # no underline in StyleBook
00195 
00196     def definition_list(self, on, **kw):
00197         result = ''
00198         if self.in_p:
00199             result = self.paragraph(0)
00200         return result + ['<gloss>', '</gloss>'][not on]
00201 
00202     def definition_term(self, on, compact=0, **kw):
00203         return ['<label>', '</label>'][not on]
00204 
00205     def definition_desc(self, on, **kw):
00206         return ['<item>', '</item>'][not on]
00207 
00208     def image(self, src=None, **kw):
00209         valid_attrs = ['src', 'width', 'height', 'alt', 'title']
00210         attrs = {'src': src}
00211         for key, value in kw.items():
00212             if key in valid_attrs:
00213                 attrs[key] = value
00214         return FormatterBase.image(self, **attrs) + '</img>'
00215 
00216     def transclusion(self, on, **kw):
00217         # TODO, see text_html formatter
00218         return ''
00219 
00220     def transclusion_param(self, **kw):
00221         # TODO, see text_html formatter
00222         return ''
00223 
00224     def code_area(self, on, code_id, code_type='code', show=0, start=-1, step=-1, msg=None):
00225         return ('<codearea id="%s">' % code_id, '</codearea')[not on]
00226 
00227     def code_line(self, on):
00228         return ('<codeline>', '</codeline')[not on]
00229 
00230     def code_token(self, on, tok_type):
00231         return ('<codetoken type="%s">' % tok_type, '</codetoken')[not on]