Home | History | Annotate | Download | only in extensions
      1 # markdown is released under the BSD license
      2 # Copyright 2007, 2008 The Python Markdown Project (v. 1.7 and later)
      3 # Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)
      4 # Copyright 2004 Manfred Stienstra (the original version)
      5 # 
      6 # All rights reserved.
      7 # 
      8 # Redistribution and use in source and binary forms, with or without
      9 # modification, are permitted provided that the following conditions are met:
     10 # 
     11 # *   Redistributions of source code must retain the above copyright
     12 #     notice, this list of conditions and the following disclaimer.
     13 # *   Redistributions in binary form must reproduce the above copyright
     14 #     notice, this list of conditions and the following disclaimer in the
     15 #     documentation and/or other materials provided with the distribution.
     16 # *   Neither the name of the <organization> nor the
     17 #     names of its contributors may be used to endorse or promote products
     18 #     derived from this software without specific prior written permission.
     19 # 
     20 # THIS SOFTWARE IS PROVIDED BY THE PYTHON MARKDOWN PROJECT ''AS IS'' AND ANY
     21 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     22 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     23 # DISCLAIMED. IN NO EVENT SHALL ANY CONTRIBUTORS TO THE PYTHON MARKDOWN PROJECT
     24 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30 # POSSIBILITY OF SUCH DAMAGE.
     31 
     32 
     33 '''
     34 Abbreviation Extension for Python-Markdown
     35 ==========================================
     36 
     37 This extension adds abbreviation handling to Python-Markdown.
     38 
     39 Simple Usage:
     40 
     41     >>> import markdown
     42     >>> text = """
     43     ... Some text with an ABBR and a REF. Ignore REFERENCE and ref.
     44     ...
     45     ... *[ABBR]: Abbreviation
     46     ... *[REF]: Abbreviation Reference
     47     ... """
     48     >>> print markdown.markdown(text, ['abbr'])
     49     <p>Some text with an <abbr title="Abbreviation">ABBR</abbr> and a <abbr title="Abbreviation Reference">REF</abbr>. Ignore REFERENCE and ref.</p>
     50 
     51 Copyright 2007-2008
     52 * [Waylan Limberg](http://achinghead.com/)
     53 * [Seemant Kulleen](http://www.kulleen.org/)
     54 	
     55 
     56 '''
     57 
     58 from __future__ import absolute_import
     59 from __future__ import unicode_literals
     60 from . import Extension
     61 from ..preprocessors import Preprocessor
     62 from ..inlinepatterns import Pattern
     63 from ..util import etree
     64 import re
     65 
     66 # Global Vars
     67 ABBR_REF_RE = re.compile(r'[*]\[(?P<abbr>[^\]]*)\][ ]?:\s*(?P<title>.*)')
     68 
     69 class AbbrExtension(Extension):
     70     """ Abbreviation Extension for Python-Markdown. """
     71 
     72     def extendMarkdown(self, md, md_globals):
     73         """ Insert AbbrPreprocessor before ReferencePreprocessor. """
     74         md.preprocessors.add('abbr', AbbrPreprocessor(md), '<reference')
     75         
     76            
     77 class AbbrPreprocessor(Preprocessor):
     78     """ Abbreviation Preprocessor - parse text for abbr references. """
     79 
     80     def run(self, lines):
     81         '''
     82         Find and remove all Abbreviation references from the text.
     83         Each reference is set as a new AbbrPattern in the markdown instance.
     84         
     85         '''
     86         new_text = []
     87         for line in lines:
     88             m = ABBR_REF_RE.match(line)
     89             if m:
     90                 abbr = m.group('abbr').strip()
     91                 title = m.group('title').strip()
     92                 self.markdown.inlinePatterns['abbr-%s'%abbr] = \
     93                     AbbrPattern(self._generate_pattern(abbr), title)
     94             else:
     95                 new_text.append(line)
     96         return new_text
     97     
     98     def _generate_pattern(self, text):
     99         '''
    100         Given a string, returns an regex pattern to match that string. 
    101         
    102         'HTML' -> r'(?P<abbr>[H][T][M][L])' 
    103         
    104         Note: we force each char as a literal match (in brackets) as we don't 
    105         know what they will be beforehand.
    106 
    107         '''
    108         chars = list(text)
    109         for i in range(len(chars)):
    110             chars[i] = r'[%s]' % chars[i]
    111         return r'(?P<abbr>\b%s\b)' % (r''.join(chars))
    112 
    113 
    114 class AbbrPattern(Pattern):
    115     """ Abbreviation inline pattern. """
    116 
    117     def __init__(self, pattern, title):
    118         super(AbbrPattern, self).__init__(pattern)
    119         self.title = title
    120 
    121     def handleMatch(self, m):
    122         abbr = etree.Element('abbr')
    123         abbr.text = m.group('abbr')
    124         abbr.set('title', self.title)
    125         return abbr
    126 
    127 def makeExtension(configs=None):
    128     return AbbrExtension(configs=configs)
    129