mirror of
https://github.com/zulip/zulip.git
synced 2025-11-10 00:46:03 +00:00
Refactor fence block code to never infinite loop
(imported from commit f72cb182e4fc9c4e8003853276d8aa40b454d08f)
This commit is contained in:
@@ -95,36 +95,8 @@ class FencedBlockPreprocessor(markdown.preprocessors.Preprocessor):
|
|||||||
self.checked_for_codehilite = False
|
self.checked_for_codehilite = False
|
||||||
self.codehilite_conf = {}
|
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 = ''
|
lang = ''
|
||||||
if m.group('lang'):
|
if m.group('lang'):
|
||||||
lang = LANG_TAG % 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')))
|
code = CODE_WRAP % (lang, self._escape(m.group('code')))
|
||||||
|
|
||||||
placeholder = self.markdown.htmlStash.store(code, safe=True)
|
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")
|
return text.split("\n")
|
||||||
|
|
||||||
def _escape(self, txt):
|
def _escape(self, txt):
|
||||||
|
|||||||
Reference in New Issue
Block a user