Back to index

moin  1.9.0~rc2
text_plain.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - "text/plain" Formatter
00004 
00005     @copyright: 2000-2002 Juergen Hermann <jh@web.de>
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 
00009 from MoinMoin.formatter import FormatterBase
00010 
00011 class Formatter(FormatterBase):
00012     """
00013         Send plain text data.
00014     """
00015 
00016     hardspace = u' '
00017 
00018     def __init__(self, request, **kw):
00019         FormatterBase.__init__(self, request, **kw)
00020         self._in_code_area = 0
00021         self._in_code_line = 0
00022         self._code_area_state = [0, -1, -1, 0]
00023         self._in_list = 0
00024         self._did_para = 0
00025         self._url = None
00026         self._text = None # XXX does not work with links in headings!!!!!
00027 
00028     def startDocument(self, pagename):
00029         line = u"*" * (len(pagename) + 2) + u'\n'
00030         return u"%s %s \n%s" % (line, pagename, line)
00031 
00032     def endDocument(self):
00033         return u'\n'
00034 
00035     def sysmsg(self, on, **kw):
00036         return (u'\n\n*** ', u' ***\n\n')[not on]
00037 
00038     def pagelink(self, on, pagename='', page=None, **kw):
00039         FormatterBase.pagelink(self, on, pagename, page, **kw)
00040         return (u">>", u"<<") [not on]
00041 
00042     def interwikilink(self, on, interwiki='', pagename='', **kw):
00043         if on:
00044             self._url = u"%s:%s" % (interwiki, pagename)
00045             self._text = []
00046             return u''
00047         else:
00048             if "".join(self._text) == self._url:
00049                 self._url = None
00050                 self._text = None
00051                 return ''
00052             else:
00053                 self._url = None
00054                 self._text = None
00055                 return u' [%s]' % (self._url)
00056 
00057     def url(self, on, url='', css=None, **kw):
00058         if on:
00059             self._url = url
00060             self._text = []
00061             return u''
00062         else:
00063             if "".join(self._text) == self._url:
00064                 self._url = None
00065                 self._text = None
00066                 return ''
00067             else:
00068                 self._url = None
00069                 self._text = None
00070                 return u' [%s]' % (self._url)
00071 
00072     def attachment_link(self, on, url=None, **kw):
00073         if on:
00074             return "["
00075         else:
00076             return "]"
00077 
00078     def attachment_image(self, url, **kw):
00079         title = ''
00080         for a in (u'title', u'html__title', u'alt', u'html_alt'):
00081             if a in kw:
00082                 title = ':' + kw[a]
00083         return "[image:%s%s]" % (url, title)
00084 
00085     def attachment_drawing(self, url, text, **kw):
00086         return "[drawing:%s]" % text
00087 
00088     def text(self, text, **kw):
00089         self._did_para = 0
00090         if self._text is not None:
00091             self._text.append(text)
00092         return text
00093 
00094     def rule(self, size=0, **kw):
00095         size = min(size, 10)
00096         ch = u"---~=*+#####"[size]
00097         return (ch * 79) + u'\n'
00098 
00099     def strong(self, on, **kw):
00100         return u'*'
00101 
00102     def emphasis(self, on, **kw):
00103         return u'/'
00104 
00105     def highlight(self, on, **kw):
00106         return u''
00107 
00108     def number_list(self, on, type=None, start=None, **kw):
00109         if on:
00110             self._in_list = 1
00111             return [u'\n', u'\n\n'][not self._did_para]
00112         else:
00113             self._in_list = 0
00114             if not self._did_para:
00115                 self._did_para = 1
00116                 return u'\n'
00117         return u''
00118 
00119     def bullet_list(self, on, **kw):
00120         if on:
00121             self._in_list = -1
00122             return [u'\n', u'\n\n'][not self._did_para]
00123         else:
00124             self._in_list = 0
00125             if not self._did_para:
00126                 self._did_para = 1
00127                 return u'\n'
00128         return u''
00129 
00130     def listitem(self, on, **kw):
00131         if on:
00132             if self._in_list > 0:
00133                 self._in_list += 1
00134                 self._did_para = 1
00135                 return ' %d. ' % (self._in_list-1, )
00136             elif self._in_list < 0:
00137                 self._did_para = 1
00138                 return u' * '
00139             else:
00140                 return u' * '
00141         else:
00142             self._did_para = 1
00143             return u'\n'
00144 
00145     def sup(self, on, **kw):
00146         return u'^'
00147 
00148     def sub(self, on, **kw):
00149         return u'_'
00150 
00151     def strike(self, on, **kw):
00152         return u'__'
00153 
00154     def code(self, on, **kw):
00155         #return [unichr(0x60), unichr(0xb4)][not on]
00156         return u"'" # avoid high-ascii
00157 
00158     def preformatted(self, on, **kw):
00159         FormatterBase.preformatted(self, on)
00160         snip = u'---%<'
00161         snip = snip + (u'-' * (78 - len(snip)))
00162         if on:
00163             return u'\n' + snip + u'\n'
00164         else:
00165             return snip + u'\n'
00166 
00167     def small(self, on, **kw):
00168         return u''
00169 
00170     def big(self, on, **kw):
00171         return u''
00172 
00173     def code_area(self, on, code_id, code_type='code', show=0, start=-1, step=-1, msg=None):
00174         snip = u'---CodeArea'
00175         snip = snip + (u'-' * (78 - len(snip)))
00176         if on:
00177             self._in_code_area = 1
00178             self._in_code_line = 0
00179             self._code_area_state = [show, start, step, start]
00180             return u'\n' + snip + u'\n'
00181         else:
00182             if self._in_code_line:
00183                 return self.code_line(0) + snip + u'\n'
00184             return snip + u'\n'
00185 
00186     def code_line(self, on):
00187         res = u''
00188         if not on or (on and self._in_code_line):
00189             res += u'\n'
00190         if on:
00191             if self._code_area_state[0] > 0:
00192                 res += u' %4d  ' % self._code_area_state[3]
00193                 self._code_area_state[3] += self._code_area_state[2]
00194         self._in_code_line = on != 0
00195         return res
00196 
00197     def code_token(self, on, tok_type):
00198         return ""
00199 
00200     def paragraph(self, on, **kw):
00201         FormatterBase.paragraph(self, on)
00202         if self._did_para:
00203             on = 0
00204         return [u'\n', u''][not on]
00205 
00206     def linebreak(self, preformatted=1):
00207         return u'\n'
00208 
00209     def smiley(self, text):
00210         return text
00211 
00212     def heading(self, on, depth, **kw):
00213         if on:
00214             self._text = []
00215             return '\n\n'
00216         else:
00217             result = u'\n%s\n\n' % (u'=' * len("".join(self._text)))
00218             self._text = None
00219             return result
00220 
00221     def table(self, on, attrs={}, **kw):
00222         return u''
00223 
00224     def table_row(self, on, attrs={}, **kw):
00225         return u''
00226 
00227     def table_cell(self, on, attrs={}, **kw):
00228         return u''
00229 
00230     def underline(self, on, **kw):
00231         return u'_'
00232 
00233     def definition_list(self, on, **kw):
00234         return u''
00235 
00236     def definition_term(self, on, compact=0, **kw):
00237         result = u''
00238         if not compact:
00239             result = result + u'\n'
00240         if not on:
00241             result = result + u':\n'
00242         return result
00243 
00244     def definition_desc(self, on, **kw):
00245         return [u'    ', u'\n'][not on]
00246 
00247     def image(self, src=None, **kw):
00248         for a in (u'title', u'html__title', u'alt', u'html_alt'):
00249             if a in kw:
00250                 return kw[a]
00251         return u''
00252 
00253     def transclusion(self, on, **kw):
00254         return u''
00255 
00256     def transclusion_param(self, **kw):
00257         return u''
00258 
00259     def lang(self, on, lang_name):
00260         return ''
00261