Attachment 'text.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - Plain Text Parser, fallback for text/*
   4 
   5     You can supply format_args like this:
   6     {{{#!text cssclass1/cssclass2,<show_num>
   7     ...
   8     }}}
   9     The css classes are used for a wrapping div (same way as wiki parser does
  10     it).
  11     If show_num is 1, we use the code_area formatter and you will be able to
  12     toggle the line numbers by some javascript toggling link.
  13 
  14     @copyright: 2000-2002 Juergen Hermann <jh@web.de>,
  15                 2007 MoinMoin:ThomasWaldmann
  16     @license: GNU GPL, see COPYING for details.
  17 """
  18 
  19 import sha
  20 from MoinMoin import config
  21 
  22 Dependencies = []
  23 
  24 class Parser:
  25     """
  26         Send plain text in a HTML <pre> element.
  27     """
  28 
  29     ## specify extensions willing to handle (for inline:)
  30     ## should be a list of extensions including the leading dot
  31     ## TODO: remove the leading dot from the extension. This is stupid.
  32     #extensions = ['.txt']
  33     ## use '*' instead of the list(!) to specify a default parser
  34     ## which is used as fallback
  35     extensions = '*'
  36     Dependencies = []
  37     
  38     def __init__(self, raw, request, **kw):
  39         self.raw = raw
  40         self.request = request
  41         self.form = request.form
  42         self._ = request.getText
  43         format_args = kw.get('format_args', '').strip() + ','
  44         wrapping_div_class, show_num = format_args.split(',')[:2]
  45         self.wrapping_div_class = wrapping_div_class.replace('/', ' ')
  46         try:
  47             self.show_num = int(show_num)
  48             self.num_start = 1
  49             self.num_step = 1
  50         except:
  51             self.show_num = 0
  52 
  53     def format(self, formatter):
  54         """ Send the text. """
  55         if self.wrapping_div_class:
  56             div_kw = {'css_class': self.wrapping_div_class, }
  57             if 'comment' in self.wrapping_div_class.split():
  58                 # show comment divs depending on user profile (and wiki configuration)
  59                 div_kw['style'] = self.request.user.show_comments and "display:''" or "display:none"
  60             self.request.write(formatter.div(1, **div_kw))
  61 
  62         lines = self.raw.expandtabs().split('\n')
  63         output = []
  64         if self.show_num:
  65             _code_id = sha.new(self.raw.encode(config.charset)).hexdigest()
  66             self.request.write(formatter.code_area(1, _code_id, 'NumberedText', self.show_num, self.num_start, self.num_step))
  67             for line in lines:
  68                 output.extend([formatter.code_line(1),
  69                                formatter.text(line),
  70                                formatter.code_line(0), ])
  71             self.request.write(''.join(output))
  72             self.request.write(formatter.code_area(0, _code_id))
  73         else:
  74             self.request.write(formatter.preformatted(1))
  75             for line in lines:
  76                 output.extend([formatter.text(line),
  77                                formatter.linebreak(), ])
  78             self.request.write(''.join(output))
  79             self.request.write(formatter.preformatted(0))
  80 
  81         if self.wrapping_div_class:
  82             self.request.write(formatter.div(0))

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] (2007-02-21 08:53:00, 9.0 KB) [[attachment:FrBrGeorge.gif]]
  • [get | view] (2007-04-01 22:32:51, 3.0 KB) [[attachment:text.py]]
 All files | Selected Files: delete move to page copy to page

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