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

Method run

markdown/extensions/fenced_code.py:91–171  ·  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 index = 0
106 while 1:
107 m = self.FENCED_BLOCK_RE.search(text, index)
108 if m:
109 lang, id, classes, config = None, '', [], {}
110 if m.group('attrs'):
111 attrs, remainder = get_attrs_and_remainder(m.group('attrs'))
112 if remainder: # Does not have correctly matching curly braces, so the syntax is invalid.
113 index = m.end('attrs') # Explicitly skip over this, to prevent an infinite loop.
114 continue
115 id, classes, config = self.handle_attrs(attrs)
116 if len(classes):
117 lang = classes.pop(0)
118 else:
119 if m.group('lang'):
120 lang = m.group('lang')
121 if m.group('hl_lines'):
122 # Support `hl_lines` outside of `attrs` for backward-compatibility
123 config['hl_lines'] = parse_hl_lines(m.group('hl_lines'))
124
125 # If `config` is not empty, then the `codehighlite` extension
126 # is enabled, so we call it to highlight the code
127 if self.codehilite_conf and self.codehilite_conf['use_pygments'] and config.get('use_pygments', True):
128 local_config = self.codehilite_conf.copy()
129 local_config.update(config)
130 # Combine classes with `cssclass`. Ensure `cssclass` is at end
131 # as Pygments appends a suffix under certain circumstances.
132 # Ignore ID as Pygments does not offer an option to set it.
133 if classes:
134 local_config['css_class'] = '{} {}'.format(
135 ' '.join(classes),
136 local_config['css_class']
137 )
138 highliter = CodeHilite(
139 m.group('code'),
140 lang=lang,
141 style=local_config.pop('pygments_style', 'default'),
142 **local_config
143 )
144
145 code = highliter.hilite(shebang=False)
146 else:
147 id_attr = lang_attr = class_attr = kv_pairs = ''
148 if lang:

Callers

nothing calls this directly

Calls 9

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
getConfigsMethod · 0.80
storeMethod · 0.80

Tested by

no test coverage detected