MCPcopy Index your code
hub / github.com/Python-Markdown/markdown / run

Method run

markdown/extensions/fenced_code.py:83–163  ·  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

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