diff --git a/README.md b/README.md index eae3f59..9f9100b 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,8 @@ Available block directives include: - `@setting name` declares a setting (global or per-buffer configuration variable). - `@default arg=value` describes the default value of an optional arg. +- `@tag name` renders an arbitrary help tag at that position in the block. + The name is used literally, without a plugin-name prefix. - `@throws exception` describes the type of exceptions that a function or command may throw. - `@deprecated reason` marks a command or function as deprecated and excludes it @@ -173,6 +175,7 @@ enclosed in parenthesis. - `@flag(name)` generates a link to a flag defined in the plugin. - `@setting(name)` generates a link to a setting defined in the plugin. - `@section(id)` generates a link to a section defined in the plugin. +- `@tag(name)` renders an arbitrary help tag inline in prose. - `@dict(name)` generates a link to a dictionary defined in the plugin. - `@plugin(attr)` Outputs some plugin data, such as the name or author. `attr` must be one of `stylized`, `name`, or `author`. If the attr (and parenthesis) diff --git a/tests/test_arbitrary_tags.py b/tests/test_arbitrary_tags.py new file mode 100644 index 0000000..4a9d2f9 --- /dev/null +++ b/tests/test_arbitrary_tags.py @@ -0,0 +1,39 @@ +import io +import unittest + +from vimdoc import module +from vimdoc import output +from vimdoc import parser + + +class TestArbitraryTags(unittest.TestCase): + + def writer(self): + writer = output.Helpfile(module.Module('example', module.VimPlugin('example')), '.') + writer.file = io.StringIO() + return writer + + def test_tags_are_rendered_at_their_documented_position(self): + source = ['""', '" @section Introduction', '" Before the tags.', + '" @tag plugin[]', '" @tag plugin[commands]', '" After the tags.'] + block, = parser.ParseBlocks(source, 'plugin/example.vim') + block.Local(namespace=None) + writer = self.writer() + writer.WriteParagraphs(block) + text = writer.file.getvalue() + self.assertLess(text.index('Before the tags.'), text.index('*plugin[]*')) + self.assertLess(text.index('*plugin[]*'), text.index('*plugin[commands]*')) + self.assertLess(text.index('*plugin[commands]*'), text.index('After the tags.')) + + def test_inline_tag_is_not_prefixed_with_plugin_name(self): + self.assertEqual('Before *plugin[]* after', + self.writer().Expand('Before @tag(plugin[]) after', None)) + + def test_long_tag_is_not_wrapped_or_rejected(self): + tag = 'long-tag-' * 15 + block, = parser.ParseBlocks(['""', '" @section Introduction', '" @tag ' + tag], + 'plugin/example.vim') + block.Local(namespace=None) + writer = self.writer() + writer.WriteParagraphs(block) + self.assertIn('*' + tag + '*\n', writer.file.getvalue()) diff --git a/vimdoc/block.py b/vimdoc/block.py index b02113b..1772392 100644 --- a/vimdoc/block.py +++ b/vimdoc/block.py @@ -167,6 +167,10 @@ def Default(self, arg, value): self._optional_args.append(arg) self.paragraphs.SetType(paragraph.DefaultLine, arg, value) + def AddTag(self, tag): + """Adds an arbitrary help tag at the current position in the block.""" + self.paragraphs.SetType(paragraph.TagLine, tag) + def Except(self, typ, description): """Adds a line specifying that the code can throw a specific exception.""" description = description or '' diff --git a/vimdoc/docline.py b/vimdoc/docline.py index c5c3251..3744002 100644 --- a/vimdoc/docline.py +++ b/vimdoc/docline.py @@ -269,6 +269,16 @@ def Update(self, block): block.Except(self.error, self.description) +class Tag(BlockDirective): + REGEX = regex.stylizing_args + + def Assign(self, tag): + self.tag = tag + + def Update(self, block): + block.AddTag(self.tag) + + class Header(BlockDirective): """A header directive, like @usage @function or @command.""" @@ -407,6 +417,7 @@ def GenerateUsage(self, block): 'stylized': Stylized, 'subsection': SubSection, 'tagline': Tagline, + 'tag': Tag, 'throws': Throws, 'usage': Usage, } diff --git a/vimdoc/output.py b/vimdoc/output.py index 6e2a267..c53c7b4 100644 --- a/vimdoc/output.py +++ b/vimdoc/output.py @@ -170,6 +170,8 @@ def WriteParagraph(self, p, namespace, indent=0): elif isinstance(p, paragraph.DefaultLine): self.WriteLine(self.Default( p.arg, p.value, namespace), indent=indent) + elif isinstance(p, paragraph.TagLine): + self.Print(self.Tag(p.tag).rjust(self.WIDTH), wide=True) elif isinstance(p, paragraph.ExceptionLine): self.WriteLine(self.Throws( p.exception, p.description, namespace), indent=indent) @@ -256,7 +258,9 @@ def Expander(match): def ExpandInline(self, inline, element, namespace): """Expands inline directives, like @function().""" - if inline == 'section': + if inline == 'tag': + return None if element is None else self.Tag(element) + elif inline == 'section': return self.Link(self.Slug(element)) elif inline == 'function': # If a user says @function(#Foo) then that points to in#this#file#Foo. diff --git a/vimdoc/paragraph.py b/vimdoc/paragraph.py index 82f5637..c8671de 100644 --- a/vimdoc/paragraph.py +++ b/vimdoc/paragraph.py @@ -66,6 +66,13 @@ def __init__(self, arg, value): self.value = value +class TagLine(Paragraph): + def __init__(self, tag): + super(TagLine, self).__init__() + self.open = False + self.tag = tag + + class ListItem(TextParagraph): def __init__(self, leader='*', level=0): super(ListItem, self).__init__()