diff --git a/tests/test_consecutive_examples.py b/tests/test_consecutive_examples.py new file mode 100644 index 0000000..2dccd08 --- /dev/null +++ b/tests/test_consecutive_examples.py @@ -0,0 +1,32 @@ +import io +import unittest + +from vimdoc import module +from vimdoc import output +from vimdoc import parser + + +class TestConsecutiveExamples(unittest.TestCase): + + def render(self, body): + source = ['""', '" @section Examples'] + ['" ' + line for line in body] + block, = parser.ParseBlocks(source, 'plugin/example.vim') + block.Local(namespace=None) + writer = output.Helpfile(module.Module('example', module.VimPlugin('example')), '.') + writer.file = io.StringIO() + writer.WriteParagraphs(block) + return writer.file.getvalue() + + def test_adjacent_examples_use_one_boundary_line(self): + actual = self.render(['Example: >', ' call Foo()', '< >', ' call Bar()', '<']) + self.assertEqual('Example:\n>\n call Foo()\n< >\n call Bar()\n<\n\n', actual) + + def test_three_adjacent_examples_have_two_boundaries(self): + actual = self.render(['>', ' call One()', '< >', ' call Two()', + '< >', ' call Three()', '<']) + self.assertEqual(2, actual.count('< >\n')) + + def test_explicitly_separated_examples_remain_separate(self): + actual = self.render(['>', ' call Foo()', '<', '', '>', ' call Bar()', '<']) + self.assertNotIn('< >', actual) + self.assertIn('<\n\n', actual) diff --git a/vimdoc/block.py b/vimdoc/block.py index b02113b..66970f7 100644 --- a/vimdoc/block.py +++ b/vimdoc/block.py @@ -98,6 +98,11 @@ def AddLine(self, line): return elif self.paragraphs.IsType(paragraph.ListItem): self.paragraphs.Close() + # Consecutive examples need no empty text paragraph between code blocks. + if (line == '>' and self.paragraphs + and isinstance(self.paragraphs[-1], paragraph.CodeBlock)): + self.paragraphs.SetType(paragraph.CodeBlock) + return # Everything else is text. self.paragraphs.SetType(paragraph.TextParagraph) # Lines ending in '>' enter code blocks. Must have a space before if it if diff --git a/vimdoc/output.py b/vimdoc/output.py index 6e2a267..6133049 100644 --- a/vimdoc/output.py +++ b/vimdoc/output.py @@ -138,8 +138,17 @@ def WriteFooter(self): def WriteParagraphs(self, block, indent=0): """Writes a series of text with optional indents.""" assert 'namespace' in block.locals - for p in block.paragraphs: - self.WriteParagraph(p, block.locals['namespace'], indent=indent) + for index, p in enumerate(block.paragraphs): + if isinstance(p, paragraph.CodeBlock): + previous_code = index > 0 and isinstance( + block.paragraphs[index - 1], paragraph.CodeBlock) + next_code = index + 1 < len(block.paragraphs) and isinstance( + block.paragraphs[index + 1], paragraph.CodeBlock) + self.WriteCodeBlock(p, block.locals['namespace'], indent=indent, + opening='< >' if previous_code else '>', + closing=not next_code) + else: + self.WriteParagraph(p, block.locals['namespace'], indent=indent) self.WriteLine() def WriteParagraph(self, p, namespace, indent=0): @@ -163,10 +172,7 @@ def WriteParagraph(self, p, namespace, indent=0): elif isinstance(p, paragraph.BlankLine): self.WriteLine() elif isinstance(p, paragraph.CodeBlock): - self.WriteLine('>') - for line in p.lines: - self.WriteCodeLine(line, namespace, indent=indent) - self.WriteLine('<') + self.WriteCodeBlock(p, namespace, indent=indent) elif isinstance(p, paragraph.DefaultLine): self.WriteLine(self.Default( p.arg, p.value, namespace), indent=indent) @@ -178,6 +184,13 @@ def WriteParagraph(self, p, namespace, indent=0): else: raise ValueError('What kind of paragraph is {}?'.format(p)) + def WriteCodeBlock(self, block, namespace, indent=0, opening='>', closing=True): + self.WriteLine(opening) + for line in block.lines: + self.WriteCodeLine(line, namespace, indent=indent) + if closing: + self.WriteLine('<') + def WriteCodeLine(self, text, namespace, indent=0): """Writes one line of code.""" wrapper = textwrap.TextWrapper(