bugdown: Remove whitespace strip from inline code.

Fixes #4507
This commit is contained in:
Andy Perez
2017-11-22 01:27:19 +00:00
committed by showell
parent 99e62f80b9
commit f0190333b8
3 changed files with 34 additions and 4 deletions

View File

@@ -445,7 +445,7 @@ var inline = {
nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/, nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
strong: /^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/, strong: /^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,
em: /^\b_((?:[^_]|__)+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/, em: /^\b_((?:[^_]|__)+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,
code: /^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/, code: /^(`+)(\s*[\s\S]*?[^`]\s*)\1(?!`)/,
br: /^ {2,}\n(?!\s*$)/, br: /^ {2,}\n(?!\s*$)/,
del: noop, del: noop,
emoji: noop, emoji: noop,

View File

@@ -25,6 +25,12 @@
"expected_output": "<p>Hamlet once said</p>\n<div class=\"codehilite\"><pre><span></span>def func():\n x = 1\n\n y = 2\n\n z = 3\n</pre></div>\n\n\n<p>And all was good.</p>", "expected_output": "<p>Hamlet once said</p>\n<div class=\"codehilite\"><pre><span></span>def func():\n x = 1\n\n y = 2\n\n z = 3\n</pre></div>\n\n\n<p>And all was good.</p>",
"text_content": "Hamlet once said\ndef func():\n x = 1\n\n y = 2\n\n z = 3\n\n\n\nAnd all was good." "text_content": "Hamlet once said\ndef func():\n x = 1\n\n y = 2\n\n z = 3\n\n\n\nAnd all was good."
}, },
{
"name": "inline_code_spaces",
"input": "` outer ` ``` space ```",
"expected_output": "<p><code> outer </code> <code> space </code></p>",
"text_content": " outer space "
},
{ {
"name": "codeblock_backticks", "name": "codeblock_backticks",
"input": "\n```\nfenced code\n```\n\n```inline code```\n", "input": "\n```\nfenced code\n```\n\n```inline code```\n",
@@ -93,8 +99,8 @@
{ {
"name": "dangerous_block", "name": "dangerous_block",
"input": "``` one ```\n\n``` two ```\n\n~~~~\nx = 1", "input": "``` one ```\n\n``` two ```\n\n~~~~\nx = 1",
"expected_output": "<p><code>one</code></p>\n<p><code>two</code></p>\n<div class=\"codehilite\"><pre><span></span>x = 1\n</pre></div>", "expected_output": "<p><code> one </code></p>\n<p><code> two </code></p>\n<div class=\"codehilite\"><pre><span></span>x = 1\n</pre></div>",
"text_content": "one\ntwo\nx = 1\n" "text_content": " one \n two \nx = 1\n"
}, },
{ {
"name": "four_space_code_block", "name": "four_space_code_block",

View File

@@ -355,6 +355,24 @@ class InlineHttpsProcessor(markdown.treeprocessors.Treeprocessor):
continue continue
img.set("src", get_camo_url(url)) img.set("src", get_camo_url(url))
class BacktickPattern(markdown.inlinepatterns.Pattern):
""" Return a `<code>` element containing the matching text. """
def __init__(self, pattern):
# type: (Text) -> None
markdown.inlinepatterns.Pattern.__init__(self, pattern)
self.ESCAPED_BSLASH = '%s%s%s' % (markdown.util.STX, ord('\\'), markdown.util.ETX)
self.tag = 'code'
def handleMatch(self, m):
# type: (Match[Text]) -> Union[Text, Element]
if m.group(4):
el = markdown.util.etree.Element(self.tag)
# Modified to not strip whitespace
el.text = markdown.util.AtomicString(m.group(4))
return el
else:
return m.group(2).replace('\\\\', self.ESCAPED_BSLASH)
class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor): class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor):
TWITTER_MAX_IMAGE_HEIGHT = 400 TWITTER_MAX_IMAGE_HEIGHT = 400
TWITTER_MAX_TO_PREVIEW = 3 TWITTER_MAX_TO_PREVIEW = 3
@@ -1346,7 +1364,7 @@ class Bugdown(markdown.Extension):
for k in ('image_link', 'image_reference', 'automail', for k in ('image_link', 'image_reference', 'automail',
'autolink', 'link', 'reference', 'short_reference', 'autolink', 'link', 'reference', 'short_reference',
'escape', 'strong_em', 'emphasis', 'emphasis2', 'escape', 'strong_em', 'emphasis', 'emphasis2',
'linebreak', 'strong'): 'linebreak', 'strong', 'backtick'):
del md.inlinePatterns[k] del md.inlinePatterns[k]
try: try:
# linebreak2 was removed upstream in version 3.2.1, so # linebreak2 was removed upstream in version 3.2.1, so
@@ -1357,6 +1375,12 @@ class Bugdown(markdown.Extension):
md.preprocessors.add("custom_text_notifications", AlertWordsNotificationProcessor(md), "_end") md.preprocessors.add("custom_text_notifications", AlertWordsNotificationProcessor(md), "_end")
# Inline code block without whitespace stripping
md.inlinePatterns.add(
"backtick",
BacktickPattern(r'(?:(?<!\\)((?:\\{2})+)(?=`+)|(?<!\\)(`+)(.+?)(?<!`)\3(?!`))'),
"_begin")
# Custom bold syntax: **foo** but not __foo__ # Custom bold syntax: **foo** but not __foo__
md.inlinePatterns.add('strong', md.inlinePatterns.add('strong',
markdown.inlinepatterns.SimpleTagPattern(r'(\*\*)([^\n]+?)\2', 'strong'), markdown.inlinepatterns.SimpleTagPattern(r'(\*\*)([^\n]+?)\2', 'strong'),