Attachment 'mointextile.py'

Download

   1 """
   2     A Textile parser for MoinMoin
   3 
   4     @copyright: 2011 by Andrei Mackenzie
   5     @license: GNU GPL
   6 
   7     The following license terms pertain to all code inside the MoinTextile
   8     class:
   9 
  10 Copyright (c) 2009, Jason Samsa, http://jsamsa.com/
  11 Copyright (c) 2004, Roberto A. F. De Almeida, http://dealmeida.net/
  12 Copyright (c) 2003, Mark Pilgrim, http://diveintomark.org/
  13 
  14 Original PHP Version:
  15 Copyright (c) 2003-2004, Dean Allen <dean@textism.com>
  16 All rights reserved.
  17 
  18 Thanks to Carlo Zottmann <carlo@g-blog.net> for refactoring
  19 Textile's procedural code into a class framework
  20 
  21 Additions and fixes Copyright (c) 2006 Alex Shiels http://thresholdstate.com/
  22 
  23 L I C E N S E
  24 =============
  25 Redistribution and use in source and binary forms, with or without
  26 modification, are permitted provided that the following conditions are met:
  27 
  28 * Redistributions of source code must retain the above copyright notice,
  29   this list of conditions and the following disclaimer.
  30 
  31 * Redistributions in binary form must reproduce the above copyright notice,
  32   this list of conditions and the following disclaimer in the documentation
  33   and/or other materials provided with the distribution.
  34 
  35 * Neither the name Textile nor the names of its contributors may be used to
  36   endorse or promote products derived from this software without specific
  37   prior written permission.
  38 
  39 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  40 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  42 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  43 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  44 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  45 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  46 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  47 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  49 POSSIBILITY OF SUCH DAMAGE.
  50 
  51 """
  52 
  53 import textile
  54 import re
  55 
  56 class MoinTextile(textile.Textile):
  57     """
  58     Special handling for hyperlinks
  59     """
  60 
  61     def links(self, text):
  62         """
  63         Accept '%' as a valid character in URLs so as to accept URLs that
  64         contain %20, etc.
  65         """
  66 
  67         punct = '!"#$%&\'*+,-./:;=?@\\^_`|~'
  68 
  69         pattern = r'''
  70             (?P<pre>    [\s\[{(]|[%s]   )?
  71             "                          # start
  72             (?P<atts>   %s       )
  73             (?P<text>   [^"]+?   )
  74             \s?
  75             (?:   \(([^)]+?)\)(?=")   )?     # $title
  76             ":
  77             (?P<url>    (?:ftp|https?)? (?: :// )? [-A-Za-z0-9+&@#/?=~_()|!:,.;%%]*[-A-Za-z0-9+&@#/=~_()|]   )
  78             (?P<post>   [^\w\/;]*?   )
  79             (?=<|\s|$)
  80         ''' % (re.escape(punct), self.c)
  81 
  82         text = re.compile(pattern, re.X).sub(self.fLink, text)
  83 
  84         return text
  85 
  86 class Parser:
  87     """
  88     Parse the raw markup as Textile
  89     """
  90 
  91     def __init__(self, raw, request, **kw):
  92         """ 
  93         Initialize this parser with the raw markup, request, etc. 
  94         """
  95         self.raw = raw
  96         self.request = request
  97         self.form = request.form
  98         self._ = request.getText
  99         self.textiler = MoinTextile()
 100 
 101     def format(self, formatter):
 102         """ 
 103         Process the raw markup as Textile and write the result to
 104         the request 
 105         """
 106         processed = self.textiler.textile(self.raw)
 107         self.request.write(processed)

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] (2011-02-25 17:25:06, 3.6 KB) [[attachment:mointextile.py]]
 All files | Selected Files: delete move to page copy to page

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