Attachment 'Multiline.py'

Download

   1 # -*- coding: utf-8 -*-
   2 """
   3     MoinMoin - Multiline parser
   4 
   5     Allows line breaks in wiki page source.
   6 
   7     Lines are truncated at the delimiter (default is #),
   8     then joined with the next line.
   9     
  10 
  11     EXAMPLES:
  12 
  13 
  14 {{{#!Multiline
  15 A very long # This comment will be ignored.
  16 line.
  17 
  18 [[attachment:Multiline.py  A very #
  19 long link label]]
  20 
  21 Escaped \#.  This is NOT a comment.
  22 
  23 ||'''Name'''||'''Email'''||'''Phone'''||
  24 || Albert Einstein          #
  25 || weird_al@einste.in       #
  26 || 800-314-1897             #
  27 ||
  28 || David Hilbert                # name
  29 || root@math.uni-goettingen.de  # email
  30 || 49-(0)551 39 1337            # phone
  31 ||
  32 }}}
  33 
  34 
  35 
  36 {{{#!Multiline  delimiter=//
  37 A very long  // C++ pwned Python
  38 line.
  39 }}}
  40 
  41 
  42     NON-EXAMPLE:
  43 
  44     {{{#!Multiline
  45        {{{#!SomeLesserKnownParser
  46            Moin doesn't support #
  47            nested brackets.
  48        }}}
  49     }}}
  50 
  51 
  52     @copyright: 2007 by Paisa Seeluangsawat <paisa@users.sf.net>
  53     @license: GNU GPL v.2 or later.
  54 """
  55 
  56 
  57 from MoinMoin import wikiutil
  58 import re
  59 
  60 Dependencies = []
  61 
  62 
  63 class Parser:
  64 
  65     # allow caching
  66     caching = 1
  67     Dependencies = []
  68 
  69     def __init__(self, raw, request, **kw):
  70         self.delimiter = '#'
  71 
  72         # standard __init__ for a Parser
  73         self.raw = raw
  74         self.request = request
  75         self.form = request.form
  76         self._ = request.getText
  77 
  78         for arg in kw.get('format_args', '').split(','):
  79             if arg.find('=') > -1:
  80                 key, value = arg.split('=')
  81                 setattr(self, key, value)
  82 
  83         ### Make sure that our delimiter is sane.
  84         d = self.delimiter.strip()
  85         if len(d)==0:
  86             d = '#'
  87         # Remove quotes
  88         if len(d)>2 and (d[0]==d[-1]=="'" or d[0]==d[-1]=='"') :
  89             d = d[1:-1]
  90         # Escape non-alphanumeric characters.
  91         # d = ''.join(  [c.isalnum() and c or '\\'+c for c in d]  )
  92         d = re.sub('(\W)', lambda m: '\\%s'%m.groups(),  d)
  93         self.delimiter = d
  94 
  95 
  96     def format(self, formatter):
  97         pattern = re.compile(r"""
  98             (?<!\\)   # Is the delimiter escaped?
  99             %s[^\n]*  # delimiter and comments
 100             \n
 101         """ % self.delimiter, re.VERBOSE + re.UNICODE)
 102 
 103         new_raw = pattern.sub('', self.raw)
 104 
 105 #       # Uncomment this block if you are paranoid.
 106 #         try:
 107 #             processor = wikiutil.importPlugin(self.request.cfg, "parser", "wiki", "Parser")
 108 #         except wikiutil.PluginMissingError:
 109 #             self.request.write(formatter.text(new_raw))
 110 
 111         res = formatter.processor('wiki', [new_raw], 1)      # 1 for "is_parser"
 112         self.request.write(res)

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-12-09 15:50:18, 2.6 KB) [[attachment:Multiline.py]]
 All files | Selected Files: delete move to page copy to page

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