MCPcopy Create free account
hub / github.com/Python-Markdown/markdown / AbbrExtension

Class AbbrExtension

markdown/extensions/abbr.py:39–87  ·  view source on GitHub ↗

Abbreviation Extension for Python-Markdown.

Source from the content-addressed store, hash-verified

37
38
39class AbbrExtension(Extension):
40 """ Abbreviation Extension for Python-Markdown. """
41
42 def __init__(self, **kwargs):
43 """ Initiate Extension and set up configs. """
44 self.config = {
45 'glossary': [
46 {},
47 'A dictionary where the `key` is the abbreviation and the `value` is the definition.'
48 "Default: `{}`"
49 ],
50 }
51 """ Default configuration options. """
52 super().__init__(**kwargs)
53 self.abbrs = {}
54 self.glossary = {}
55
56 def reset(self):
57 """ Clear all previously defined abbreviations. """
58 self.abbrs.clear()
59 if (self.glossary):
60 self.abbrs.update(self.glossary)
61
62 def reset_glossary(self):
63 """ Clear all abbreviations from the glossary. """
64 self.glossary.clear()
65
66 def load_glossary(self, dictionary: dict[str, str]):
67 """Adds `dictionary` to our glossary. Any abbreviations that already exist will be overwritten."""
68 if dictionary:
69 self.glossary = {**dictionary, **self.glossary}
70
71 def extendMarkdown(self, md):
72 """
73 Register the processors.
74
75 | Class Instance | Registry | Name | Priority |
76 | ------------------------------------------------------------------- | ---------------------------------------------------------------- | ------ | :------: |
77 | [`AbbrTreeprocessor`][markdown.extensions.abbr.AbbrTreeprocessor] | [`treeprocessors`][markdown.treeprocessors.build_treeprocessors] | `abbr` | `7` |
78 | [`AbbrBlockprocessor`][markdown.extensions.abbr.AbbrBlockprocessor] | [`blockprocessors`][markdown.blockprocessors.build_block_parser] | `abbr` | `16` |
79
80 """
81 # flake8: noqa: E501 75-78
82 if (self.config['glossary'][0]):
83 self.load_glossary(self.config['glossary'][0])
84 self.abbrs.update(self.glossary)
85 md.registerExtension(self)
86 md.treeprocessors.register(AbbrTreeprocessor(md, self.abbrs), 'abbr', 7)
87 md.parser.blockprocessors.register(AbbrBlockprocessor(md.parser, self.abbrs), 'abbr', 16)
88
89
90class AbbrTreeprocessor(Treeprocessor):

Callers 4

test_abbr_glossaryMethod · 0.90
test_abbr_glossary_2Method · 0.90
test_abbr_resetMethod · 0.90
makeExtensionFunction · 0.85

Calls

no outgoing calls

Tested by 3

test_abbr_glossaryMethod · 0.72
test_abbr_glossary_2Method · 0.72
test_abbr_resetMethod · 0.72