summaryrefslogtreecommitdiff
path: root/src/mistune/plugins/speedup.py
diff options
context:
space:
mode:
authorArturs Artamonovs <dos21h@gmail.com>2023-01-29 10:30:54 +0000
committerArturs Artamonovs <dos21h@gmail.com>2023-01-29 10:30:54 +0000
commit66fa71a8f11b6ce5e8471b533f67cc3a1fdb85a8 (patch)
tree7aed7f385826a3bd88c76a373e28c6cfae4f396e /src/mistune/plugins/speedup.py
parent129c1201ea5c4418f0f89ad932633c7cea2439b7 (diff)
downloadmd-site-66fa71a8f11b6ce5e8471b533f67cc3a1fdb85a8.tar.gz
md-site-66fa71a8f11b6ce5e8471b533f67cc3a1fdb85a8.zip
Update to new mistune, removed old mistune, rewrite to python3
Diffstat (limited to 'src/mistune/plugins/speedup.py')
-rw-r--r--src/mistune/plugins/speedup.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/mistune/plugins/speedup.py b/src/mistune/plugins/speedup.py
new file mode 100644
index 0000000..784022c
--- /dev/null
+++ b/src/mistune/plugins/speedup.py
@@ -0,0 +1,44 @@
+import re
+import string
+
+# because mismatch is too slow, add parsers for paragraph and text
+
+HARD_LINEBREAK_RE = re.compile(r' *\n\s*')
+PARAGRAPH = (
+ # start with none punctuation, not number, not whitespace
+ r'(?:^[^\s\d' + re.escape(string.punctuation) + r'][^\n]*\n)+'
+)
+
+__all__ = ['speedup']
+
+
+
+def parse_text(inline, m, state):
+ text = m.group(0)
+ text = HARD_LINEBREAK_RE.sub('\n', text)
+ inline.process_text(text, state)
+ return m.end()
+
+
+def parse_paragraph(block, m, state):
+ text = m.group(0)
+ state.add_paragraph(text)
+ return m.end()
+
+
+def speedup(md):
+ """Increase the speed of parsing paragraph and inline text."""
+ md.block.register('paragraph', PARAGRAPH, parse_paragraph)
+
+ punc = r'\\><!\[_*`~\^\$='
+ text_pattern = r'[\s\S]+?(?=[' + punc + r']|'
+ if 'url_link' in md.inline.rules:
+ text_pattern += 'https?:|'
+
+ if md.inline.hard_wrap:
+ text_pattern += r' *\n|'
+ else:
+ text_pattern += r' {2,}\n|'
+
+ text_pattern += r'$)'
+ md.inline.register('text', text_pattern, parse_text)