2
Abbreviation Extension for Python-Markdown
3
==========================================
5
This extension adds abbreviation handling to Python-Markdown.
7
See <https://pythonhosted.org/Markdown/extensions/abbreviations.html>
10
Oringinal code Copyright 2007-2008 [Waylan Limberg](http://achinghead.com/) and
11
[Seemant Kulleen](http://www.kulleen.org/)
13
All changes Copyright 2008-2014 The Python Markdown Project
15
License: [BSD](http://www.opensource.org/licenses/bsd-license.php)
19
from __future__ import absolute_import
20
from __future__ import unicode_literals
21
from . import Extension
22
from ..preprocessors import Preprocessor
23
from ..inlinepatterns import Pattern
24
from ..util import etree, AtomicString
28
ABBR_REF_RE = re.compile(r'[*]\[(?P<abbr>[^\]]*)\][ ]?:\s*(?P<title>.*)')
31
class AbbrExtension(Extension):
32
""" Abbreviation Extension for Python-Markdown. """
34
def extendMarkdown(self, md, md_globals):
35
""" Insert AbbrPreprocessor before ReferencePreprocessor. """
36
md.preprocessors.add('abbr', AbbrPreprocessor(md), '<reference')
39
class AbbrPreprocessor(Preprocessor):
40
""" Abbreviation Preprocessor - parse text for abbr references. """
44
Find and remove all Abbreviation references from the text.
45
Each reference is set as a new AbbrPattern in the markdown instance.
50
m = ABBR_REF_RE.match(line)
52
abbr = m.group('abbr').strip()
53
title = m.group('title').strip()
54
self.markdown.inlinePatterns['abbr-%s' % abbr] = \
55
AbbrPattern(self._generate_pattern(abbr), title)
60
def _generate_pattern(self, text):
62
Given a string, returns an regex pattern to match that string.
64
'HTML' -> r'(?P<abbr>[H][T][M][L])'
66
Note: we force each char as a literal match (in brackets) as we don't
67
know what they will be beforehand.
71
for i in range(len(chars)):
72
chars[i] = r'[%s]' % chars[i]
73
return r'(?P<abbr>\b%s\b)' % (r''.join(chars))
76
class AbbrPattern(Pattern):
77
""" Abbreviation inline pattern. """
79
def __init__(self, pattern, title):
80
super(AbbrPattern, self).__init__(pattern)
83
def handleMatch(self, m):
84
abbr = etree.Element('abbr')
85
abbr.text = AtomicString(m.group('abbr'))
86
abbr.set('title', self.title)
90
def makeExtension(*args, **kwargs):
91
return AbbrExtension(*args, **kwargs)