From bb09de93520ec43b23b70c99c1f5439a16cb255e Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sun, 1 Apr 2018 06:39:37 +0000 Subject: md_tw.py: Update TWMarkdown.output_block_quote. Add subsequent indent text to all 'block' tokens inside block quote. * md_tw.py (TWMarkdown.output_block_quote): Update method. (TWRenderer.block_quote): Update method. * tests/data/renderer-block-quote-w.md: New file. * tests/data/renderer-block-quote.md: New file. * tests/data/renderer-fences-w.md: Update file. * tests/test_md_tw.py (test_render_block-_quote): New test. --- md_tw.py | 28 ++++++++++++++++++++++++++-- tests/data/renderer-block-quote-w.md | 34 ++++++++++++++++++++++++++++++++++ tests/data/renderer-block-quote.md | 34 ++++++++++++++++++++++++++++++++++ tests/data/renderer-fences-w.md | 1 + tests/test_md_tw.py | 6 ++++++ 5 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 tests/data/renderer-block-quote-w.md create mode 100644 tests/data/renderer-block-quote.md diff --git a/md_tw.py b/md_tw.py index 866f1fa..0ed992e 100644 --- a/md_tw.py +++ b/md_tw.py @@ -270,7 +270,7 @@ class TWRenderer(mistune.Renderer): return out def block_quote(self, text): - out = '{}\n'.format(text.rstrip('>\n')) + out = '{}\n\n'.format(text) return out def block_html(self, html): @@ -368,8 +368,32 @@ class TWMarkdown(mistune.Markdown): # Add prefix self._add_prefix('> ') + def process(): + if self.token['type'] == 'text': + txt = self.renderer.tw_fill(self.tok_text()) + else: + # Append subsequent indent. + txt = ''.join([ + self.tok().rstrip(), + '\n', + self.renderer.tw_get('subsequent_indent'), + '\n' + ]) + + return txt + + body = self.renderer.placeholder() + while self.pop()['type'] != 'block_quote_end': + body += process() + + # Remove last trailing subsequent indent. + body = body.rstrip( + self.renderer.tw_get('subsequent_indent') + + '\n' + ) + # Render block quote - rendered_bq = super(TWMarkdown, self).output_block_quote() + rendered_bq = self.renderer.block_quote(body) # Remove prefix self._remove_prefix(len('> ')) diff --git a/tests/data/renderer-block-quote-w.md b/tests/data/renderer-block-quote-w.md new file mode 100644 index 0000000..c4bb232 --- /dev/null +++ b/tests/data/renderer-block-quote-w.md @@ -0,0 +1,34 @@ +> This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet, +> consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus. +> Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus. +> +> Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse +> id sem consectetuer libero luctus adipiscing. + +Making Me Nervous + +> This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet, +> consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus. +> Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus. +> +> Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse +> id sem consectetuer libero luctus adipiscing. + +Bad Attraction + +> This is the first level of quoting. +> +> > This is nested blockquote. +> +> Back to the first level. + +Overreacting + +> ## This is a header. +> +> 1. This is the first list item. +> 2. This is the second list item. +> +> Here's some example code: +> +> return shell_exec("echo $input | $markdown_script"); diff --git a/tests/data/renderer-block-quote.md b/tests/data/renderer-block-quote.md new file mode 100644 index 0000000..5dce085 --- /dev/null +++ b/tests/data/renderer-block-quote.md @@ -0,0 +1,34 @@ +> This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet, +> consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus. +> Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus. +> +> Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse +> id sem consectetuer libero luctus adipiscing. + +Making Me Nervous + +> This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet, +consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus. +Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus. + +> Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse +id sem consectetuer libero luctus adipiscing. + +Bad Attraction + +> This is the first level of quoting. +> +> > This is nested blockquote. +> +> Back to the first level. + +Overreacting + +> ## This is a header. +> +> 1. This is the first list item. +> 2. This is the second list item. +> +> Here's some example code: +> +> return shell_exec("echo $input | $markdown_script"); diff --git a/tests/data/renderer-fences-w.md b/tests/data/renderer-fences-w.md index 5cbb95c..21959ba 100644 --- a/tests/data/renderer-fences-w.md +++ b/tests/data/renderer-fences-w.md @@ -9,4 +9,5 @@ $ rm -rf / > ```bash > $ :(){:|:&};: > ``` +> > For more look at jaramil.dyne.org. He's awesome. diff --git a/tests/test_md_tw.py b/tests/test_md_tw.py index 34fe766..64b59a9 100644 --- a/tests/test_md_tw.py +++ b/tests/test_md_tw.py @@ -797,6 +797,12 @@ class TestTWRenderer(object): self._validate(txt, expected_txt) + def test_render_block_quote(self): + txt = self._md('renderer-block-quote.md') + expected_txt = self._get('renderer-block-quote-w.md') + + self._validate(txt, expected_txt) + def test_render_block_html(self): txt = self._md('renderer-block-html.md') expected_txt = self._get('renderer-block-html-w.md') -- cgit v1.2.3