Remove common whitespace prefix from src block lines.
This commit is contained in:
parent
5654c40d03
commit
2ae4839ce0
@ -14,12 +14,60 @@ intermediate!(
|
|||||||
original,
|
original,
|
||||||
_registry,
|
_registry,
|
||||||
{
|
{
|
||||||
let lines = original
|
let source_code = original.get_value();
|
||||||
.get_value()
|
let prefix_content_pairs: Vec<_> = source_code
|
||||||
.split_inclusive('\n')
|
.split_inclusive('\n')
|
||||||
.map(|s| s.to_owned())
|
.map(|line| {
|
||||||
|
line.find(|c: char| !c.is_ascii_whitespace() && c != '\n')
|
||||||
|
.map(|content_start| line.split_at(content_start))
|
||||||
|
.map(|(leading_whitespace, content)| {
|
||||||
|
(
|
||||||
|
leading_whitespace
|
||||||
|
.chars()
|
||||||
|
.map(ascii_whitespace_value)
|
||||||
|
.sum::<usize>(),
|
||||||
|
content,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.unwrap_or((0, line))
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let common_whitespace_prefix = prefix_content_pairs
|
||||||
|
.iter()
|
||||||
|
.filter_map(|(leading_whitespace, _content)| {
|
||||||
|
if *leading_whitespace > 0 {
|
||||||
|
Some(*leading_whitespace)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.min()
|
||||||
|
.unwrap_or(0);
|
||||||
|
let lines: Vec<_> = prefix_content_pairs
|
||||||
|
.into_iter()
|
||||||
|
.map(|(leading_whitespace, content)| {
|
||||||
|
let mut line = String::with_capacity(common_whitespace_prefix + content.len());
|
||||||
|
if leading_whitespace > common_whitespace_prefix {
|
||||||
|
// Empty lines can have a leading_whitespace less than common_whitespace_prefix.
|
||||||
|
for _ in 0..(leading_whitespace - common_whitespace_prefix) {
|
||||||
|
line.push(' ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
line.push_str(content);
|
||||||
|
line
|
||||||
|
})
|
||||||
.collect();
|
.collect();
|
||||||
let language = original.language.map(str::to_owned);
|
let language = original.language.map(str::to_owned);
|
||||||
Ok(ISrcBlock { lines, language })
|
Ok(ISrcBlock { lines, language })
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
fn ascii_whitespace_value(c: char) -> usize {
|
||||||
|
match c {
|
||||||
|
' ' => 1,
|
||||||
|
'\t' => 8,
|
||||||
|
'\r' | '\n' => 0,
|
||||||
|
c if c as u32 == 12 => 0, // form feed
|
||||||
|
_ => unreachable!("Only ascii whitespace can reach this code."),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user