Skip to content

Commit

Permalink
[ruby/rdoc] Allow rich definition list labels for Markdown
Browse files Browse the repository at this point in the history
Previously, any sort of "rich" markup for a definition list's label
would cause the Markdown parser to not recognize a definition list:

```ruby
md = <<~md
`one`
:    This is a definition
md

doc = RDoc::Markdown.parse(md)
doc # => [doc: [para: "<code>one</code>\n: This is a definition"]]
```

This commit tweaks the grammar for Markdown definition lists so that
labels can include "rich" markup such as bold (`**`), code (```), etc:

```ruby
md = <<~md
`one`
:    This is a definition
md

doc = RDoc::Markdown.parse(md)
doc # => [doc: [list: NOTE [item: ["<code>one</code>"]; [para: "This is a definition"]]]]
```

The [PHP Markdown Extra][1] Spec does not seem to specify whether or not
this should be allowed, but it is allowed in the RDoc format:

```ruby
rdoc = <<~rdoc
+code+::
    This is a definition
rdoc

doc = RDoc::Markup.parse(rdoc)
doc # => [doc: [list: NOTE [item: ["+code+"]; [para: "This is a definition"]]]]
```

so accepting this change increases the parity of the two formats.

[1]: https://michelf.ca/projects/php-markdown/extra/#def-list

ruby/rdoc@8f943bbba4
  • Loading branch information
skipkayhil authored and nobu committed Mar 11, 2024
1 parent 7a398ad commit 08961ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/rdoc/markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16445,12 +16445,12 @@ def _DefinitionListItem
return _tmp
end

# DefinitionListLabel = StrChunk:label @Sp @Newline { label }
# DefinitionListLabel = Inline:label @Sp @Newline { label }
def _DefinitionListLabel

_save = self.pos
while true # sequence
_tmp = apply(:_StrChunk)
_tmp = apply(:_Inline)
label = @result
unless _tmp
self.pos = _save
Expand Down Expand Up @@ -16777,7 +16777,7 @@ def _DefinitionListDefinition
Rules[:_TableAlign] = rule_info("TableAlign", "< /:?-+:?/ > @Sp { text.start_with?(\":\") ? (text.end_with?(\":\") ? :center : :left) : (text.end_with?(\":\") ? :right : nil) }")
Rules[:_DefinitionList] = rule_info("DefinitionList", "&{ definition_lists? } DefinitionListItem+:list { RDoc::Markup::List.new :NOTE, *list.flatten }")
Rules[:_DefinitionListItem] = rule_info("DefinitionListItem", "DefinitionListLabel+:label DefinitionListDefinition+:defns { list_items = [] list_items << RDoc::Markup::ListItem.new(label, defns.shift) list_items.concat defns.map { |defn| RDoc::Markup::ListItem.new nil, defn } unless list_items.empty? list_items }")
Rules[:_DefinitionListLabel] = rule_info("DefinitionListLabel", "StrChunk:label @Sp @Newline { label }")
Rules[:_DefinitionListLabel] = rule_info("DefinitionListLabel", "Inline:label @Sp @Newline { label }")
Rules[:_DefinitionListDefinition] = rule_info("DefinitionListDefinition", "@NonindentSpace \":\" @Space Inlines:a @BlankLine+ { paragraph a }")
# :startdoc:
end
19 changes: 19 additions & 0 deletions test/rdoc/test_rdoc_markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,25 @@ def test_parse_definition_list_multi_line
assert_equal expected, doc
end

def test_parse_definition_list_rich_label
doc = parse <<-MD
`one`
: This is a definition
**two**
: This is another definition
MD

expected = doc(
list(:NOTE,
item(%w[<code>one</code>],
para("This is a definition")),
item(%w[*two*],
para("This is another definition"))))

assert_equal expected, doc
end

def test_parse_definition_list_no
@parser.definition_lists = false

Expand Down

0 comments on commit 08961ce

Please sign in to comment.