Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/mistune/plugins/speedup.py: 64%
25 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
1import re
2import string
4# because mismatch is too slow, add parsers for paragraph and text
6HARD_LINEBREAK_RE = re.compile(r' *\n\s*')
7PARAGRAPH = (
8 # start with none punctuation, not number, not whitespace
9 r'(?:^[^\s\d' + re.escape(string.punctuation) + r'][^\n]*\n)+'
10)
12__all__ = ['speedup']
16def parse_text(inline, m, state):
17 text = m.group(0)
18 text = HARD_LINEBREAK_RE.sub('\n', text)
19 inline.process_text(text, state)
20 return m.end()
23def parse_paragraph(block, m, state):
24 text = m.group(0)
25 state.add_paragraph(text)
26 return m.end()
29def speedup(md):
30 """Increase the speed of parsing paragraph and inline text."""
31 md.block.register('paragraph', PARAGRAPH, parse_paragraph)
33 punc = r'\\><!\[_*`~\^\$='
34 text_pattern = r'[\s\S]+?(?=[' + punc + r']|'
35 if 'url_link' in md.inline.rules:
36 text_pattern += 'https?:|'
38 if md.inline.hard_wrap:
39 text_pattern += r' *\n|'
40 else:
41 text_pattern += r' {2,}\n|'
43 text_pattern += r'$)'
44 md.inline.register('text', text_pattern, parse_text)