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

Method _parseHeader

markdown/extensions/codehilite.py:194–247  ·  view source on GitHub ↗

Determines language of a code block from shebang line and whether the said line should be removed or left in place. If the shebang line contains a path (even a single /) then it is assumed to be a real shebang line and left alone. However, if no path is given

(self)

Source from the content-addressed store, hash-verified

192 )
193
194 def _parseHeader(self) -> None:
195 """
196 Determines language of a code block from shebang line and whether the
197 said line should be removed or left in place. If the shebang line
198 contains a path (even a single /) then it is assumed to be a real
199 shebang line and left alone. However, if no path is given
200 (e.i.: `#!python` or `:::python`) then it is assumed to be a mock shebang
201 for language identification of a code fragment and removed from the
202 code block prior to processing for code highlighting. When a mock
203 shebang (e.i: `#!python`) is found, line numbering is turned on. When
204 colons are found in place of a shebang (e.i.: `:::python`), line
205 numbering is left in the current state - off by default.
206
207 Also parses optional list of highlight lines, like:
208
209 :::python hl_lines="1 3"
210 """
211
212 import re
213
214 # split text into lines
215 lines = self.src.split("\n")
216 # pull first line to examine
217 fl = lines.pop(0)
218
219 c = re.compile(r'''
220 (?:(?:^::+)|(?P<shebang>^[#]!)) # Shebang or 2 or more colons
221 (?P<path>(?:/\w+)*[/ ])? # Zero or 1 path
222 (?P<lang>[\w#.+-]*) # The language
223 \s* # Arbitrary whitespace
224 # Optional highlight lines, single- or double-quote-delimited
225 (hl_lines=(?P<quot>"|&#x27;)(?P<hl_lines>.*?)(?P=quot))?
226 ''&#x27;, re.VERBOSE)
227 # search first line for shebang
228 m = c.search(fl)
229 if m:
230 # we have a match
231 try:
232 self.lang = m.group('lang').lower()
233 except IndexError: # pragma: no cover
234 self.lang = None
235 if m.group('path'):
236 # path exists - restore first line
237 lines.insert(0, fl)
238 if self.options['linenos'] is None and m.group('shebang'):
239 # Overridable and Shebang exists - use line numbers
240 self.options['linenos'] = True
241
242 self.options['hl_lines'] = parse_hl_lines(m.group('hl_lines'))
243 else:
244 # No match
245 lines.insert(0, fl)
246
247 self.src = "\n".join(lines).strip("\n")
248
249
250# ------------------ The Markdown Extension -------------------------------

Callers 1

hiliteMethod · 0.95

Calls 1

parse_hl_linesFunction · 0.85

Tested by

no test coverage detected