From f96ea1d84be579d7c7552a2a0085fb810798944a Mon Sep 17 00:00:00 2001 From: digitalMoksha Date: Sat, 28 Sep 2024 12:58:46 -0500 Subject: [PATCH] Try and get tight / loose working --- src/html.rs | 11 ++--- src/nodes.rs | 4 ++ src/parser/mod.rs | 28 ++++++++++++- src/tests/fixtures/description_lists.md | 56 +++++++------------------ 4 files changed, 51 insertions(+), 48 deletions(-) diff --git a/src/html.rs b/src/html.rs index b759711c..635dd8b2 100644 --- a/src/html.rs +++ b/src/html.rs @@ -663,14 +663,15 @@ impl<'o> HtmlFormatter<'o> { .map(|n| n.data.borrow().value.clone()) { Some(NodeValue::List(nl)) => nl.tight, + Some(NodeValue::DescriptionItem(nd)) => nd.tight, _ => false, }; - let tight = tight - || matches!( - node.parent().map(|n| n.data.borrow().value.clone()), - Some(NodeValue::DescriptionTerm) - ); + // let tight = tight + // || matches!( + // node.parent().map(|n| n.data.borrow().value.clone()), + // Some(NodeValue::DescriptionTerm) + // ); if !tight { if entering { diff --git a/src/nodes.rs b/src/nodes.rs index ec6e549c..85407a7a 100644 --- a/src/nodes.rs +++ b/src/nodes.rs @@ -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 `

` tags when formatted as HTML. + pub tight: bool, } /// The type of list. diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 03b8d5de..c115de2e 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -1886,12 +1886,14 @@ impl<'a, 'o> Parser<'a, 'o> { fn parse_desc_list_details(&mut self, node: &mut &'a AstNode<'a>) -> bool { let container = node; - + 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() } @@ -1942,6 +1944,7 @@ impl<'a, 'o> Parser<'a, 'o> { let metadata = NodeDescriptionItem { marker_offset: self.indent, padding: 2, + tight, }; let item = self.add_child( @@ -1960,12 +1963,14 @@ impl<'a, 'o> Parser<'a, 'o> { true } else if node_matches!(last_child, NodeValue::DescriptionItem(..)) { + // ORIGINAL CODE let parent = last_child.parent().unwrap(); reopen_ast_nodes(parent); let metadata = NodeDescriptionItem { marker_offset: self.indent, padding: 2, + tight, }; let item = self.add_child( @@ -1980,6 +1985,27 @@ impl<'a, 'o> Parser<'a, 'o> { *container = details; true + + // ATTEMPT 1 + // reopen_ast_nodes(last_child); + // + // let details = + // self.add_child(last_child, NodeValue::DescriptionDetails, self.first_nonspace + 1); + // *container = details; + // + // true + + // ATTEMPT 2 + // let parent = last_child.parent().unwrap(); + // let item = parent.last_child().unwrap(); + // + // reopen_ast_nodes(item); + // + // let details = + // self.add_child(item, NodeValue::DescriptionDetails, self.first_nonspace + 1); + // *container = details; + // + // true } else { false } diff --git a/src/tests/fixtures/description_lists.md b/src/tests/fixtures/description_lists.md index a9ca0fc4..5f689014 100644 --- a/src/tests/fixtures/description_lists.md +++ b/src/tests/fixtures/description_lists.md @@ -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 `

` -tags. +The list is tight if there is no blank line between +the term and the first definition, otherwise loose. ```````````````````````````````` example apple @@ -24,13 +23,9 @@ orange .

apple
-
-

red fruit

-
+
red fruit
orange
-
-

orange fruit

-
+
orange fruit
```````````````````````````````` @@ -68,13 +63,9 @@ orange .
apple
-
-

red fruit

-
+
red fruit
orange
-
-

orange fruit

-
+
orange fruit
```````````````````````````````` @@ -101,8 +92,6 @@ orange Multiple blocks in a definition: -Note that the column - ```````````````````````````````` example *apple* @@ -161,7 +150,6 @@ term ```````````````````````````````` Multiple definitions, tight: -(always rendered as loose) ```````````````````````````````` example apple @@ -174,19 +162,11 @@ orange .
apple
-
-

red fruit

-
-
-

computer company

-
+
red fruit
+
computer company
orange
-
-

orange fruit

-
-
-

telecom company

-
+
orange fruit
+
telecom company
```````````````````````````````` @@ -271,13 +251,9 @@ orange .
apple
-
-

red fruit

-
+
red fruit
orange
-
-

orange fruit

-
+
orange fruit
```````````````````````````````` @@ -315,12 +291,8 @@ bim

Foo

bar
-
-

baz

-
+
baz
bim
-
-

bor

-
+
bor
````````````````````````````````