Improve line breaks for code
kuchiki’s NodeRef::text_contents() implementation does not take into account the value of the display attribute or line breaks caused by br elements. Therefore we have to implement our own converter that produces readable output for the HTML code generated by rustdoc.
This commit is contained in:
parent
7d1bf00d31
commit
17e407d6b1
@ -19,6 +19,7 @@ SPDX-License-Identifier: MIT
|
||||
configuration file can be used to set defaults for the command-line
|
||||
options.
|
||||
- Add the `--config-file [file]` option to set a custom configuration file.
|
||||
- Improve line break rendering when displaying code.
|
||||
|
||||
## v0.1.3 (2020-07-28)
|
||||
|
||||
|
@ -27,7 +27,7 @@ impl From<kuchiki::NodeRef> for doc::Text {
|
||||
impl From<&kuchiki::NodeRef> for doc::Text {
|
||||
fn from(node: &kuchiki::NodeRef) -> doc::Text {
|
||||
doc::Text {
|
||||
plain: node.text_contents(),
|
||||
plain: node_to_text(node),
|
||||
html: node.to_string(),
|
||||
}
|
||||
}
|
||||
@ -39,6 +39,39 @@ impl<T> From<kuchiki::NodeDataRef<T>> for doc::Text {
|
||||
}
|
||||
}
|
||||
|
||||
fn node_to_text(node: &kuchiki::NodeRef) -> String {
|
||||
let mut s = String::new();
|
||||
push_node_to_text(&mut s, node);
|
||||
s.trim().to_string()
|
||||
}
|
||||
|
||||
fn push_node_to_text(s: &mut String, node: &kuchiki::NodeRef) {
|
||||
let is_docblock = has_class(node, "docblock");
|
||||
|
||||
let add_newline = if is_element(node, &local_name!("br")) {
|
||||
true
|
||||
} else if has_class(node, "fmt-newline") || is_docblock {
|
||||
!s.is_empty() && !s.ends_with('\n')
|
||||
} else {
|
||||
false
|
||||
};
|
||||
if add_newline {
|
||||
s.push_str("\n");
|
||||
}
|
||||
|
||||
if let Some(text) = node.as_text() {
|
||||
s.push_str(&text.borrow())
|
||||
}
|
||||
|
||||
for child in node.children() {
|
||||
push_node_to_text(s, &child);
|
||||
}
|
||||
|
||||
if is_docblock && !s.is_empty() && !s.ends_with('\n') {
|
||||
s.push_str("\n");
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses the HTML document at the given path and returns the DOM.
|
||||
fn parse_file<P: AsRef<path::Path>>(path: P) -> anyhow::Result<kuchiki::NodeRef> {
|
||||
use kuchiki::traits::TendrilSink;
|
||||
|
Loading…
Reference in New Issue
Block a user