mirror of
https://github.com/zulip/zulip.git
synced 2025-11-02 21:13:36 +00:00
Support arbitrarily nested fenced quote/code blocks.
Now we can nest fenced code/quote blocks inside of quote blocks down to arbitrary depths. Code blocks are always leafs. Fenced blocks start with at least three tildes or backticks, and the clump of punctuation then becomes the terminator for the block. If the user ends their message without terminators, all blocks are automatically closed. When inside a quote block, you can start another fenced block with any header that doesn't match the end-string of the outer block. (If you don't want to specify a language, then you can change the number of backticks/tildes to avoid amiguity.) Most of the heavy lifting happens in FencedBlockPreprocessor.run(). The parser works by pushing handlers on to a stack and popping them off when the ends of blocks are encountered. Parents communicate with their children by passing in a simple Python list of strings for the child to append to. Handlers also maintain their own lists for their own content, and when their done() method is called, they render their data as needed. The handlers are objects returned by functions, and the handler functions close on variables push, pop, and processor. The closure style here makes the handlers pretty tightly coupled to the outer run() method. If we wanted to move to a class-based style, the tradeoff would be that the class instances would have to marshall push/pop/processor etc., but we could test the components more easily in isolation. Dealing with blank lines is very fiddly inside of bugdown. The new functionality here is captured in the test BugdownTest.test_complexly_nested_quote(). (imported from commit 53886c8de74bdf2bbd3cef8be9de25f05bddb93c)
This commit is contained in:
@@ -2846,7 +2846,7 @@ class FencedBlockPreprocessorTest(TestCase):
|
||||
|
||||
# Simulate code formatting.
|
||||
processor.format_code = lambda lang, code: lang + ':' + code
|
||||
processor.placeholder = lambda s: '(' + s + ')'
|
||||
processor.placeholder = lambda s: '**' + s.strip('\n') + '**'
|
||||
|
||||
markdown = [
|
||||
'``` .py',
|
||||
@@ -2861,13 +2861,11 @@ class FencedBlockPreprocessorTest(TestCase):
|
||||
]
|
||||
expected = [
|
||||
'',
|
||||
'(py:hello()',
|
||||
')',
|
||||
'**py:hello()**',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
'(py:goodbye()',
|
||||
')',
|
||||
'**py:goodbye()**',
|
||||
'',
|
||||
'',
|
||||
''
|
||||
@@ -2880,7 +2878,7 @@ class FencedBlockPreprocessorTest(TestCase):
|
||||
|
||||
# Simulate code formatting.
|
||||
processor.format_code = lambda lang, code: lang + ':' + code
|
||||
processor.placeholder = lambda s: '(' + s + ')'
|
||||
processor.placeholder = lambda s: '**' + s.strip('\n') + '**'
|
||||
|
||||
markdown = [
|
||||
'~~~ quote',
|
||||
@@ -2895,10 +2893,7 @@ class FencedBlockPreprocessorTest(TestCase):
|
||||
'',
|
||||
'> hi',
|
||||
'',
|
||||
'> (py:hello()',
|
||||
'> )',
|
||||
'',
|
||||
'',
|
||||
'> **py:hello()**',
|
||||
'',
|
||||
'',
|
||||
''
|
||||
@@ -3047,6 +3042,70 @@ Thou canst not then be false to any man.</p>
|
||||
|
||||
self.common_bugdown_test(fenced_quote, expected_convert)
|
||||
|
||||
def test_complexly_nested_quote(self):
|
||||
fenced_quote = \
|
||||
"""I heard about this second hand...
|
||||
|
||||
~~~ quote
|
||||
|
||||
He said:
|
||||
~~~ quote
|
||||
The customer is complaining.
|
||||
|
||||
They looked at this code:
|
||||
``` .py
|
||||
def hello(): print 'hello
|
||||
```
|
||||
They would prefer:
|
||||
~~~ .rb
|
||||
def hello()
|
||||
puts 'hello'
|
||||
end
|
||||
~~~
|
||||
|
||||
Please advise.
|
||||
~~~
|
||||
|
||||
She said:
|
||||
~~~ quote
|
||||
Just send them this:
|
||||
``` .sh
|
||||
echo "hello\n"
|
||||
```
|
||||
~~~"""
|
||||
expected = \
|
||||
"""<p>I heard about this second hand...</p>
|
||||
<blockquote>
|
||||
<p>He said:</p>
|
||||
<blockquote>
|
||||
<p>The customer is complaining.</p>
|
||||
<p>They looked at this code:</p>
|
||||
<div class="codehilite"><pre><span class="k">def</span> <span class="nf">hello</span><span class="p">():</span> <span class="k">print</span> <span class="s">'hello</span>
|
||||
</pre></div>
|
||||
|
||||
|
||||
<p>They would prefer:</p>
|
||||
<div class="codehilite"><pre><span class="k">def</span> <span class="nf">hello</span><span class="p">()</span>
|
||||
<span class="nb">puts</span> <span class="s1">'hello'</span>
|
||||
<span class="k">end</span>
|
||||
</pre></div>
|
||||
|
||||
|
||||
<p>Please advise.</p>
|
||||
</blockquote>
|
||||
<p>She said:</p>
|
||||
<blockquote>
|
||||
<p>Just send them this:</p>
|
||||
<div class="codehilite"><pre><span class="nb">echo</span> <span class="s2">"hello</span>
|
||||
<span class="s2">"</span>
|
||||
</pre></div>
|
||||
|
||||
|
||||
</blockquote>
|
||||
</blockquote>"""
|
||||
|
||||
self.common_bugdown_test(fenced_quote, expected)
|
||||
|
||||
def test_dangerous_block(self):
|
||||
fenced_code = u'xxxxxx xxxxx xxxxxxxx xxxx. x xxxx xxxxxxxxxx:\n\n```\
|
||||
"xxxx xxxx\\xxxxx\\xxxxxx"```\n\nxxx xxxx xxxxx:```xx.xxxxxxx(x\'^xxxx$\'\
|
||||
|
||||
Reference in New Issue
Block a user