Skip to content

Commit

Permalink
Allow defintions to follow a term without a blank line
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalmoksha committed Sep 28, 2024
1 parent 30f6828 commit 2db5510
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 22 deletions.
20 changes: 15 additions & 5 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ pub struct ExtensionOptions {
/// let mut options = Options::default();
/// options.extension.description_lists = true;
/// assert_eq!(markdown_to_html("Term\n\n: Definition", &options),
/// "<dl><dt>Term</dt>\n<dd>\n<p>Definition</p>\n</dd>\n</dl>\n");
/// "<dl>\n<dt>Term</dt>\n<dd>\n<p>Definition</p>\n</dd>\n</dl>\n");
/// ```
pub description_lists: bool,

Expand Down Expand Up @@ -1503,10 +1503,13 @@ impl<'a, 'o, 'c: 'o> Parser<'a, 'o, 'c> {
container.data.borrow_mut().internal_offset = matched;
} else if !indented
&& self.options.extension.description_lists
&& line[self.first_nonspace] == b':'
&& unwrap_into(
scanners::description_item_start(&line[self.first_nonspace..]),
&mut matched,
)
&& self.parse_desc_list_details(container)
{
let offset = self.first_nonspace + 1 - self.offset;
let offset = self.first_nonspace + matched - self.offset;
self.advance_offset(line, offset, false);
if strings::is_space_or_tab(line[self.offset]) {
self.advance_offset(line, 1, true);
Expand Down Expand Up @@ -1748,10 +1751,17 @@ impl<'a, 'o, 'c: 'o> Parser<'a, 'o, 'c> {
}
}

fn parse_desc_list_details(&mut self, container: &mut &'a AstNode<'a>) -> bool {
fn parse_desc_list_details(&mut self, node: &mut &'a AstNode<'a>) -> bool {
let container = node;

let last_child = match container.last_child() {
Some(lc) => lc,
None => return false,
None => {
// Happens when the detail line is directly after the term,
// without a blank line between.
*container = container.parent().unwrap();
container.last_child().unwrap()
}
};

if node_matches!(last_child, NodeValue::Paragraph) {
Expand Down
10 changes: 10 additions & 0 deletions src/scanners.re
Original file line number Diff line number Diff line change
Expand Up @@ -431,4 +431,14 @@ pub fn tasklist(s: &[u8]) -> Option<(usize, u8)> {
*/
}

pub fn description_item_start(s: &[u8]) -> Option<usize> {
let mut cursor = 0;
let _marker = 0;
let len = s.len();
/*!re2c
[:~] ([ \t]+|[\r\n]) { return Some(cursor); }
* { return None; }
*/
}

// vim: set ft=rust:
100 changes: 99 additions & 1 deletion src/scanners.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 34 additions & 16 deletions src/tests/fixtures/description_lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ 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.
There is no distinction between a "tight" list or a
"loose" list. Definitions are always wrapped in `<p>`
tags.

```````````````````````````````` example
apple
Expand All @@ -23,10 +24,12 @@ orange
.
<dl>
<dt>apple</dt>
<dd>red fruit
<dd>
<p>red fruit</p>
</dd>
<dt>orange</dt>
<dd>orange fruit
<dd>
<p>orange fruit</p>
</dd>
</dl>
````````````````````````````````
Expand Down Expand Up @@ -65,10 +68,12 @@ orange
.
<dl>
<dt>apple</dt>
<dd>red fruit
<dd>
<p>red fruit</p>
</dd>
<dt>orange</dt>
<dd>orange fruit
<dd>
<p>orange fruit</p>
</dd>
</dl>
````````````````````````````````
Expand Down Expand Up @@ -96,6 +101,8 @@ orange

Multiple blocks in a definition:

Note that the column

```````````````````````````````` example
*apple*
Expand Down Expand Up @@ -144,14 +151,17 @@ term
<dt>term</dt>
<dd>
<ol>
<li><p>Para one</p>
<p>Para two</p></li>
<li>
<p>Para one</p>
<p>Para two</p>
</li>
</ol>
</dd>
</dl>
````````````````````````````````

Multiple definitions, tight:
(always rendered as loose)

```````````````````````````````` example
apple
Expand All @@ -164,14 +174,18 @@ orange
.
<dl>
<dt>apple</dt>
<dd>red fruit
<dd>
<p>red fruit</p>
</dd>
<dd>computer company
<dd>
<p>computer company</p>
</dd>
<dt>orange</dt>
<dd>orange fruit
<dd>
<p>orange fruit</p>
</dd>
<dd>telecom company
<dd>
<p>telecom company</p>
</dd>
</dl>
````````````````````````````````
Expand Down Expand Up @@ -257,10 +271,12 @@ orange
.
<dl>
<dt>apple</dt>
<dd>red fruit
<dd>
<p>red fruit</p>
</dd>
<dt>orange</dt>
<dd>orange fruit
<dd>
<p>orange fruit</p>
</dd>
</dl>
````````````````````````````````
Expand Down Expand Up @@ -299,10 +315,12 @@ bim
<p>Foo</p>
<dl>
<dt>bar</dt>
<dd>baz
<dd>
<p>baz</p>
</dd>
<dt>bim</dt>
<dd>bor
<dd>
<p>bor</p>
</dd>
</dl>
````````````````````````````````

0 comments on commit 2db5510

Please sign in to comment.