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

Method run

markdown/extensions/fenced_code.py:91–176  ·  view source on GitHub ↗

Match and store Fenced Code Blocks in the `HtmlStash`.

(self, lines: list[str])

Source from the content-addressed store, hash-verified

89 ]
90
91 def run(self, lines: list[str]) -> list[str]:
92 """ Match and store Fenced Code Blocks in the `HtmlStash`. """
93
94 # Check for dependent extensions
95 if not self.checked_for_deps:
96 for ext in self.md.registeredExtensions:
97 if isinstance(ext, CodeHiliteExtension):
98 self.codehilite_conf = ext.getConfigs()
99 if isinstance(ext, AttrListExtension):
100 self.use_attr_list = True
101
102 self.checked_for_deps = True
103
104 text = "\n".join(lines)
105 # Collect the output in pieces, as rebuilding the full text after each block is quadratic.
106 pieces: list[str] = []
107 last = 0
108 index = 0
109 while 1:
110 m = self.FENCED_BLOCK_RE.search(text, index)
111 if m:
112 lang, id, classes, config = None, '', [], {}
113 if m.group('attrs'):
114 attrs, remainder = get_attrs_and_remainder(m.group('attrs'))
115 if remainder: # Does not have correctly matching curly braces, so the syntax is invalid.
116 index = m.end('attrs') # Explicitly skip over this, to prevent an infinite loop.
117 continue
118 id, classes, config = self.handle_attrs(attrs)
119 if len(classes):
120 lang = classes.pop(0)
121 else:
122 if m.group('lang'):
123 lang = m.group('lang')
124 if m.group('hl_lines'):
125 # Support `hl_lines` outside of `attrs` for backward-compatibility
126 config['hl_lines'] = parse_hl_lines(m.group('hl_lines'))
127
128 # If `config` is not empty, then the `codehighlite` extension
129 # is enabled, so we call it to highlight the code
130 if self.codehilite_conf and self.codehilite_conf['use_pygments'] and config.get('use_pygments', True):
131 local_config = self.codehilite_conf.copy()
132 local_config.update(config)
133 # Combine classes with `cssclass`. Ensure `cssclass` is at end
134 # as Pygments appends a suffix under certain circumstances.
135 # Ignore ID as Pygments does not offer an option to set it.
136 if classes:
137 local_config['css_class'] = '{} {}'.format(
138 ' '.join(classes),
139 local_config['css_class']
140 )
141 highliter = CodeHilite(
142 m.group('code'),
143 lang=lang,
144 style=local_config.pop('pygments_style', 'default'),
145 **local_config
146 )
147
148 code = highliter.hilite(shebang=False)

Callers

nothing calls this directly

Calls 11

handle_attrsMethod · 0.95
hiliteMethod · 0.95
_escapeMethod · 0.95
get_attrs_and_remainderFunction · 0.85
parse_hl_linesFunction · 0.85
CodeHiliteClass · 0.85
_escape_attrib_htmlFunction · 0.85
sanitize_nameFunction · 0.85
getConfigsMethod · 0.80
searchMethod · 0.80
storeMethod · 0.80

Tested by

no test coverage detected