Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions crates/mdbook-core/src/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ mod tests;

/// A tree structure representing a book.
///
/// For the moment a book is just a collection of [`BookItems`] which are
/// accessible by either iterating (immutably) over the book with [`iter()`], or
/// recursively applying a closure to each section to mutate the chapters, using
/// A book is just a collection of [`BookItems`] which are accessible by
/// either iterating (immutably) over the book with [`iter()`], or recursively
/// applying a closure to each item to mutate the chapters, using
/// [`for_each_mut()`].
///
/// [`iter()`]: #method.iter
/// [`for_each_mut()`]: #method.for_each_mut
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Book {
/// The sections in this book.
pub sections: Vec<BookItem>,
/// The items in this book.
pub items: Vec<BookItem>,
}

impl Book {
Expand All @@ -33,13 +33,13 @@ impl Book {

/// Creates a new book with the given items.
pub fn new_with_items(items: Vec<BookItem>) -> Book {
Book { sections: items }
Book { items }
}

/// Get a depth-first iterator over the items in the book.
pub fn iter(&self) -> BookItems<'_> {
BookItems {
items: self.sections.iter().collect(),
items: self.items.iter().collect(),
}
}

Expand All @@ -55,12 +55,12 @@ impl Book {
where
F: FnMut(&mut BookItem),
{
for_each_mut(&mut func, &mut self.sections);
for_each_mut(&mut func, &mut self.items);
}

/// Append a `BookItem` to the `Book`.
pub fn push_item<I: Into<BookItem>>(&mut self, item: I) -> &mut Self {
self.sections.push(item.into());
self.items.push(item.into());
self
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/mdbook-core/src/book/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn book_iter_iterates_over_sequential_items() {
];
let book = Book::new_with_items(items);

let should_be: Vec<_> = book.sections.iter().collect();
let should_be: Vec<_> = book.items.iter().collect();

let got: Vec<_> = book.iter().collect();

Expand Down
4 changes: 2 additions & 2 deletions crates/mdbook-driver/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ And here is some \
PathBuf::from("chapter_1.md"),
vec![],
);
let sections = vec![BookItem::Chapter(chapter)];
let should_be = Book::new_with_items(sections);
let items = vec![BookItem::Chapter(chapter)];
let should_be = Book::new_with_items(items);

let got = load_book_from_disk(&summary, temp.path()).unwrap();

Expand Down
2 changes: 1 addition & 1 deletion crates/mdbook-html/src/html_handlebars/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub(super) fn create_files(
.add_field_with_tokenizer("breadcrumbs", Box::new(&tokenize))
.build();

let mut doc_urls = Vec::with_capacity(book.sections.len());
let mut doc_urls = Vec::with_capacity(book.items.len());

let chapter_configs = sort_search_config(&search_config.chapter);
validate_chapter_config(&chapter_configs, book)?;
Expand Down
6 changes: 3 additions & 3 deletions crates/mdbook-summary/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,9 @@ impl<'a> SummaryParser<'a> {
}
}

fn update_section_numbers(sections: &mut [SummaryItem], level: usize, by: u32) {
for section in sections {
if let SummaryItem::Link(ref mut link) = *section {
fn update_section_numbers(items: &mut [SummaryItem], level: usize, by: u32) {
for item in items {
if let SummaryItem::Link(ref mut link) = *item {
if let Some(ref mut number) = link.number {
number[level] += by;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/nop-preprocessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ mod nop_lib {
"mdbook_version": "0.4.21"
},
{
"sections": [
"items": [
{
"Chapter": {
"name": "Chapter 1",
Expand Down
2 changes: 1 addition & 1 deletion guide/src/for_developers/preprocessors.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ if __name__ == '__main__':
# load both the context and the book representations from stdin
context, book = json.load(sys.stdin)
# and now, we can just modify the content of the first chapter
book['sections'][0]['Chapter']['content'] = '# Hello'
book['items'][0]['Chapter']['content'] = '# Hello'
# we are done with the book's modification, we can just print it to stdout,
print(json.dumps(book))
```
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn preprocessor_cfg_from_env() {
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
std::fs::write("out.txt", s).unwrap();
println!("{{\"sections\": []}}");
println!("{{\"items\": []}}");
}
"#,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/preprocessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn relative_command_path() {
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
std::fs::write("preprocessor-ran", "test").unwrap();
println!("{{\"sections\": []}}");
println!("{{\"items\": []}}");
}
"#,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ fn backends_receive_render_context_via_stdin() {
str![[r##"
{
"book": {
"sections": [
"items": [
{
"Chapter": {
"content": "# Chapter 1\n",
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn with_no_source_path() {
let test = BookTest::from_dir("search/reasonable_search_index");
let mut book = test.load_book();
let chapter = Chapter::new("Sample chapter", String::new(), "sample.html", vec![]);
book.book.sections.push(BookItem::Chapter(chapter));
book.book.items.push(BookItem::Chapter(chapter));
book.build().unwrap();
}

Expand Down
Loading