Skip to content

Commit

Permalink
More consistent newline behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
trask committed Dec 6, 2024
1 parent fa30323 commit 7ff4ba4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
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 @@ -109,7 +109,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
24 changes: 16 additions & 8 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 tag.
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 Expand Up @@ -551,8 +561,7 @@ it's RECOMMENDED to:
<ul>
<li>Use a domain-specific attribute
<li>Set {@code error.type} to capture all errors, regardless of whether they are defined within the domain-specific set or not.
</ul>
"##
</ul>"##
);
Ok(())
}
Expand Down Expand Up @@ -777,8 +786,7 @@ RECOMMENDED to:
regardless of whether they
are defined within the
domain-specific set or not.
</ul>
"##
</ul>"##
);
Ok(())
}
Expand Down

0 comments on commit 7ff4ba4

Please sign in to comment.