replace tabs in code blocks with 4 spaces

This commit is contained in:
Jesse de Wit
2023-11-10 12:17:20 +01:00
parent ad9c48d66a
commit 74f018a647

View File

@@ -78,18 +78,18 @@ impl Preprocessor for SnippetsProcessor {
fn run(&self, _ctx: &PreprocessorContext, mut book: Book) -> Result<Book> { fn run(&self, _ctx: &PreprocessorContext, mut book: Book) -> Result<Book> {
book.for_each_mut(|item| { book.for_each_mut(|item| {
if let BookItem::Chapter(chapter) = item { if let BookItem::Chapter(chapter) = item {
let mut resulting_lines: Vec<&str> = vec![]; let mut resulting_lines: Vec<String> = vec![];
let mut in_block = false; let mut in_block = false;
let mut block_lines: Vec<&str> = vec![]; let mut block_lines: Vec<String> = vec![];
let mut min_indentation: usize = 0; let mut min_indentation: usize = 0;
for line in chapter.content.lines() { for line in chapter.content.lines() {
if line.starts_with("```") { if line.starts_with("```") {
if in_block { if in_block {
// This is end of block // This is end of block
// Replace previous lines // Replace previous lines
for block_line in block_lines.iter() { for block_line in block_lines.iter().cloned() {
let indent = std::cmp::min(min_indentation, block_line.len()); let indent = std::cmp::min(min_indentation, block_line.len());
resulting_lines.push(&block_line[indent..]) resulting_lines.push(block_line[indent..].to_string())
} }
in_block = false; in_block = false;
} else { } else {
@@ -99,19 +99,20 @@ impl Preprocessor for SnippetsProcessor {
min_indentation = usize::MAX; min_indentation = usize::MAX;
} }
resulting_lines.push(line); resulting_lines.push(line.to_string());
continue; continue;
} }
if in_block { if in_block {
block_lines.push(line); let line = line.replace('\t', " ");
block_lines.push(line.clone());
let trimmed = line.trim_start_matches(' '); let trimmed = line.trim_start_matches(' ');
if !trimmed.is_empty() { if !trimmed.is_empty() {
min_indentation = min_indentation =
std::cmp::min(min_indentation, line.len() - trimmed.len()) std::cmp::min(min_indentation, line.len() - trimmed.len())
} }
} else { } else {
resulting_lines.push(line); resulting_lines.push(line.to_string());
} }
} }