// mdbook-wordcount, rebuilt against mdbook 0.5. // // Iterates every chapter, counts whitespace-separated words, and prints // per-chapter counts plus a grand total to stdout. // // The only reason this exists separately is that the crates.io release is // compiled against mdbook 0.4, whose RenderContext JSON schema differs from // 0.5 ("missing field `sections`"). See rust-lang.github.io/mdBook backends. use mdbook_renderer::book::{BookItem, Chapter}; use mdbook_renderer::RenderContext; use std::io; fn main() { let mut stdin = io::stdin(); let ctx = RenderContext::from_json(&mut stdin).expect("couldn't parse RenderContext"); println!("\n----------"); println!("Word Count"); let mut total = 0; for item in ctx.book.iter() { if let BookItem::Chapter(ref ch) = *item { let words = count_words(ch); total += words; println!("{}: {}", ch.name, words); } } println!("Total: {}", total); println!("----------"); } fn count_words(ch: &Chapter) -> usize { ch.content.split_whitespace().count() }