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 25, 2024
1 parent 2c35b13 commit f4b0b9e
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 208 deletions.
98 changes: 54 additions & 44 deletions fuzz/Cargo.lock

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

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
34 changes: 25 additions & 9 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,15 +1884,26 @@ 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.
*container = container.parent().unwrap();
if !node_matches!(container, NodeValue::Paragraph) {
// If the container is not a paragraph, then this can't
// be a description list item.
return false;
}

let parent = container.parent();
if parent.is_none() {
return false;
}

tight = true;
*container = parent.unwrap();
container.last_child().unwrap()
}
};
Expand All @@ -1918,7 +1929,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 +1952,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 +1973,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
3 changes: 1 addition & 2 deletions src/scanners.re
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,9 @@ 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); }
[:~] ([ \t]+) { return Some(cursor); }
* { return None; }
*/
}
Expand Down
Loading

0 comments on commit f4b0b9e

Please sign in to comment.