Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions tests/test_consecutive_examples.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 5 additions & 0 deletions vimdoc/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 19 additions & 6 deletions vimdoc/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand All @@ -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(
Expand Down
Loading