mirror of
https://github.com/typst/typst
synced 2025-06-25 14:52:52 +08:00
Extract write_children
function
This commit is contained in:
parent
f8dc1ad3bd
commit
c2e2fd99f6
@ -28,7 +28,7 @@ struct Writer {
|
|||||||
pretty: bool,
|
pretty: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write a newline and indent, if pretty printing is enabled.
|
/// Writes a newline and indent, if pretty printing is enabled.
|
||||||
fn write_indent(w: &mut Writer) {
|
fn write_indent(w: &mut Writer) {
|
||||||
if w.pretty {
|
if w.pretty {
|
||||||
w.buf.push('\n');
|
w.buf.push('\n');
|
||||||
@ -38,7 +38,7 @@ fn write_indent(w: &mut Writer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encode an HTML node into the writer.
|
/// Encodes an HTML node into the writer.
|
||||||
fn write_node(w: &mut Writer, node: &HtmlNode) -> SourceResult<()> {
|
fn write_node(w: &mut Writer, node: &HtmlNode) -> SourceResult<()> {
|
||||||
match node {
|
match node {
|
||||||
HtmlNode::Tag(_) => {}
|
HtmlNode::Tag(_) => {}
|
||||||
@ -49,7 +49,7 @@ fn write_node(w: &mut Writer, node: &HtmlNode) -> SourceResult<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encode plain text into the writer.
|
/// Encodes plain text into the writer.
|
||||||
fn write_text(w: &mut Writer, text: &str, span: Span) -> SourceResult<()> {
|
fn write_text(w: &mut Writer, text: &str, span: Span) -> SourceResult<()> {
|
||||||
for c in text.chars() {
|
for c in text.chars() {
|
||||||
if charsets::is_valid_in_normal_element_text(c) {
|
if charsets::is_valid_in_normal_element_text(c) {
|
||||||
@ -61,7 +61,7 @@ fn write_text(w: &mut Writer, text: &str, span: Span) -> SourceResult<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encode one element into the write.
|
/// Encodes one element into the writer.
|
||||||
fn write_element(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
|
fn write_element(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
|
||||||
w.buf.push('<');
|
w.buf.push('<');
|
||||||
w.buf.push_str(&element.tag.resolve());
|
w.buf.push_str(&element.tag.resolve());
|
||||||
@ -95,13 +95,25 @@ fn write_element(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let pretty = w.pretty;
|
|
||||||
if !element.children.is_empty() {
|
if !element.children.is_empty() {
|
||||||
|
write_children(w, element)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
w.buf.push_str("</");
|
||||||
|
w.buf.push_str(&element.tag.resolve());
|
||||||
|
w.buf.push('>');
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Encodes the children of an element.
|
||||||
|
fn write_children(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
|
||||||
// See HTML spec § 13.1.2.5.
|
// See HTML spec § 13.1.2.5.
|
||||||
if element.tag == tag::pre && starts_with_newline(element) {
|
if element.tag == tag::pre && starts_with_newline(element) {
|
||||||
w.buf.push('\n');
|
w.buf.push('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let pretty = w.pretty;
|
||||||
let pretty_inside = allows_pretty_inside(element.tag)
|
let pretty_inside = allows_pretty_inside(element.tag)
|
||||||
&& element.children.iter().any(|node| match node {
|
&& element.children.iter().any(|node| match node {
|
||||||
HtmlNode::Element(child) => wants_pretty_around(child.tag),
|
HtmlNode::Element(child) => wants_pretty_around(child.tag),
|
||||||
@ -128,13 +140,8 @@ fn write_element(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
|
|||||||
w.level -= 1;
|
w.level -= 1;
|
||||||
|
|
||||||
write_indent(w);
|
write_indent(w);
|
||||||
}
|
|
||||||
w.pretty = pretty;
|
w.pretty = pretty;
|
||||||
|
|
||||||
w.buf.push_str("</");
|
|
||||||
w.buf.push_str(&element.tag.resolve());
|
|
||||||
w.buf.push('>');
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user