Skip to content

Commit

Permalink
Implement tight / loose
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalmoksha committed Nov 24, 2024
1 parent 2c35b13 commit 54668df
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 51 deletions.
1 change: 1 addition & 0 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ impl<'o> HtmlFormatter<'o> {
.map(|n| n.data.borrow().value.clone())
{
Some(NodeValue::List(nl)) => nl.tight,
Some(NodeValue::DescriptionItem(nd)) => nd.tight,
_ => false,
};

Expand Down
4 changes: 4 additions & 0 deletions src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ pub struct NodeDescriptionItem {

/// Number of characters between the start of the list marker and the item text (including the list marker(s)).
pub padding: usize,

/// Whether the list is [tight](https://github.github.com/gfm/#tight), i.e. whether the
/// paragraphs are wrapped in `<p>` tags when formatted as HTML.
pub tight: bool,
}

/// The type of list.
Expand Down
21 changes: 13 additions & 8 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1640,7 +1640,7 @@ impl<'a, 'o> Parser<'a, 'o> {
scanners::description_item_start(&line[self.first_nonspace..]),
&mut matched,
)
&& self.parse_desc_list_details(container)
&& self.parse_desc_list_details(container, matched)
{
let offset = self.first_nonspace + matched - self.offset;
self.advance_offset(line, offset, false);
Expand Down Expand Up @@ -1884,14 +1884,14 @@ impl<'a, 'o> Parser<'a, 'o> {
}
}

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

fn parse_desc_list_details(&mut self, container: &mut &'a AstNode<'a>, matched: usize) -> bool {
let mut tight = false;
let last_child = match container.last_child() {
Some(lc) => lc,
None => {
// Happens when the detail line is directly after the term,
// without a blank line between.
tight = true;
*container = container.parent().unwrap();
container.last_child().unwrap()
}
Expand All @@ -1918,7 +1918,7 @@ impl<'a, 'o> Parser<'a, 'o> {
// All are incorrect; they all give the start line/col of
// the DescriptionDetails, and the end line/col is completely off.
//
// descriptionDetails:
// DescriptionDetails:
// Same as the DescriptionItem. All but last, the end line/col
// is (l+1):0.
//
Expand All @@ -1941,7 +1941,8 @@ impl<'a, 'o> Parser<'a, 'o> {

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

let item = self.add_child(
Expand All @@ -1961,11 +1962,15 @@ impl<'a, 'o> Parser<'a, 'o> {
true
} else if node_matches!(last_child, NodeValue::DescriptionItem(..)) {
let parent = last_child.parent().unwrap();
reopen_ast_nodes(parent);
let tight = match last_child.data.borrow().value {
NodeValue::DescriptionItem(ref ndi) => ndi.tight,
_ => false,
};

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

let item = self.add_child(
Expand Down
50 changes: 49 additions & 1 deletion src/tests/description_lists.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

#[test]
fn description_lists() {
fn description_lists_loose() {
html_opts!(
[extension.description_lists],
concat!(
Expand Down Expand Up @@ -57,6 +57,54 @@ fn description_lists() {
);
}

#[test]
fn description_lists_tight() {
html_opts!(
[extension.description_lists],
concat!(
"Term 1\n",
": Definition 1\n",
"\n",
"Term 2 with *inline markup*\n",
": Definition 2\n"
),
concat!(
"<dl>\n",
"<dt>Term 1</dt>\n",
"<dd>Definition 1</dd>\n",
"<dt>Term 2 with <em>inline markup</em></dt>\n",
"<dd>Definition 2</dd>\n",
"</dl>\n",
),
no_roundtrip,
);

html_opts!(
[extension.description_lists],
concat!(
"* Nested\n",
"\n",
" Term 1\n",
" : Definition 1\n\n",
" Term 2 with *inline markup*\n",
" : Definition 2\n\n"
),
concat!(
"<ul>\n",
"<li>\n",
"<p>Nested</p>\n",
"<dl>\n",
"<dt>Term 1</dt>\n",
"<dd>Definition 1</dd>\n",
"<dt>Term 2 with <em>inline markup</em></dt>\n",
"<dd>Definition 2</dd>\n",
"</dl>\n",
"</li>\n",
"</ul>\n",
),
no_roundtrip,
);
}
#[test]
fn sourcepos() {
// TODO There's plenty of work to do here still. The test currently represents
Expand Down
56 changes: 14 additions & 42 deletions src/tests/fixtures/description_lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ with `:` (after 0-2 spaces); subsequent lines must
be indented unless they are lazy paragraph
continuations.

There is no distinction between a "tight" list or a
"loose" list. Definitions are always wrapped in `<p>`
tags.
The list is tight if there is no blank line between
the term and the first definition, otherwise loose.

```````````````````````````````` example
apple
Expand All @@ -24,13 +23,9 @@ orange
.
<dl>
<dt>apple</dt>
<dd>
<p>red fruit</p>
</dd>
<dd>red fruit</dd>
<dt>orange</dt>
<dd>
<p>orange fruit</p>
</dd>
<dd>orange fruit</dd>
</dl>
````````````````````````````````

Expand Down Expand Up @@ -68,13 +63,9 @@ orange
.
<dl>
<dt>apple</dt>
<dd>
<p>red fruit</p>
</dd>
<dd>red fruit</dd>
<dt>orange</dt>
<dd>
<p>orange fruit</p>
</dd>
<dd>orange fruit</dd>
</dl>
````````````````````````````````

Expand All @@ -101,8 +92,6 @@ orange

Multiple blocks in a definition:

Note that the column

```````````````````````````````` example
*apple*
Expand Down Expand Up @@ -161,7 +150,6 @@ term
````````````````````````````````

Multiple definitions, tight:
(always rendered as loose)

```````````````````````````````` example
apple
Expand All @@ -174,19 +162,11 @@ orange
.
<dl>
<dt>apple</dt>
<dd>
<p>red fruit</p>
</dd>
<dd>
<p>computer company</p>
</dd>
<dd>red fruit</dd>
<dd>computer company</dd>
<dt>orange</dt>
<dd>
<p>orange fruit</p>
</dd>
<dd>
<p>telecom company</p>
</dd>
<dd>orange fruit</dd>
<dd>telecom company</dd>
</dl>
````````````````````````````````

Expand Down Expand Up @@ -271,13 +251,9 @@ orange
.
<dl>
<dt>apple</dt>
<dd>
<p>red fruit</p>
</dd>
<dd>red fruit</dd>
<dt>orange</dt>
<dd>
<p>orange fruit</p>
</dd>
<dd>orange fruit</dd>
</dl>
````````````````````````````````

Expand Down Expand Up @@ -315,12 +291,8 @@ bim
<p>Foo</p>
<dl>
<dt>bar</dt>
<dd>
<p>baz</p>
</dd>
<dd>baz</dd>
<dt>bim</dt>
<dd>
<p>bor</p>
</dd>
<dd>bor</dd>
</dl>
````````````````````````````````

0 comments on commit 54668df

Please sign in to comment.