Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance description lists #462

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 7 additions & 6 deletions 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 Expand Up @@ -696,14 +696,15 @@ impl<'o, 'c: 'o> HtmlFormatter<'o, 'c> {
.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 {
Expand Down
4 changes: 4 additions & 0 deletions src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,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
67 changes: 62 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,19 @@ 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 mut tight = false;

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.
tight = true;
*container = container.parent().unwrap();
container.last_child().unwrap()
}
};

if node_matches!(last_child, NodeValue::Paragraph) {
Expand Down Expand Up @@ -1799,6 +1811,7 @@ impl<'a, 'o, 'c: 'o> Parser<'a, 'o, 'c> {
let metadata = NodeDescriptionItem {
marker_offset: self.indent,
padding: 2,
tight,
};

let item = self.add_child(
Expand All @@ -1816,6 +1829,50 @@ impl<'a, 'o, 'c: 'o> Parser<'a, 'o, 'c> {
*container = details;

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(
parent,
NodeValue::DescriptionItem(metadata),
self.first_nonspace + 1,
);

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

*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
Comment on lines +1856 to +1875
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kivikakk I was wondering if you could take a look at this.

I realized that when supporting multiple definitions, the second DescriptionDetails gets added as an entirely new DescriptionItem, rather than being added as a child to the existing DescriptionItem.

But both my attempts to do this resulted in the assert!(ast.open); in finalize_borrowed tripping.

Sample input

foo
: bar
: one

I thought maybe it's the shenanigans I'm doing at line 1760, the None case, but using the following input (with blanks lines) bypasses that and it still asserts.

foo

: bar

: one

Even with re-opening the ast node, it's not working. I'm missing some magic incantation. 🤔

} else {
false
}
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.

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
Loading
Loading