OLD | NEW |
| (Empty) |
1 ''' | |
2 Abbreviation Extension for Python-Markdown | |
3 ========================================== | |
4 | |
5 This extension adds abbreviation handling to Python-Markdown. | |
6 | |
7 Simple Usage: | |
8 | |
9 >>> import markdown | |
10 >>> text = """ | |
11 ... Some text with an ABBR and a REF. Ignore REFERENCE and ref. | |
12 ... | |
13 ... *[ABBR]: Abbreviation | |
14 ... *[REF]: Abbreviation Reference | |
15 ... """ | |
16 >>> print markdown.markdown(text, ['abbr']) | |
17 <p>Some text with an <abbr title="Abbreviation">ABBR</abbr> and a <abbr titl
e="Abbreviation Reference">REF</abbr>. Ignore REFERENCE and ref.</p> | |
18 | |
19 Copyright 2007-2008 | |
20 * [Waylan Limberg](http://achinghead.com/) | |
21 * [Seemant Kulleen](http://www.kulleen.org/) | |
22 | |
23 | |
24 ''' | |
25 | |
26 from __future__ import absolute_import | |
27 from __future__ import unicode_literals | |
28 from . import Extension | |
29 from ..preprocessors import Preprocessor | |
30 from ..inlinepatterns import Pattern | |
31 from ..util import etree | |
32 import re | |
33 | |
34 # Global Vars | |
35 ABBR_REF_RE = re.compile(r'[*]\[(?P<abbr>[^\]]*)\][ ]?:\s*(?P<title>.*)') | |
36 | |
37 class AbbrExtension(Extension): | |
38 """ Abbreviation Extension for Python-Markdown. """ | |
39 | |
40 def extendMarkdown(self, md, md_globals): | |
41 """ Insert AbbrPreprocessor before ReferencePreprocessor. """ | |
42 md.preprocessors.add('abbr', AbbrPreprocessor(md), '<reference') | |
43 | |
44 | |
45 class AbbrPreprocessor(Preprocessor): | |
46 """ Abbreviation Preprocessor - parse text for abbr references. """ | |
47 | |
48 def run(self, lines): | |
49 ''' | |
50 Find and remove all Abbreviation references from the text. | |
51 Each reference is set as a new AbbrPattern in the markdown instance. | |
52 | |
53 ''' | |
54 new_text = [] | |
55 for line in lines: | |
56 m = ABBR_REF_RE.match(line) | |
57 if m: | |
58 abbr = m.group('abbr').strip() | |
59 title = m.group('title').strip() | |
60 self.markdown.inlinePatterns['abbr-%s'%abbr] = \ | |
61 AbbrPattern(self._generate_pattern(abbr), title) | |
62 else: | |
63 new_text.append(line) | |
64 return new_text | |
65 | |
66 def _generate_pattern(self, text): | |
67 ''' | |
68 Given a string, returns an regex pattern to match that string. | |
69 | |
70 'HTML' -> r'(?P<abbr>[H][T][M][L])' | |
71 | |
72 Note: we force each char as a literal match (in brackets) as we don't | |
73 know what they will be beforehand. | |
74 | |
75 ''' | |
76 chars = list(text) | |
77 for i in range(len(chars)): | |
78 chars[i] = r'[%s]' % chars[i] | |
79 return r'(?P<abbr>\b%s\b)' % (r''.join(chars)) | |
80 | |
81 | |
82 class AbbrPattern(Pattern): | |
83 """ Abbreviation inline pattern. """ | |
84 | |
85 def __init__(self, pattern, title): | |
86 super(AbbrPattern, self).__init__(pattern) | |
87 self.title = title | |
88 | |
89 def handleMatch(self, m): | |
90 abbr = etree.Element('abbr') | |
91 abbr.text = m.group('abbr') | |
92 abbr.set('title', self.title) | |
93 return abbr | |
94 | |
95 def makeExtension(configs=None): | |
96 return AbbrExtension(configs=configs) | |
OLD | NEW |