mirror of
https://github.com/rust-lang/mdBook.git
synced 2025-12-27 10:16:09 -05:00
Add an iterator over chapters
This adds the `Book::chapters` iterator (and `for_each_chapter_mut`) to iterate over non-draft chapters. This is a common pattern I keep encountering, and I figure it might simplify things. It runs a little risk that callers may not be properly handling every item type, but I think it should be ok.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//! This is a demonstration of an mdBook preprocessor which parses markdown
|
||||
//! and removes any instances of emphasis.
|
||||
|
||||
use mdbook_preprocessor::book::{Book, BookItem, Chapter};
|
||||
use mdbook_preprocessor::book::{Book, Chapter};
|
||||
use mdbook_preprocessor::errors::Result;
|
||||
use mdbook_preprocessor::{Preprocessor, PreprocessorContext};
|
||||
use pulldown_cmark::{Event, Parser, Tag, TagEnd};
|
||||
@@ -36,17 +36,9 @@ impl Preprocessor for RemoveEmphasis {
|
||||
|
||||
fn run(&self, _ctx: &PreprocessorContext, mut book: Book) -> Result<Book> {
|
||||
let mut total = 0;
|
||||
book.for_each_mut(|item| {
|
||||
let BookItem::Chapter(ch) = item else {
|
||||
return;
|
||||
};
|
||||
if ch.is_draft_chapter() {
|
||||
return;
|
||||
}
|
||||
match remove_emphasis(&mut total, ch) {
|
||||
Ok(s) => ch.content = s,
|
||||
Err(e) => eprintln!("failed to process chapter: {e:?}"),
|
||||
}
|
||||
book.for_each_chapter_mut(|ch| match remove_emphasis(&mut total, ch) {
|
||||
Ok(s) => ch.content = s,
|
||||
Err(e) => eprintln!("failed to process chapter: {e:?}"),
|
||||
});
|
||||
eprintln!("removed {total} emphasis");
|
||||
Ok(book)
|
||||
|
||||
Reference in New Issue
Block a user