mirror of https://github.com/zulip/zulip.git
markdown: Tweak data-code-language testing and comments.
This should make it clearer the precise decisions we've made about the intended semantics of this feature.
This commit is contained in:
parent
b0c9e0a295
commit
ae58ed5a74
|
@ -395,23 +395,32 @@ class FencedBlockPreprocessor(markdown.preprocessors.Preprocessor):
|
||||||
else:
|
else:
|
||||||
code = CODE_WRAP.format(langclass, self._escape(text))
|
code = CODE_WRAP.format(langclass, self._escape(text))
|
||||||
|
|
||||||
# In order to display a "view-in-playground" option in the frontend,
|
# To support our "view in playground" feature, the frontend
|
||||||
# we need to know the language used in the codeblock. We tweak the HTML
|
# needs to know what Pygments language was used for
|
||||||
# CodeHilite generates to add this language as a data-attribute.
|
# highlighting this code block. We record this in a data
|
||||||
|
# attribute attached to the outer `pre` element.
|
||||||
|
# Unfortunately, the pygments API doesn't offer a way to add
|
||||||
|
# this, so we need to do it in a post-processing step.
|
||||||
if lang:
|
if lang:
|
||||||
parsed_code = etree.HTML(code)
|
parsed_code = etree.HTML(code)
|
||||||
div_tag = parsed_code[0][0]
|
div_tag = parsed_code[0][0]
|
||||||
# We get the lexer subclass name instead of directly processing the lang, to avoid
|
|
||||||
# different tags being generated for each of the lang's alias. Eg: `js` and `javascript`
|
# For the value of our data element, we get the lexer
|
||||||
# would now be mapped to `JavaScript`. In case no lexer with that alias is found, we
|
# subclass name instead of directly using the language,
|
||||||
# return back the text, wrapped in a data-codehilite tag.
|
# since that canonicalizes aliases (Eg: `js` and
|
||||||
|
# `javascript` will be mapped to `JavaScript`).
|
||||||
try:
|
try:
|
||||||
lexer_subclass_name = get_lexer_by_name(lang).name
|
code_language = get_lexer_by_name(lang).name
|
||||||
except ClassNotFound:
|
except ClassNotFound:
|
||||||
lexer_subclass_name = lang
|
# If there isn't a Pygments lexer by this name, we
|
||||||
div_tag.attrib['data-code-language'] = lexer_subclass_name
|
# still tag it with the user's data-code-language
|
||||||
# Lxml implicitly converts tags like <span></span> into <span/>
|
# value, since this allows hooking up a "playground"
|
||||||
# specifying method="c14n" when converting to string, prevents that.
|
# for custom "languages" that aren't known to Pygments.
|
||||||
|
code_language = lang
|
||||||
|
|
||||||
|
div_tag.attrib['data-code-language'] = code_language
|
||||||
|
# lxml implicitly converts tags like <span></span> into <span/>.
|
||||||
|
# Specifying method="c14n" when converting to string prevents that.
|
||||||
code = etree.tostring(div_tag, method="c14n").decode()
|
code = etree.tostring(div_tag, method="c14n").decode()
|
||||||
return code
|
return code
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,13 @@
|
||||||
"marked_expected_output": "<p>Hamlet said:</p>\n<div class=\"codehilite\"><pre><span></span><code>def speak(self):\n x = 1\n</code></pre></div>",
|
"marked_expected_output": "<p>Hamlet said:</p>\n<div class=\"codehilite\"><pre><span></span><code>def speak(self):\n x = 1\n</code></pre></div>",
|
||||||
"text_content": "Hamlet said:\ndef speak(self):\n x = 1\n"
|
"text_content": "Hamlet said:\ndef speak(self):\n x = 1\n"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "codeblock_hilite",
|
||||||
|
"input": "``` inventedlanguage\ndef speak(self):\n x = 1\n```",
|
||||||
|
"expected_output": "<div class=\"codehilite\" data-code-language=\"inventedlanguage\"><pre><span></span><code>def speak(self):\n x = 1\n</code></pre></div>",
|
||||||
|
"marked_expected_output": "<div class=\"codehilite\"><pre><span></span><code>def speak(self):\n x = 1\n</code></pre></div>",
|
||||||
|
"text_content": "def speak(self):\n x = 1\n"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "ampampamp",
|
"name": "ampampamp",
|
||||||
"input": "& & &amp;\n~~~~\n& & &amp;\n~~~~\n & & &amp;",
|
"input": "& & &amp;\n~~~~\n& & &amp;\n~~~~\n & & &amp;",
|
||||||
|
|
Loading…
Reference in New Issue