aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaurice van der Pot <griffon26@kfk4ever.com>2016-02-26 19:32:38 +0100
committerMaurice van der Pot <griffon26@kfk4ever.com>2016-02-26 19:56:01 +0100
commit429cc98556bd399268a375a4dfd58e387be9f6e0 (patch)
treededdc4840f7ce0295822bff93299ee690f2f8248
parent81b724cfec03634c0bcd88b1bccb5936872d04e2 (diff)
downloadmarkdown-429cc98556bd399268a375a4dfd58e387be9f6e0.tar.gz
markdown-429cc98556bd399268a375a4dfd58e387be9f6e0.tar.bz2
markdown-429cc98556bd399268a375a4dfd58e387be9f6e0.zip
Improve RawHtmlProcessor to have linear iso quadratic performance
-rw-r--r--markdown/postprocessors.py17
1 files changed, 10 insertions, 7 deletions
diff --git a/markdown/postprocessors.py b/markdown/postprocessors.py
index 2d4dcb5..8b311b2 100644
--- a/markdown/postprocessors.py
+++ b/markdown/postprocessors.py
@@ -10,6 +10,7 @@ processing.
from __future__ import absolute_import
from __future__ import unicode_literals
+from collections import OrderedDict
from . import util
from . import odict
import re
@@ -50,6 +51,7 @@ class RawHtmlPostprocessor(Postprocessor):
def run(self, text):
""" Iterate over html stash and restore "safe" html. """
+ replacements = OrderedDict()
for i in range(self.markdown.htmlStash.html_counter):
html, safe = self.markdown.htmlStash.rawHtmlBlocks[i]
if self.markdown.safeMode and not safe:
@@ -61,14 +63,15 @@ class RawHtmlPostprocessor(Postprocessor):
html = self.markdown.html_replacement_text
if (self.isblocklevel(html) and
(safe or not self.markdown.safeMode)):
- text = text.replace(
- "<p>%s</p>" %
- (self.markdown.htmlStash.get_placeholder(i)),
+ replacements["<p>%s</p>" %
+ (self.markdown.htmlStash.get_placeholder(i))] = \
html + "\n"
- )
- text = text.replace(
- self.markdown.htmlStash.get_placeholder(i), html
- )
+ replacements[self.markdown.htmlStash.get_placeholder(i)] = html
+
+ if replacements:
+ pattern = re.compile("|".join(re.escape(k) for k in replacements))
+ text = pattern.sub(lambda m: replacements[m.group(0)], text)
+
return text
def escape(self, html):