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)
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
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.
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.
34 Abbreviation Extension for Python-Markdown
35 ==========================================
37 This extension adds abbreviation handling to Python-Markdown.
43 ... Some text with an ABBR and a REF. Ignore REFERENCE and ref.
45 ... *[ABBR]: Abbreviation
46 ... *[REF]: Abbreviation Reference
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>
52 * [Waylan Limberg](http://achinghead.com/)
53 * [Seemant Kulleen](http://www.kulleen.org/)
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
67 ABBR_REF_RE
= re
.compile(r
'[*]\[(?P<abbr>[^\]]*)\][ ]?:\s*(?P<title>.*)')
69 class AbbrExtension(Extension
):
70 """ Abbreviation Extension for Python-Markdown. """
72 def extendMarkdown(self
, md
, md_globals
):
73 """ Insert AbbrPreprocessor before ReferencePreprocessor. """
74 md
.preprocessors
.add('abbr', AbbrPreprocessor(md
), '<reference')
77 class AbbrPreprocessor(Preprocessor
):
78 """ Abbreviation Preprocessor - parse text for abbr references. """
82 Find and remove all Abbreviation references from the text.
83 Each reference is set as a new AbbrPattern in the markdown instance.
88 m
= ABBR_REF_RE
.match(line
)
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
)
98 def _generate_pattern(self
, text
):
100 Given a string, returns an regex pattern to match that string.
102 'HTML' -> r'(?P<abbr>[H][T][M][L])'
104 Note: we force each char as a literal match (in brackets) as we don't
105 know what they will be beforehand.
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
))
114 class AbbrPattern(Pattern
):
115 """ Abbreviation inline pattern. """
117 def __init__(self
, pattern
, title
):
118 super(AbbrPattern
, self
).__init
__(pattern
)
121 def handleMatch(self
, m
):
122 abbr
= etree
.Element('abbr')
123 abbr
.text
= m
.group('abbr')
124 abbr
.set('title', self
.title
)
127 def makeExtension(configs
=None):
128 return AbbrExtension(configs
=configs
)