Rename Book.sections to Book.items

This renames the "sections" list to "items". In practice, this list has
contained more than just "sections" since parts were added. Also, the
rest of the code consistently uses the term "items", since the values it
contains are called `BookItem`s. Finally, the naming has always been a
little confusing to me.

This is a very disruptive change, and I'm not doing it lightly. However,
since there are a number of other API changes going into 0.5, I think
now is an ok time to change this.
This commit is contained in:
Eric Huss 2025-08-22 17:13:27 -07:00
parent 45e700db00
commit 800fb54aeb
11 changed files with 22 additions and 22 deletions

View file

@ -11,9 +11,9 @@ mod tests;
/// A tree structure representing a book. /// A tree structure representing a book.
/// ///
/// For the moment a book is just a collection of [`BookItems`] which are /// A book is just a collection of [`BookItems`] which are accessible by
/// accessible by either iterating (immutably) over the book with [`iter()`], or /// either iterating (immutably) over the book with [`iter()`], or recursively
/// recursively applying a closure to each section to mutate the chapters, using /// applying a closure to each item to mutate the chapters, using
/// [`for_each_mut()`]. /// [`for_each_mut()`].
/// ///
/// [`iter()`]: #method.iter /// [`iter()`]: #method.iter
@ -21,8 +21,8 @@ mod tests;
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive] #[non_exhaustive]
pub struct Book { pub struct Book {
/// The sections in this book. /// The items in this book.
pub sections: Vec<BookItem>, pub items: Vec<BookItem>,
} }
impl Book { impl Book {
@ -33,13 +33,13 @@ impl Book {
/// Creates a new book with the given items. /// Creates a new book with the given items.
pub fn new_with_items(items: Vec<BookItem>) -> Book { 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. /// Get a depth-first iterator over the items in the book.
pub fn iter(&self) -> BookItems<'_> { pub fn iter(&self) -> BookItems<'_> {
BookItems { BookItems {
items: self.sections.iter().collect(), items: self.items.iter().collect(),
} }
} }
@ -55,12 +55,12 @@ impl Book {
where where
F: FnMut(&mut BookItem), 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`. /// Append a `BookItem` to the `Book`.
pub fn push_item<I: Into<BookItem>>(&mut self, item: I) -> &mut Self { pub fn push_item<I: Into<BookItem>>(&mut self, item: I) -> &mut Self {
self.sections.push(item.into()); self.items.push(item.into());
self self
} }
} }

View file

@ -26,7 +26,7 @@ fn book_iter_iterates_over_sequential_items() {
]; ];
let book = Book::new_with_items(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(); let got: Vec<_> = book.iter().collect();

View file

@ -284,8 +284,8 @@ And here is some \
PathBuf::from("chapter_1.md"), PathBuf::from("chapter_1.md"),
vec![], vec![],
); );
let sections = vec![BookItem::Chapter(chapter)]; let items = vec![BookItem::Chapter(chapter)];
let should_be = Book::new_with_items(sections); let should_be = Book::new_with_items(items);
let got = load_book_from_disk(&summary, temp.path()).unwrap(); let got = load_book_from_disk(&summary, temp.path()).unwrap();

View file

@ -38,7 +38,7 @@ pub(super) fn create_files(
.add_field_with_tokenizer("breadcrumbs", Box::new(&tokenize)) .add_field_with_tokenizer("breadcrumbs", Box::new(&tokenize))
.build(); .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); let chapter_configs = sort_search_config(&search_config.chapter);
validate_chapter_config(&chapter_configs, book)?; validate_chapter_config(&chapter_configs, book)?;

View file

@ -601,9 +601,9 @@ impl<'a> SummaryParser<'a> {
} }
} }
fn update_section_numbers(sections: &mut [SummaryItem], level: usize, by: u32) { fn update_section_numbers(items: &mut [SummaryItem], level: usize, by: u32) {
for section in sections { for item in items {
if let SummaryItem::Link(ref mut link) = *section { if let SummaryItem::Link(ref mut link) = *item {
if let Some(ref mut number) = link.number { if let Some(ref mut number) = link.number {
number[level] += by; number[level] += by;
} }

View file

@ -134,7 +134,7 @@ mod nop_lib {
"mdbook_version": "0.4.21" "mdbook_version": "0.4.21"
}, },
{ {
"sections": [ "items": [
{ {
"Chapter": { "Chapter": {
"name": "Chapter 1", "name": "Chapter 1",

View file

@ -91,7 +91,7 @@ if __name__ == '__main__':
# load both the context and the book representations from stdin # load both the context and the book representations from stdin
context, book = json.load(sys.stdin) context, book = json.load(sys.stdin)
# and now, we can just modify the content of the first chapter # 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, # we are done with the book's modification, we can just print it to stdout,
print(json.dumps(book)) print(json.dumps(book))
``` ```

View file

@ -54,7 +54,7 @@ fn preprocessor_cfg_from_env() {
let mut s = String::new(); let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap(); std::io::stdin().read_to_string(&mut s).unwrap();
std::fs::write("out.txt", s).unwrap(); std::fs::write("out.txt", s).unwrap();
println!("{{\"sections\": []}}"); println!("{{\"items\": []}}");
} }
"#, "#,
) )

View file

@ -115,7 +115,7 @@ fn relative_command_path() {
let mut s = String::new(); let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap(); std::io::stdin().read_to_string(&mut s).unwrap();
std::fs::write("preprocessor-ran", "test").unwrap(); std::fs::write("preprocessor-ran", "test").unwrap();
println!("{{\"sections\": []}}"); println!("{{\"items\": []}}");
} }
"#, "#,
) )

View file

@ -167,7 +167,7 @@ fn backends_receive_render_context_via_stdin() {
str![[r##" str![[r##"
{ {
"book": { "book": {
"sections": [ "items": [
{ {
"Chapter": { "Chapter": {
"content": "# Chapter 1\n", "content": "# Chapter 1\n",

View file

@ -117,7 +117,7 @@ fn with_no_source_path() {
let test = BookTest::from_dir("search/reasonable_search_index"); let test = BookTest::from_dir("search/reasonable_search_index");
let mut book = test.load_book(); let mut book = test.load_book();
let chapter = Chapter::new("Sample chapter", String::new(), "sample.html", vec![]); 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(); book.build().unwrap();
} }