Refactor fence block code to never infinite loop

(imported from commit f72cb182e4fc9c4e8003853276d8aa40b454d08f)
This commit is contained in:
Leo Franchi 2013-01-29 10:14:30 -05:00
parent 56e29ba11c
commit c6f73f7697
1 changed files with 43 additions and 34 deletions

View File

@ -95,36 +95,8 @@ class FencedBlockPreprocessor(markdown.preprocessors.Preprocessor):
self.checked_for_codehilite = False
self.codehilite_conf = {}
def run(self, lines):
""" Match and store Fenced Code Blocks in the HtmlStash. """
# Check for code hilite extension
if not self.checked_for_codehilite:
for ext in self.markdown.registeredExtensions:
if isinstance(ext, CodeHiliteExtension):
self.codehilite_conf = ext.config
break
self.checked_for_codehilite = True
text = "\n".join(lines)
while 1:
m = FENCED_BLOCK_RE.search(text)
if not m:
fence = FENCE_RE.search(text)
if fence:
# If we found a starting fence but no ending fence,
# then we add a closing fence before the two newlines that
# markdown automatically inserts
if text[-2:] == '\n\n':
text = text[:-2] + '\n' + fence.group('fence') + text[-2:]
else:
text += fence.group('fence')
continue
else:
break
def process_fence(self, m, text):
lang = ''
if m.group('lang'):
lang = LANG_TAG % m.group('lang')
@ -145,7 +117,44 @@ class FencedBlockPreprocessor(markdown.preprocessors.Preprocessor):
code = CODE_WRAP % (lang, self._escape(m.group('code')))
placeholder = self.markdown.htmlStash.store(code, safe=True)
text = '%s\n%s\n%s'% (text[:m.start()], placeholder, text[m.end():])
return '%s\n%s\n%s'% (text[:m.start()], placeholder, text[m.end():])
def run(self, lines):
""" Match and store Fenced Code Blocks in the HtmlStash. """
# Check for code hilite extension
if not self.checked_for_codehilite:
for ext in self.markdown.registeredExtensions:
if isinstance(ext, CodeHiliteExtension):
self.codehilite_conf = ext.config
break
self.checked_for_codehilite = True
text = "\n".join(lines)
end = 0
while 1:
m = FENCED_BLOCK_RE.search(text)
if m:
end = m.end()
text = self.process_fence(m, text)
else:
break
fence = FENCE_RE.search(text, end)
if fence:
# If we found a starting fence but no ending fence,
# then we add a closing fence before the two newlines that
# markdown automatically inserts
if text[-2:] == '\n\n':
text = text[:-2] + '\n' + fence.group('fence') + text[-2:]
else:
text += fence.group('fence')
m = FENCED_BLOCK_RE.search(text)
if m:
text = self.process_fence(m, text)
return text.split("\n")
def _escape(self, txt):