MCPcopy Create free account
hub / github.com/ronreiter/interactive-tutorials / AbbrPreprocessor

Class AbbrPreprocessor

markdown/extensions/abbr.py:40–74  ·  view source on GitHub ↗

Abbreviation Preprocessor - parse text for abbr references.

Source from the content-addressed store, hash-verified

38
39
40class AbbrPreprocessor(markdown.preprocessors.Preprocessor):
41 """ Abbreviation Preprocessor - parse text for abbr references. """
42
43 def run(self, lines):
44 '''
45 Find and remove all Abbreviation references from the text.
46 Each reference is set as a new AbbrPattern in the markdown instance.
47
48 '''
49 new_text = []
50 for line in lines:
51 m = ABBR_REF_RE.match(line)
52 if m:
53 abbr = m.group('abbr').strip()
54 title = m.group('title').strip()
55 self.markdown.inlinePatterns['abbr-%s'%abbr] = \
56 AbbrPattern(self._generate_pattern(abbr), title)
57 else:
58 new_text.append(line)
59 return new_text
60
61 def _generate_pattern(self, text):
62 '''
63 Given a string, returns an regex pattern to match that string.
64
65 'HTML' -> r'(?P<abbr>[H][T][M][L])'
66
67 Note: we force each char as a literal match (in brackets) as we don&#x27;t
68 know what they will be beforehand.
69
70 ''&#x27;
71 chars = list(text)
72 for i in range(len(chars)):
73 chars[i] = r'[%s]' % chars[i]
74 return r'(?P<abbr>\b%s\b)' % (r''.join(chars))
75
76
77class AbbrPattern(markdown.inlinepatterns.Pattern):

Callers 1

extendMarkdownMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected