Attachment 'text_latex-1.6.py'

Download

   1 # -*- coding: utf-8 -*-
   2 """
   3     MoinMoin - "text/latex" Formatter
   4 
   5     Copyright 2005 Johannes Berg <johannes@sipsolutions.net>
   6     Copyright (c) 2003 by João Neves <moin@silvaneves.org>
   7     Copyright (c) 2000, 2001, 2002 by Jürgen Hermann <jh@web.de>
   8 
   9     All rights reserved, see COPYING for details.
  10 """
  11 
  12 # Imports
  13 
  14 import sys, re
  15 from MoinMoin.formatter import FormatterBase
  16 from MoinMoin.Page import Page
  17 
  18 #############################################################################
  19 ### LaTeX Formatter
  20 #############################################################################
  21 
  22 class Formatter(FormatterBase):
  23     """
  24         Send text data.
  25     """
  26 
  27     hardspace = ' '
  28 
  29     def __init__(self, request, **kw):
  30         apply(FormatterBase.__init__, (self, request), kw)
  31         self.verbatim = False
  32         self.itemized = False
  33 
  34     def text2latex(self, text):
  35         "Escape special characters if not in verbatim mode"
  36         if self.verbatim: return text
  37         text = text.replace('\\', '$\\backslash$ ');
  38         text = text.replace('$', r'\$');
  39         text = text.replace(r'\$\backslash\$', r'$\backslash$')
  40         text = text.replace('#', r'\#');
  41         text = text.replace('%', r'\%');
  42         text = text.replace('^', r'\^{}');
  43         text = text.replace('&', r'\&');
  44         text = text.replace('_', r'\_');
  45         text = text.replace('{', r'\{');
  46         text = text.replace('}', r'\}');
  47         text = text.replace('~', r'\~{}');
  48         text = text.replace('"', r'\"{}');
  49         text = text.replace(u'ä', r'"a');
  50         text = text.replace(u'ü', r'"u');
  51         text = text.replace(u'ö', r'"o');
  52         text = text.replace(u'Ä', r'"A');
  53         text = text.replace(u'Ü', r'"U');
  54         text = text.replace(u'Ö', r'"O');
  55         text = text.replace(u'ß', r'\ss{}');
  56         return text
  57 
  58     def write_text(self, text):
  59       if self.item is None:
  60         return text
  61       else:
  62         self.item = (self.item[0], self.item[1]+text)
  63         return ''
  64 
  65     def startDocument(self, pagename):
  66         extra_preamble = ''
  67         preamble_page = self.request.pragma.get('latex_preamble', None)
  68         if preamble_page is not None:
  69           extra_preamble = Page(self.request, preamble_page).get_raw_body()
  70         extra_preamble = re.sub(re.compile('^#'), '%', extra_preamble)
  71         return """
  72 \\documentclass[a4paper,12pt]{article}
  73 
  74 \\usepackage[utf-8]{inputenc}
  75 \\usepackage{helvet}
  76 \\usepackage{graphicx}
  77 \\usepackage{multicol}
  78 \\usepackage{fullpage}
  79 \\usepackage{fancyhdr}
  80 \\usepackage{multirow}
  81 \\makeatletter
  82 \\DeclareRobustCommand*\\textsubscript[1]{%%
  83   \\@textsubscript{\\selectfont#1}}
  84 \\def\\@textsubscript#1{%%
  85   {\\m@th\\ensuremath{_{\\mbox{\\fontsize\\sf@size\\z@#1}}}}}
  86 \\makeatother
  87 
  88 %% begin extra preamble inclusion %%
  89 %s
  90 
  91 %% end extra preamble inclusion %%
  92 
  93 \\title{%s}
  94 
  95 \\author{ }
  96 
  97 \\date{ }
  98 
  99 \\renewcommand{\\theenumi}{\\arabic{enumi}}
 100 \\renewcommand{\\theenumii}{\\arabic{enumi}.\\arabic{enumii}}
 101 \\renewcommand{\\theenumiii}{\\arabic{enumi}.\\arabic{enumii}.\\arabic{enumiii}}
 102 \\renewcommand{\\theenumiv}{\\arabic{enumi}.\\arabic{enumii}.\\arabic{enumiii}.\\arabic{enumiv}}
 103 
 104 \\begin{document}
 105 """ % (extra_preamble, pagename)
 106 
 107     def endDocument(self):
 108         return '\\end{document}\n'
 109 
 110     def sysmsg(self, text, **kw):
 111         return self.write_text('')
 112 
 113     def pagelink(self, on, pagename, text=None, **kw):
 114         return self.write_text('')
 115 
 116     def url(self, on, url=None, css=None, **kw):
 117         if not on:
 118             return self.write_text('}')
 119         if url[-4:] == '.pdf':
 120             nid = self.next_img_data
 121             self.next_img_data = ''
 122             return '\\includegraphics%s{' % nid
 123         url = url.replace('&', '\\&')
 124         return self.write_text('\\href{%s}{' % url)
 125 
 126     def text(self, text):
 127         return self.write_text(self.text2latex(text))
 128 
 129     def rule(self, size=0):
 130         size = min(size, 10)
 131         ch = "---~=*+#####"[size]
 132         return self.write_text('\\vrule \n')
 133 
 134     def strong(self, on):
 135         return self.write_text(['{\\bf ', '}'][not on])
 136 
 137     def emphasis(self, on):
 138         return self.write_text(['{\\em ', '}'][not on])
 139 
 140     def highlight(self, on):
 141         return self.write_text(['{\\tt ', '}'][not on])
 142 
 143     def number_list(self, on, type=None, start=None):
 144         self.itemized = on
 145         if on:
 146             text = "\\begin{enumerate}"
 147         else:
 148             text = '\\end{enumerate}\n'
 149         return self.write_text(text)
 150 
 151     def bullet_list(self, on):
 152         self.itemized = on
 153         return self.write_text(['\\begin{itemize}\n', '\n\\end{itemize}\n'][not on])
 154 
 155     def listitem(self, on, **kw):
 156         if not self.itemized: return ''
 157         self._in_li = on != 0
 158         if on:
 159             return self.write_text('\\item ')
 160         else:
 161             return ''
 162 
 163     def sup(self, on):
 164         return self.write_text(['\\textsuperscript{', '}'][not on])
 165 
 166     def sub(self, on):
 167         return self.write_text(['\\textsubscript{', '}'][not on])
 168 
 169     def code(self, on, **kw):
 170         return self.write_text(['{\\tt ', '}'][not on])
 171 
 172     def code_area(self, on, code_id, code_type='code', show=0, start=-1, step=-1):
 173         res = self.preformatted(on)
 174         self.verbatim = False
 175         return self.write_text(res)
 176 
 177     def code_token(self, on, tok_type):
 178         return self.write_text('')
 179 
 180     def code_line(self, on):
 181         return self.write_text('\n')
 182 
 183     def preformatted(self, on):
 184         FormatterBase.preformatted(self, on)
 185         self.verbatim = on
 186         return self.write_text(['\\begin{verbatim}\n', '\\end{verbatim}\n'][not on])
 187 
 188     def smiley(self, text):
 189         return self.write_text(self.text2latex(text))
 190 
 191     def paragraph(self, on, **kw):
 192         FormatterBase.paragraph(self, on)
 193         return self.write_text(['', '\n\n'][not on])
 194 
 195     def linebreak(self, preformatted=1):
 196         if preformatted==1:
 197             return self.write_text('\n')
 198         else:
 199             return self.write_text('\\newline')
 200 
 201     def heading(self, on, depth, **kw):
 202         if depth == 1:
 203             rv = (r'\section{','}\n')
 204         elif depth == 2:
 205             rv = ('\\subsection{','}\n')
 206         elif depth == 3:
 207             rv = ('\\subsubsection{','}\n')
 208         else:
 209             rv = (r'\paragraph{','}\n',)
 210         return self.write_text(rv[not on])
 211 
 212     rows = []
 213     row = []
 214     item = None
 215 
 216     def table(self, on, attrs={}):
 217         def count_cols(row):
 218           cols = 0
 219           for cell in row:
 220             if cell[0].has_key('colspan'):
 221               cols += int(cell[0]['colspan'][1:-1])
 222             else:
 223               cols += 1
 224           return cols
 225 
 226         if on:
 227           self.rows = []
 228           self.item = None
 229           self.row = []
 230           return ''
 231         # not on:
 232         if self.rows == []: return ''
 233         cols = count_cols(self.rows[0])
 234         rows = len(self.rows)
 235         _table = [[0 for i in xrange(0,cols)] for j in xrange(0,rows)]
 236         _rownum = -1
 237         for _row in self.rows:
 238           _rownum += 1
 239           _cellnum = -1
 240           for _cell in _row:
 241             _cellnum += 1
 242 
 243             while _table[_rownum][_cellnum] is None or type(_table[_rownum][_cellnum]) == type(()):
 244               _cellnum += 1
 245 
 246             if _cell[0].get('rowspan') == '"1"':
 247               del _cell[0]['rowspan']
 248             if _cell[0].get('colspan') == '"1"':
 249               del _cell[0]['colspan']
 250 
 251             _rowspan = int(_cell[0].get('rowspan', '"1"')[1:-1])
 252             _colspan = int(_cell[0].get('colspan', '"1"')[1:-1])
 253 
 254             for j in xrange(0,_rowspan):
 255               for i in xrange(0,_colspan):
 256                 _table[_rownum+j][_cellnum+i] = None
 257               _table[_rownum+j][_cellnum] = ({'colspan':'"%d"'%_colspan},None)
 258             _table[_rownum][_cellnum] = _cell
 259 
 260 
 261         table = '\\begin{tabular}{|%s}\n' % (cols * 'l|')
 262         for _row in _table:
 263           row = ''
 264           cellnum = 0
 265           _lines = []
 266           _do_line = True
 267           for _cell in _row:
 268             cellnum+=1
 269             if _cell == 0:
 270               return 'INVALID TABLE'
 271             if _cell is None:
 272               if _do_line:
 273                 _lines += [cellnum]
 274               continue
 275             _rowspan = int(_cell[0].get('rowspan', '"1"')[1:-1])
 276             _colspan = int(_cell[0].get('colspan', '"1"')[1:-1])
 277             format = '%s'
 278             if not (_cell[1] is None):
 279               _do_line = True
 280               _lines += [cellnum]
 281             else:
 282               _do_line = False
 283               _cell = (_cell[0], u'')
 284             if _rowspan > 1:
 285               format = r'\multirow{%d}*{%%s}' % _rowspan
 286             if _colspan > 1:
 287               format = r'\multicolumn{%d}{|l|}{ %s }' % (_colspan, format)
 288             row += (format+' & ') % _cell[1].replace('\n',' ')
 289           for l in _lines:
 290             table += r'\cline{%d-%d}' % (l,l)
 291           table += row[0:-3] + '\\\\ \n'
 292         table += '\\hline\\end{tabular}\n\n'
 293         return table
 294 
 295 
 296     def table_row(self, on, attrs={}):
 297         if not on:
 298           self.rows += [self.row]
 299           self.row = []
 300         return ''
 301 
 302     def table_cell(self, on, attrs={}):
 303         if not on:
 304           self.row += [self.item]
 305           self.item = None
 306         else:
 307           self.item = (attrs,'')
 308         return ''
 309 
 310     def underline(self, on):
 311         return self.write_text(['\\underline{', '}'][not on])
 312 
 313     def definition_list(self, on):
 314         return self.write_text(['\\begin{description}\n', '\\end{description}\n'][not on])
 315 
 316     def definition_term(self, on, compact=0):
 317         return self.write_text(['\\item[', '] '][not on])
 318 
 319     def definition_desc(self, on):
 320         return self.write_text('')
 321 
 322     def attachment_image(self, fname):
 323         return self.image(src=fname)
 324     def image(self, **kw):
 325         # I am using alt for caption, but how to integrate the image?
 326         text = ''
 327         imgname = kw['src'].split('=')[-1]
 328         nid = self.next_img_data
 329         self.next_img_data = ''
 330         return '\\includegraphics%s{%s}' % (nid, imgname)
 331         #if kw.has_key('alt'):
 332         #    text += '\\begin{picture}\n'
 333         #    text += '\\caption{%s}\n' % kw[alt]
 334         #    text += '\\end{picture}\n'
 335         return self.write_text(text)
 336 
 337     def johill_sidecall_emit_latex(self, code):
 338         # nothing else for now
 339         return code
 340     
 341     next_img_data = ''
 342     def johill_sidecall_next_image_data(self, data):
 343         self.next_img_data = '['+data+']'
 344     
 345     def open(self, on, **kw):
 346         return ""
 347     def close(self, on, **kw):
 348         return ""
 349 
 350     # suckers who call this. we can't do raw HTML, so we ignore it
 351     def rawHTML(self, markup):
 352         return ''

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.
  • [get | view] (2008-10-12 09:05:04, 18.8 KB) [[attachment:MoinMoin2ODF-0.5.tar.gz]]
  • [get | view] (2008-10-26 10:32:16, 18.7 KB) [[attachment:MoinMoin2ODF-0.6.tar.gz]]
  • [get | view] (2009-05-20 19:58:54, 19.3 KB) [[attachment:MoinMoin2ODF-0.8.tar.gz]]
  • [get | view] (2010-04-02 20:52:11, 19.3 KB) [[attachment:MoinMoin2ODF-0.9.tar.gz]]
  • [get | view] (2006-05-27 12:18:55, 10.4 KB) [[attachment:WordML_formatter-0.1.zip]]
  • [get | view] (2012-01-04 10:50:12, 9.0 KB) [[attachment:moin2rst_1.9.zip]]
  • [get | view] (2006-11-02 07:18:20, 4.4 KB) [[attachment:moinPdf-0.1.1.tar.gz]]
  • [get | view] (2006-10-16 15:42:11, 4.2 KB) [[attachment:moinPdf-0.1.tar.gz]]
  • [get | view] (2008-03-20 10:30:54, 10.5 KB) [[attachment:text_latex-1.6.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.