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

More consistent newline behavior #509

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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: 0 additions & 1 deletion crates/weaver_forge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,6 @@ The resulting comment in JavaDoc format would be:
* It can contain multiple lines.
* Lorem ipsum dolor sit amet, consectetur adipiscing
* elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</blockquote>
*
* <p>
* <blockquote>
* [!NOTE] Something very important here</blockquote>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@
* It can contain multiple lines.
* Lorem ipsum dolor sit amet, consectetur adipiscing
* elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</blockquote>
*
* <p>
* <blockquote>
* [!NOTE] Something very important here</blockquote>
Expand Down
18 changes: 14 additions & 4 deletions crates/weaver_forge/src/formats/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ struct RenderContext {
// The rendered HTML.
html: String,

// Add a newline before rendering the next node.
add_newline: bool,

// The rendering process traverses the AST tree in a depth-first manner.
// In certain circumstances, a tag should only be rendered if there is a
// node following the current one in the AST traversal. This field contains
Expand All @@ -80,6 +83,7 @@ impl RenderContext {
fn new(cfg: &WordWrapConfig) -> Self {
Self {
html: Default::default(),
add_newline: Default::default(),
add_old_style_paragraph: Default::default(),
word_wrap: WordWrapContext::new(cfg),
}
Expand Down Expand Up @@ -243,8 +247,11 @@ impl<'source> HtmlRenderer<'source> {
format: &str,
options: &HtmlRenderOptions,
) -> Result<(), Error> {
if ctx.add_old_style_paragraph {
if ctx.add_newline {
ctx.pushln(indent)?;
ctx.add_newline = false;
}
if ctx.add_old_style_paragraph {
if !matches!(md_node, Node::List(_)) {
ctx.push_unbroken_ln("<p>", indent)?;
}
Expand All @@ -270,8 +277,9 @@ impl<'source> HtmlRenderer<'source> {
if options.old_style_paragraph {
ctx.add_old_style_paragraph = true;
} else {
ctx.push_unbroken_ln("</p>", indent)?;
ctx.push_unbroken("</p>", indent)?;
}
ctx.add_newline = true;
}
Node::List(list) => {
let tag = if list.ordered { "ol" } else { "ul" };
Expand All @@ -286,7 +294,8 @@ impl<'source> HtmlRenderer<'source> {
}
}
ctx.pushln(indent)?;
ctx.push_unbroken_ln(&format!("</{}>", tag), indent)?;
ctx.push_unbroken(&format!("</{}>", tag), indent)?;
ctx.add_newline = true;
}
Node::ListItem(item) => {
for child in &item.children {
Expand Down Expand Up @@ -325,7 +334,8 @@ impl<'source> HtmlRenderer<'source> {
for child in &block_quote.children {
self.write_html_to(ctx, indent, child, format, options)?;
}
ctx.push_unbroken_ln("</blockquote>", indent)?;
ctx.push_unbroken("</blockquote>", indent)?;
ctx.add_newline = true;
}
Node::Link(link) => {
ctx.push_unbroken(&format!("<a href=\"{}\">", link.url), indent)?;
Expand Down
Loading