Skip to content

Commit

Permalink
Add support for multiple description definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalmoksha committed Sep 2, 2024
1 parent 92efde0 commit 1977e63
Show file tree
Hide file tree
Showing 5 changed files with 334 additions and 3 deletions.
2 changes: 2 additions & 0 deletions script/cibuild
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ python3 spec_tests.py --no-normalize --spec ../../../src/tests/fixtures/wikilink
|| failed=1
python3 spec_tests.py --no-normalize --spec ../../../src/tests/fixtures/wikilinks_title_before_pipe.md "$PROGRAM_ARG -e wikilinks-title-before-pipe" \
|| failed=1
python3 spec_tests.py --no-normalize --spec ../../../src/tests/fixtures/description_lists.md "$PROGRAM_ARG -e description-lists" \
|| failed=1

python3 spec_tests.py --no-normalize --spec regression.txt "$PROGRAM_ARG" \
|| failed=1
Expand Down
2 changes: 1 addition & 1 deletion src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ impl<'o, 'c: 'o> HtmlFormatter<'o, 'c> {
self.cr()?;
self.output.write_all(b"<dl")?;
self.render_sourcepos(node)?;
self.output.write_all(b">")?;
self.output.write_all(b">\n")?;
} else {
self.output.write_all(b"</dl>\n")?;
}
Expand Down
21 changes: 21 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,27 @@ impl<'a, 'o, 'c: 'o> Parser<'a, 'o, 'c> {

*container = details;

true
} else if node_matches!(last_child, NodeValue::DescriptionItem(..)) {
let parent = last_child.parent().unwrap();
reopen_ast_nodes(parent);

let metadata = NodeDescriptionItem {
marker_offset: self.indent,
padding: 2,
};

let item = self.add_child(
parent,
NodeValue::DescriptionItem(metadata),
self.first_nonspace + 1,
);

let details =
self.add_child(item, NodeValue::DescriptionDetails, self.first_nonspace + 1);

*container = details;

true
} else {
false
Expand Down
4 changes: 2 additions & 2 deletions src/tests/description_lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn description_lists() {
": Definition 2\n"
),
concat!(
"<dl>",
"<dl>\n",
"<dt>Term 1</dt>\n",
"<dd>\n",
"<p>Definition 1</p>\n",
Expand All @@ -41,7 +41,7 @@ fn description_lists() {
"<ul>\n",
"<li>\n",
"<p>Nested</p>\n",
"<dl>",
"<dl>\n",
"<dt>Term 1</dt>\n",
"<dd>\n",
"<p>Definition 1</p>\n",
Expand Down
308 changes: 308 additions & 0 deletions src/tests/fixtures/description_lists.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
---
title: Description / defintition lists
based_on: /~https://github.com/jgm/commonmark-hs/blob/master/commonmark-extensions/test/definition_lists.md
---

## Definition lists

The term is given on a line by itself, followed by
one or more definitions. Each definition must begin
with `:` (after 0-2 spaces); subsequent lines must
be indented unless they are lazy paragraph
continuations.

The list is tight if there is no blank line between
the term and the first definition, otherwise loose.

```````````````````````````````` example
apple
: red fruit
orange
: orange fruit
.
<dl>
<dt>apple</dt>
<dd>red fruit
</dd>
<dt>orange</dt>
<dd>orange fruit
</dd>
</dl>
````````````````````````````````

Loose:

```````````````````````````````` example
apple
: red fruit
orange
: orange fruit
.
<dl>
<dt>apple</dt>
<dd>
<p>red fruit</p>
</dd>
<dt>orange</dt>
<dd>
<p>orange fruit</p>
</dd>
</dl>
````````````````````````````````

Indented marker:

```````````````````````````````` example
apple
: red fruit
orange
: orange fruit
.
<dl>
<dt>apple</dt>
<dd>red fruit
</dd>
<dt>orange</dt>
<dd>orange fruit
</dd>
</dl>
````````````````````````````````

```````````````````````````````` example
apple
: red fruit
orange
: orange fruit
.
<dl>
<dt>apple</dt>
<dd>
<p>red fruit</p>
</dd>
<dt>orange</dt>
<dd>
<p>orange fruit</p>
</dd>
</dl>
````````````````````````````````

Multiple blocks in a definition:

```````````````````````````````` example
*apple*
: red fruit
contains seeds,
crisp, pleasant to taste
*orange*
: orange fruit
{ orange code block }
> orange block quote
.
<dl>
<dt><em>apple</em></dt>
<dd>
<p>red fruit</p>
<p>contains seeds,
crisp, pleasant to taste</p>
</dd>
<dt><em>orange</em></dt>
<dd>
<p>orange fruit</p>
<pre><code>{ orange code block }
</code></pre>
<blockquote>
<p>orange block quote</p>
</blockquote>
</dd>
</dl>
````````````````````````````````

Nested lists:

```````````````````````````````` example
term
: 1. Para one
Para two
.
<dl>
<dt>term</dt>
<dd>
<ol>
<li><p>Para one</p>
<p>Para two</p></li>
</ol>
</dd>
</dl>
````````````````````````````````

Multiple definitions, tight:

```````````````````````````````` example
apple
: red fruit
: computer company
orange
: orange fruit
: telecom company
.
<dl>
<dt>apple</dt>
<dd>red fruit
</dd>
<dd>computer company
</dd>
<dt>orange</dt>
<dd>orange fruit
</dd>
<dd>telecom company
</dd>
</dl>
````````````````````````````````

Multiple definitions, loose:

```````````````````````````````` example
apple
: red fruit
: computer company
orange
: orange fruit
: telecom company
.
<dl>
<dt>apple</dt>
<dd>
<p>red fruit</p>
</dd>
<dd>
<p>computer company</p>
</dd>
<dt>orange</dt>
<dd>
<p>orange fruit</p>
</dd>
<dd>
<p>telecom company</p>
</dd>
</dl>
````````````````````````````````

Lazy line continuations:

```````````````````````````````` example
apple
: red fruit
: computer
company
orange
: orange
fruit
: telecom company
.
<dl>
<dt>apple</dt>
<dd>
<p>red fruit</p>
</dd>
<dd>
<p>computer
company</p>
</dd>
<dt>orange</dt>
<dd>
<p>orange
fruit</p>
</dd>
<dd>
<p>telecom company</p>
</dd>
</dl>
````````````````````````````````



`~` may be used as a marker instead of `:`:

```````````````````````````````` example
apple
~ red fruit
orange
~ orange fruit
.
<dl>
<dt>apple</dt>
<dd>red fruit
</dd>
<dt>orange</dt>
<dd>orange fruit
</dd>
</dl>
````````````````````````````````

Definition terms may span multiple lines:

```````````````````````````````` example
a
b\
c
: foo
.
<dl>
<dt>a
b<br />
c</dt>
<dd>
<p>foo</p>
</dd>
</dl>
````````````````````````````````

Definition list with preceding paragraph
(</~https://github.com/jgm/commonmark-hs/issues/35>):

```````````````````````````````` example
Foo
bar
: baz
bim
: bor
.
<p>Foo</p>
<dl>
<dt>bar</dt>
<dd>baz
</dd>
<dt>bim</dt>
<dd>bor
</dd>
</dl>
````````````````````````````````

0 comments on commit 1977e63

Please sign in to comment.