mirror of https://github.com/zulip/zulip.git
Extract FencedBlockPreprocessor.format_code().
(imported from commit 3c98b9a7391a71e9c55a7b5c97abd0ee74178b44)
This commit is contained in:
parent
25c63f08de
commit
9d4b6d0684
|
@ -101,6 +101,32 @@ class FencedBlockPreprocessor(markdown.preprocessors.Preprocessor):
|
|||
self.checked_for_codehilite = False
|
||||
self.codehilite_conf = {}
|
||||
|
||||
def format_code(self, langclass, lang, text):
|
||||
# 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
|
||||
|
||||
# If config is not empty, then the codehighlite extension
|
||||
# is enabled, so we call it to highlite the code
|
||||
if self.codehilite_conf:
|
||||
highliter = CodeHilite(text,
|
||||
force_linenos=self.codehilite_conf['force_linenos'][0],
|
||||
guess_lang=self.codehilite_conf['guess_lang'][0],
|
||||
css_class=self.codehilite_conf['css_class'][0],
|
||||
style=self.codehilite_conf['pygments_style'][0],
|
||||
lang=(lang or None),
|
||||
noclasses=self.codehilite_conf['noclasses'][0])
|
||||
|
||||
code = highliter.hilite()
|
||||
else:
|
||||
code = CODE_WRAP % (langclass, self._escape(text))
|
||||
|
||||
return code
|
||||
|
||||
def process_fence(self, m, text):
|
||||
langclass = ''
|
||||
|
@ -116,20 +142,7 @@ class FencedBlockPreprocessor(markdown.preprocessors.Preprocessor):
|
|||
replacement = "\n\n".join(quoted_paragraphs)
|
||||
return '%s\n%s\n%s'% (text[:m.start()], replacement, text[m.end():])
|
||||
|
||||
# If config is not empty, then the codehighlite extension
|
||||
# is enabled, so we call it to highlite the code
|
||||
if self.codehilite_conf:
|
||||
highliter = CodeHilite(m.group('code'),
|
||||
force_linenos=self.codehilite_conf['force_linenos'][0],
|
||||
guess_lang=self.codehilite_conf['guess_lang'][0],
|
||||
css_class=self.codehilite_conf['css_class'][0],
|
||||
style=self.codehilite_conf['pygments_style'][0],
|
||||
lang=(m.group('lang') or None),
|
||||
noclasses=self.codehilite_conf['noclasses'][0])
|
||||
|
||||
code = highliter.hilite()
|
||||
else:
|
||||
code = CODE_WRAP % (langclass, self._escape(m.group('code')))
|
||||
code = self.format_code(langclass, m.group('lang'), m.group('code'))
|
||||
|
||||
placeholder = self.markdown.htmlStash.store(code, safe=True)
|
||||
return '%s\n%s\n%s'% (text[:m.start()], placeholder, text[m.end():])
|
||||
|
@ -137,15 +150,6 @@ class FencedBlockPreprocessor(markdown.preprocessors.Preprocessor):
|
|||
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)
|
||||
|
|
Loading…
Reference in New Issue