Attachment 'brasis.py'

Download

   1 """
   2     brasis.py - Line break preservation
   3 
   4     Default wiki parser with preserved line breaks.
   5 
   6     Restriction:
   7         Line break preservation in tables are not supported.
   8         You can still use <<BR>> in tables.
   9 
  10     Tested with:
  11         MoinMoin 1.9.7
  12 
  13     Thanks to:
  14         DavidMontgomery for nice tweaks, this plugin is based on his idea.
  15         MoinMoin developpers and contributors.
  16 
  17     @copyright: 2014 dossist.
  18     @license: GNU GPL, see http://www.gnu.org/licenses/gpl for details.
  19 """
  20 
  21 import re
  22 from MoinMoin.parser.text_moin_wiki import Parser as WikiParser
  23 
  24 Dependencies = ['user']
  25 
  26 def list_in_groupdict(keylist, groupdict):
  27     """
  28     checks if dictionary 'groupdict' has at least one key in list 'keylist'
  29     """
  30     for key in keylist:
  31         if key in groupdict and groupdict[key] is not None:
  32             return True
  33     return False
  34 
  35 class Parser(WikiParser):
  36     # No line-break elements
  37     #   List of group labels defined in WikiParser.scan_rules (see original parser).
  38     #   Suppress <br> insertion to current line (nobreak_now) and to the next line (nobreak_next)
  39     #   when we find those elements in current line.
  40     nobreak_now  = ['li', 'li_none', 'ol', 'dl', 'table']
  41     nobreak_next = ['heading', 'table']
  42 
  43     def __init__(self, raw, request, **kw):
  44         WikiParser.__init__(self, raw, request, **kw)
  45         # state holders carried over lines
  46         self.break_next = False # can <br> be inserted in the next line?
  47         self.prev_list = False # were we in a list in the previous line?
  48 
  49     def format(self, formatter):
  50         """
  51         same as original but resetting state holders at the end
  52         """
  53         WikiParser.format(self, formatter)
  54         # reset those states every time format is done
  55         self.break_next = False
  56         self.prev_list = False
  57 
  58     def scan(self, line, inhibit_p=False):
  59         """
  60         pass line to the original method, then modify the result.
  61 
  62         * if we need <br> for the current line, we just indicate that the next line should begin with <br>,
  63           and move on to the next line doing nothing to the current line.
  64         * if the previous line needed <br>, we check whether current line is 'breakable',
  65           and finally add <br> at the beginning of the output from original scan() method if conditions are met.
  66         """
  67         formatted_line = WikiParser.scan(self, line, inhibit_p=inhibit_p)
  68 
  69         # match wiki patterns again to know what context we are in
  70         match = self.scan_re.search(line)
  71         if match:
  72             match_dict = match.groupdict()
  73         else:
  74             match_dict = {}
  75 
  76         # check if current line can be line-broken
  77         break_now = not (list_in_groupdict(self.nobreak_now, match_dict) or # current line
  78                         self.in_table or self.in_pre or # no-linebreak states carried over lines
  79                         (self.prev_list is not self.in_list) or # need this when quitting from lists
  80                         self.line_was_empty) # finally we don't need line breaks for paragraph ends
  81         # if conditions are met, append a br tag
  82         if break_now and self.break_next:
  83             formatted_line = self.formatter.linebreak(preformatted=0) + formatted_line
  84         # in certain structures, we don't want line breaks in the next line
  85         self.break_next = not (list_in_groupdict(self.nobreak_next, match_dict) or
  86                                self.in_table or self.in_pre)
  87         # save current in_list status
  88         self.prev_list = self.in_list
  89 
  90         return formatted_line

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] (2014-05-30 16:50:59, 3.5 KB) [[attachment:brasis.py]]
 All files | Selected Files: delete move to page copy to page

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