1'''
2Abbreviation Extension for Python-Markdown
3==========================================
4
5This extension adds abbreviation handling to Python-Markdown.
6
7See <https://pythonhosted.org/Markdown/extensions/abbreviations.html>
8for documentation.
9
10Oringinal code Copyright 2007-2008 [Waylan Limberg](http://achinghead.com/) and
11 [Seemant Kulleen](http://www.kulleen.org/)
12
13All changes Copyright 2008-2014 The Python Markdown Project
14
15License: [BSD](http://www.opensource.org/licenses/bsd-license.php)
16
17'''
18
19from __future__ import absolute_import
20from __future__ import unicode_literals
21from . import Extension
22from ..preprocessors import Preprocessor
23from ..inlinepatterns import Pattern
24from ..util import etree, AtomicString
25import re
26
27# Global Vars
28ABBR_REF_RE = re.compile(r'[*]\[(?P<abbr>[^\]]*)\][ ]?:\s*(?P<title>.*)')
29
30
31class AbbrExtension(Extension):
32    """ Abbreviation Extension for Python-Markdown. """
33
34    def extendMarkdown(self, md, md_globals):
35        """ Insert AbbrPreprocessor before ReferencePreprocessor. """
36        md.preprocessors.add('abbr', AbbrPreprocessor(md), '<reference')
37
38
39class AbbrPreprocessor(Preprocessor):
40    """ Abbreviation Preprocessor - parse text for abbr references. """
41
42    def run(self, lines):
43        '''
44        Find and remove all Abbreviation references from the text.
45        Each reference is set as a new AbbrPattern in the markdown instance.
46
47        '''
48        new_text = []
49        for line in lines:
50            m = ABBR_REF_RE.match(line)
51            if m:
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)
56            else:
57                new_text.append(line)
58        return new_text
59
60    def _generate_pattern(self, text):
61        '''
62        Given a string, returns an regex pattern to match that string.
63
64        'HTML' -> r'(?P<abbr>[H][T][M][L])'
65
66        Note: we force each char as a literal match (in brackets) as we don't
67        know what they will be beforehand.
68
69        '''
70        chars = list(text)
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))
74
75
76class AbbrPattern(Pattern):
77    """ Abbreviation inline pattern. """
78
79    def __init__(self, pattern, title):
80        super(AbbrPattern, self).__init__(pattern)
81        self.title = title
82
83    def handleMatch(self, m):
84        abbr = etree.Element('abbr')
85        abbr.text = AtomicString(m.group('abbr'))
86        abbr.set('title', self.title)
87        return abbr
88
89
90def makeExtension(*args, **kwargs):
91    return AbbrExtension(*args, **kwargs)
92