mirror of
https://github.com/zulip/zulip.git
synced 2025-11-09 16:37:23 +00:00
zerver/lib/bugdown/fenced_code.py: Fix types.
Change some important string literals from str to unicode. Annotate format_code and codehilite_conf in FencedBlockProcessor.
This commit is contained in:
committed by
Tim Abbott
parent
48c5b299b6
commit
7db0765a18
@@ -66,10 +66,11 @@ Dependencies:
|
|||||||
import re
|
import re
|
||||||
import markdown
|
import markdown
|
||||||
from zerver.lib.bugdown.codehilite import CodeHilite, CodeHiliteExtension
|
from zerver.lib.bugdown.codehilite import CodeHilite, CodeHiliteExtension
|
||||||
from typing import Dict, List, Sequence
|
from six import text_type
|
||||||
|
from typing import Any, Dict, List, Sequence
|
||||||
|
|
||||||
# Global vars
|
# Global vars
|
||||||
FENCE_RE = re.compile(r"""
|
FENCE_RE = re.compile(u"""
|
||||||
# ~~~ or ```
|
# ~~~ or ```
|
||||||
(?P<fence>
|
(?P<fence>
|
||||||
^(?:~{3,}|`{3,})
|
^(?:~{3,}|`{3,})
|
||||||
@@ -78,19 +79,19 @@ FENCE_RE = re.compile(r"""
|
|||||||
[ ]* # spaces
|
[ ]* # spaces
|
||||||
|
|
||||||
(
|
(
|
||||||
\{?\.?
|
\\{?\\.?
|
||||||
(?P<lang>
|
(?P<lang>
|
||||||
[a-zA-Z0-9_+-]*
|
[a-zA-Z0-9_+-]*
|
||||||
) # "py" or "javascript"
|
) # "py" or "javascript"
|
||||||
\}?
|
\\}?
|
||||||
) # language, like ".py" or "{javascript}"
|
) # language, like ".py" or "{javascript}"
|
||||||
[ ]* # spaces
|
[ ]* # spaces
|
||||||
$
|
$
|
||||||
""", re.VERBOSE)
|
""", re.VERBOSE)
|
||||||
|
|
||||||
|
|
||||||
CODE_WRAP = '<pre><code%s>%s</code></pre>'
|
CODE_WRAP = u'<pre><code%s>%s</code></pre>'
|
||||||
LANG_TAG = ' class="%s"'
|
LANG_TAG = u' class="%s"'
|
||||||
|
|
||||||
class FencedCodeExtension(markdown.Extension):
|
class FencedCodeExtension(markdown.Extension):
|
||||||
|
|
||||||
@@ -115,7 +116,7 @@ class FencedBlockPreprocessor(markdown.preprocessors.Preprocessor):
|
|||||||
markdown.preprocessors.Preprocessor.__init__(self, md)
|
markdown.preprocessors.Preprocessor.__init__(self, md)
|
||||||
|
|
||||||
self.checked_for_codehilite = False
|
self.checked_for_codehilite = False
|
||||||
self.codehilite_conf = {} # type: Dict[str, Dict[int, str]]
|
self.codehilite_conf = {} # type: Dict[str, List[Any]]
|
||||||
|
|
||||||
def run(self, lines):
|
def run(self, lines):
|
||||||
""" Match and store Fenced Code Blocks in the HtmlStash. """
|
""" Match and store Fenced Code Blocks in the HtmlStash. """
|
||||||
@@ -221,9 +222,11 @@ class FencedBlockPreprocessor(markdown.preprocessors.Preprocessor):
|
|||||||
return output
|
return output
|
||||||
|
|
||||||
def format_code(self, lang, text):
|
def format_code(self, lang, text):
|
||||||
langclass = ''
|
# type: (text_type, text_type) -> text_type
|
||||||
if lang:
|
if lang:
|
||||||
langclass = LANG_TAG % (lang,)
|
langclass = LANG_TAG % (lang,)
|
||||||
|
else:
|
||||||
|
langclass = ''
|
||||||
|
|
||||||
# Check for code hilite extension
|
# Check for code hilite extension
|
||||||
if not self.checked_for_codehilite:
|
if not self.checked_for_codehilite:
|
||||||
|
|||||||
Reference in New Issue
Block a user