Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Arguments cells that start with a code block or display equation now render validly in the `list-table` and `list` formats. Previously the `list-table` form failed to parse in Quarto 2.
- Remove a spurious blank line before the closing fence of code blocks in `grid-table` Arguments cells.
- Indent lines after a hard line break (`\cr`) inside a list item under the item, so they stay inside it instead of relying on lazy continuation.
- A `\describe{}` item whose description is only a list (for example `\item{a}{\itemize{...}}`) no longer turns the first list item into an indented code block in the definition-list format.
- Remove extra blank lines after lists, definition lists, and code blocks inside list items and definition descriptions. Every block now ends with a single newline, and sibling blocks are separated by exactly one blank line.
- A tight list whose item holds a table or a definition list is no longer read as loose by CommonMark parsers.
- Emphasis directly inside a word (for example `before\emph{em}after`) is no longer lost in common cases. It is now written with `*` where `_` would not be recognized as an emphasis delimiter. Some nested combinations (such as emphasis inside a word at the edge of strong text) are still not recognized.
Expand Down
48 changes: 48 additions & 0 deletions crates/rd2qmd-cli/tests/describe_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,51 @@ Second paragraph.
}
}
}

/// A description holding only a list keeps every item in that list. The
/// first item used to be indented past the `: ` marker and read as an
/// indented code block.
#[test]
fn describe_definition_list_with_only_a_list() {
let rd = r"\name{x}\title{X}
\description{
\describe{
\item{a}{\itemize{\item one \item two}}
\item{b}{\enumerate{\item three \item four}}
}
}";
let output = convert_core_as(rd, DescribeFormat::DefinitionList);
// Text of each term, and of the list items in its definition.
let mut groups: Vec<(String, Vec<String>)> = Vec::new();
let mut in_term = false;
let mut in_item = false;
for event in Parser::new_ext(&output, Options::ENABLE_DEFINITION_LIST) {
match event {
Event::Start(Tag::CodeBlock(_)) => panic!("unexpected code block in {output:?}"),
Event::Start(Tag::DefinitionListTitle) => {
groups.push((String::new(), Vec::new()));
in_term = true;
}
Event::End(TagEnd::DefinitionListTitle) => in_term = false,
Event::Start(Tag::Item) => {
let (_, items) = groups.last_mut().expect("item inside a definition");
items.push(String::new());
in_item = true;
}
Event::End(TagEnd::Item) => in_item = false,
// Text before the first term is the section heading.
Event::Text(text) if !groups.is_empty() => {
let (term, items) = groups.last_mut().unwrap();
if in_term {
term.push_str(&text);
} else if in_item {
items.last_mut().unwrap().push_str(text.trim());
}
}
_ => {}
}
}
let expected = [("a", ["one", "two"]), ("b", ["three", "four"])]
.map(|(term, items)| (term.to_owned(), items.map(str::to_owned).to_vec()));
assert_eq!(groups, expected, "{output:?}");
}
94 changes: 49 additions & 45 deletions crates/rd2qmd-mdast/src/writer/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ impl<'a> Writer<'a> {
}

pub(super) fn write_list(&mut self, l: &crate::mdast::List) {
self.write_list_impl(l, 0, ListContext::Flow);
self.write_list_impl(l, ListContext::Flow);
}

/// Single list renderer shared by document-flow lists and lists inside
/// definition descriptions. The two contexts intentionally differ in a few
/// places; see [`ListContext`].
fn write_list_impl(&mut self, l: &crate::mdast::List, base_indent: usize, ctx: ListContext) {
///
/// Like every other block, the list is written from the current column
/// with no indent of its own. A container that nests it writes the prefix
/// of the first line and re-indents the rest.
fn write_list_impl(&mut self, l: &crate::mdast::List, ctx: ListContext) {
let flow = ctx == ListContext::Flow;
if flow {
self.ensure_newline();
Expand All @@ -65,7 +69,6 @@ impl<'a> Writer<'a> {
let mut num = l.start.unwrap_or(1);
for (item_index, child) in l.children.iter().enumerate() {
if let Node::ListItem(li) = child {
self.output.push_str(&" ".repeat(base_indent));
// Build the marker first so item_indent matches the actual marker width.
// "- " is always 2 chars, but "10. " is 4 — a fixed +2 would mis-indent
// continuation lines for ordered lists with wide numbers.
Expand All @@ -77,7 +80,7 @@ impl<'a> Writer<'a> {
"- ".to_string()
};
self.output.push_str(&marker);
let item_indent = base_indent + marker.len();
let item_indent = marker.len();
let item_indent_str = " ".repeat(item_indent);

for (i, item_child) in li.children.iter().enumerate() {
Expand All @@ -100,27 +103,24 @@ impl<'a> Writer<'a> {
self.reindent_since(start, item_indent);
self.write_terminator();
}
Node::List(nested) if flow => {
if i == 0 {
// End the marker line; the nested list starts
// on the next one.
self.write_terminator();
}
// The recursive call emits the indent per item.
self.write_list_impl(nested, item_indent, ctx);
}
_ => {
// Block children (code fences, tables, and in descriptions also
// nested lists) have every continuation line re-indented to
// `item_indent` so they stay inside the list item.
if i > 0 {
self.output.push_str(&item_indent_str);
} else if !flow {
// In descriptions the block always starts on its own line.
// Block children (code fences, tables, nested lists) have
// every continuation line re-indented to `item_indent` so
// they stay inside the list item.
let prefix = if i > 0 {
item_indent_str.as_str()
} else if !flow || matches!(item_child, Node::List(_)) {
// A nested list, and in descriptions any block, starts
// on the line after the marker.
self.write_terminator();
self.output.push_str(&item_indent_str);
}
self.write_reindented_block(item_child, item_indent);
item_indent_str.as_str()
} else {
// The marker is the first line's prefix.
""
};
self.write_nested_block(prefix, item_indent, |w| {
w.write_node(item_child)
});
self.close_opaque_block();
}
}
Expand Down Expand Up @@ -324,16 +324,13 @@ impl<'a> Writer<'a> {
self.write_inlines(&p.children, None);
self.write_terminator();
}
Node::List(l) => {
// The list indents each of its items by 4 spaces itself.
self.write_list_impl(l, 4, ListContext::Description);
self.close_opaque_block();
}
_ => {
if j > 0 {
self.output.push_str(" ");
}
self.write_reindented_block(child, 4);
// The `: ` marker is the first block's prefix.
let prefix = if j > 0 { " " } else { "" };
self.write_nested_block(prefix, 4, |w| match child {
Node::List(l) => w.write_list_impl(l, ListContext::Description),
_ => w.write_node(child),
});
self.close_opaque_block();
}
}
Expand Down Expand Up @@ -361,28 +358,35 @@ impl<'a> Writer<'a> {
}
}

/// Render `node`, then re-indent every line after the first by `indent`
/// spaces. Used to keep multi-line block children (code fences, nested
/// lists, tables, ...) properly indented when nested inside a definition
/// description or a list item. The caller is responsible for indenting
/// the first line before calling this.
fn write_reindented_block(&mut self, node: &Node, indent: usize) {
/// Write a block nested inside a list item or definition description:
/// `prefix` starts its first line, and every later line is re-indented by
/// `indent` spaces so the block stays inside its container.
///
/// When the block writes nothing (an empty table or list, empty raw
/// HTML), the prefix is dropped as well, so no whitespace-only line is
/// left behind.
fn write_nested_block(&mut self, prefix: &str, indent: usize, write: impl FnOnce(&mut Self)) {
let start = self.output.len();
self.write_node(node);
self.reindent_since(start, indent);
self.output.push_str(prefix);
let body = self.output.len();
write(self);
if self.output.len() == body {
self.output.truncate(start);
} else {
self.reindent_since(body, indent);
}
}
}

/// Where a list is being rendered. The contexts have diverged historically and
/// their differing output is preserved on purpose for now.
#[derive(Clone, Copy, PartialEq, Eq)]
enum ListContext {
/// Top level or inside another list item: supports loose lists, recurses
/// into nested lists directly, and always terminates items with a newline.
/// Top level or inside another list item: supports loose lists, and a
/// block child other than a nested list starts on the marker line.
Flow,
/// Inside a definition description: never loose, nested lists are rendered
/// as ordinary re-indented blocks, and no extra newline follows an item
/// that already ends with one.
/// Inside a definition description: never loose, and every block child
/// starts on the line after the marker.
Description,
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rd2qmd-mdast/src/writer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,8 @@ fn test_ordered_list_loose_continuation_indent() {

#[test]
fn test_nested_list_not_double_indented() {
// Nested item indent must be exactly item_indent (2 for "- "),
// not 2 * item_indent (from pre-indent + recursive base_indent).
// Nested item indent must be exactly item_indent (2 for "- "), not
// applied twice by both the container and the nested list.
let root = Root::new(vec![Node::list(
false,
vec![Node::list_item(vec![
Expand Down
29 changes: 29 additions & 0 deletions crates/rd2qmd-mdast/src/writer/tests_boundary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,35 @@ fn opaque_children_inside_containers() {
)],
"- \n- b\n",
),
(
"empty nested list item, then item",
vec![Node::list(
false,
vec![item(vec![Node::list(false, vec![])]), item(vec![para("b")])],
)],
"- \n- b\n",
),
(
"empty table after paragraph in description",
vec![Node::definition_list(vec![
term("T"),
def(vec![para("D"), Node::table(vec![], vec![])]),
])],
"T\n: D\n\n",
),
// The separator is written before the empty list is known to be
// empty, which leaves two blank lines. Harmless to parsers, since
// the definition is already loose.
(
"paragraph and empty list description, then term",
vec![Node::definition_list(vec![
term("T1"),
def(vec![para("D1"), Node::list(false, vec![])]),
term("T2"),
def(vec![para("D2")]),
])],
"T1\n: D1\n\n\nT2\n: D2\n",
),
(
"html description without newline, then term",
vec![Node::definition_list(vec![
Expand Down
22 changes: 11 additions & 11 deletions crates/rd2qmd-mdast/src/writer/tests_dl_list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Pins how lists nested inside definition descriptions are rendered. These
//! differ from top-level lists: no list inside a description is ever loose.
//! Some outputs (e.g. the 8-space first item) are quirks pinned as-is.
//! Some outputs are quirks pinned as-is.
use super::*;
use crate::mdast::*;

Expand Down Expand Up @@ -33,15 +33,15 @@ fn lists_inside_definition_descriptions() {
(
"tight list",
vec![bullets(vec![item(vec![para("a")]), item(vec![para("b")])])],
"T\n: - a\n - b\n",
"T\n: - a\n - b\n",
),
(
"multi-block item stays tight",
vec![bullets(vec![
item(vec![para("a"), para("b")]),
item(vec![para("c")]),
])],
"T\n: - a\n\n b\n - c\n",
"T\n: - a\n\n b\n - c\n",
),
(
"ordered list with wide numbers",
Expand All @@ -53,15 +53,15 @@ fn lists_inside_definition_descriptions() {
item(vec![para("d")]),
],
)],
"T\n: 9. a\n 10. b\n\n c\n 11. d\n",
"T\n: 9. a\n 10. b\n\n c\n 11. d\n",
),
(
"nested list",
vec![bullets(vec![
item(vec![para("a"), bullets(vec![item(vec![para("b")])])]),
item(vec![para("c")]),
])],
"T\n: - a\n\n - b\n - c\n",
"T\n: - a\n\n - b\n - c\n",
),
(
"nested list in wide ordered item",
Expand All @@ -72,23 +72,23 @@ fn lists_inside_definition_descriptions() {
bullets(vec![item(vec![para("b")]), item(vec![para("c")])]),
])],
)],
"T\n: 10. a\n\n - b\n - c\n",
"T\n: 10. a\n\n - b\n - c\n",
),
(
"item with code",
vec![bullets(vec![
item(vec![para("a"), Node::code(None, "x\ny")]),
item(vec![para("b")]),
])],
"T\n: - a\n\n ```\n x\n y\n ```\n - b\n",
"T\n: - a\n\n ```\n x\n y\n ```\n - b\n",
),
(
"item starting with code",
vec![bullets(vec![
item(vec![Node::code(None, "x")]),
item(vec![para("b")]),
])],
"T\n: - \n ```\n x\n ```\n - b\n",
"T\n: - \n ```\n x\n ```\n - b\n",
),
(
"paragraph then list",
Expand All @@ -98,20 +98,20 @@ fn lists_inside_definition_descriptions() {
(
"list then paragraph",
vec![bullets(vec![item(vec![para("a")])]), para("P")],
"T\n: - a\n\n P\n",
"T\n: - a\n\n P\n",
),
(
"list then code",
vec![bullets(vec![item(vec![para("a")])]), Node::code(None, "x")],
"T\n: - a\n\n ```\n x\n ```\n",
"T\n: - a\n\n ```\n x\n ```\n",
),
(
"two lists",
vec![
bullets(vec![item(vec![para("a")])]),
Node::list(true, vec![item(vec![para("b")])]),
],
"T\n: - a\n\n 1. b\n",
"T\n: - a\n\n 1. b\n",
),
];
for (label, desc, expected) in cases {
Expand Down
Loading
Loading