1use crate::builtin_preprocessors::{CmdPreprocessor, IndexPreprocessor, LinkPreprocessor};
4use crate::builtin_renderers::{CmdRenderer, MarkdownRenderer};
5use crate::init::BookBuilder;
6use crate::load::{load_book, load_book_from_disk};
7use anyhow::{Context, Error, Result, bail};
8use indexmap::IndexMap;
9use mdbook_core::book::{Book, BookItem, BookItems};
10use mdbook_core::config::{Config, RustEdition};
11use mdbook_core::utils::fs;
12use mdbook_html::HtmlHandlebars;
13use mdbook_preprocessor::{Preprocessor, PreprocessorContext};
14use mdbook_renderer::{RenderContext, Renderer};
15use mdbook_summary::Summary;
16use serde::Deserialize;
17use std::ffi::OsString;
18use std::io::IsTerminal;
19use std::path::{Path, PathBuf};
20use std::process::Command;
21use tempfile::Builder as TempFileBuilder;
22use topological_sort::TopologicalSort;
23use tracing::{debug, info, trace, warn};
24
25#[cfg(test)]
26mod tests;
27
28pub struct MDBook {
30 pub root: PathBuf,
32
33 pub config: Config,
35
36 pub book: Book,
38
39 renderers: IndexMap<String, Box<dyn Renderer>>,
41
42 preprocessors: IndexMap<String, Box<dyn Preprocessor>>,
44}
45
46impl MDBook {
47 pub fn load<P: Into<PathBuf>>(book_root: P) -> Result<MDBook> {
49 let book_root = book_root.into();
50 let config_location = book_root.join("book.toml");
51
52 let mut config = if config_location.exists() {
53 debug!("Loading config from {}", config_location.display());
54 Config::from_disk(&config_location)?
55 } else {
56 Config::default()
57 };
58
59 config.update_from_env()?;
60
61 if tracing::enabled!(tracing::Level::TRACE) {
62 for line in format!("Config: {config:#?}").lines() {
63 trace!("{}", line);
64 }
65 }
66
67 MDBook::load_with_config(book_root, config)
68 }
69
70 pub fn load_with_config<P: Into<PathBuf>>(book_root: P, config: Config) -> Result<MDBook> {
72 let root = book_root.into();
73
74 let src_dir = root.join(&config.book.src);
75 let book = load_book(src_dir, &config.build)?;
76
77 let renderers = determine_renderers(&config)?;
78 let preprocessors = determine_preprocessors(&config, &root)?;
79
80 Ok(MDBook {
81 root,
82 config,
83 book,
84 renderers,
85 preprocessors,
86 })
87 }
88
89 pub fn load_with_config_and_summary<P: Into<PathBuf>>(
91 book_root: P,
92 config: Config,
93 summary: Summary,
94 ) -> Result<MDBook> {
95 let root = book_root.into();
96
97 let src_dir = root.join(&config.book.src);
98 let book = load_book_from_disk(&summary, src_dir)?;
99
100 let renderers = determine_renderers(&config)?;
101 let preprocessors = determine_preprocessors(&config, &root)?;
102
103 Ok(MDBook {
104 root,
105 config,
106 book,
107 renderers,
108 preprocessors,
109 })
110 }
111
112 pub fn iter(&self) -> BookItems<'_> {
136 self.book.iter()
137 }
138
139 pub fn init<P: Into<PathBuf>>(book_root: P) -> BookBuilder {
157 BookBuilder::new(book_root)
158 }
159
160 pub fn build(&self) -> Result<()> {
162 info!("Book building has started");
163
164 for renderer in self.renderers.values() {
165 self.execute_build_process(&**renderer)?;
166 }
167
168 Ok(())
169 }
170
171 pub fn preprocess_book(&self, renderer: &dyn Renderer) -> Result<(Book, PreprocessorContext)> {
173 let preprocess_ctx = PreprocessorContext::new(
174 self.root.clone(),
175 self.config.clone(),
176 renderer.name().to_string(),
177 );
178 let mut preprocessed_book = self.book.clone();
179 for preprocessor in self.preprocessors.values() {
180 if preprocessor_should_run(&**preprocessor, renderer, &self.config)? {
181 debug!("Running the {} preprocessor.", preprocessor.name());
182 preprocessed_book = preprocessor.run(&preprocess_ctx, preprocessed_book)?;
183 }
184 }
185 Ok((preprocessed_book, preprocess_ctx))
186 }
187
188 pub fn execute_build_process(&self, renderer: &dyn Renderer) -> Result<()> {
190 let (preprocessed_book, preprocess_ctx) = self.preprocess_book(renderer)?;
191
192 let name = renderer.name();
193 let build_dir = self.build_dir_for(name);
194
195 let mut render_context = RenderContext::new(
196 self.root.clone(),
197 preprocessed_book,
198 self.config.clone(),
199 build_dir,
200 );
201 render_context
202 .chapter_titles
203 .extend(preprocess_ctx.chapter_titles.borrow_mut().drain());
204
205 info!("Running the {} backend", renderer.name());
206 renderer
207 .render(&render_context)
208 .with_context(|| "Rendering failed")
209 }
210
211 pub fn with_renderer<R: Renderer + 'static>(&mut self, renderer: R) -> &mut Self {
215 self.renderers
216 .insert(renderer.name().to_string(), Box::new(renderer));
217 self
218 }
219
220 pub fn with_preprocessor<P: Preprocessor + 'static>(&mut self, preprocessor: P) -> &mut Self {
222 self.preprocessors
223 .insert(preprocessor.name().to_string(), Box::new(preprocessor));
224 self
225 }
226
227 pub fn test(&mut self, library_paths: Vec<&str>) -> Result<()> {
229 self.test_chapter(library_paths, None)
231 }
232
233 pub fn test_chapter(&mut self, library_paths: Vec<&str>, chapter: Option<&str>) -> Result<()> {
236 let cwd = std::env::current_dir()?;
237 let library_args: Vec<OsString> = library_paths
238 .into_iter()
239 .flat_map(|path| {
240 let path = Path::new(path);
241 let path = if path.is_relative() {
242 cwd.join(path).into_os_string()
243 } else {
244 path.to_path_buf().into_os_string()
245 };
246 [OsString::from("-L"), path]
247 })
248 .collect();
249
250 let temp_dir = TempFileBuilder::new().prefix("mdbook-").tempdir()?;
251
252 let mut chapter_found = false;
253
254 struct TestRenderer;
255 impl Renderer for TestRenderer {
256 fn name(&self) -> &str {
258 "test"
259 }
260
261 fn render(&self, _: &RenderContext) -> Result<()> {
262 Ok(())
263 }
264 }
265
266 let (book, _) = self.preprocess_book(&TestRenderer)?;
267
268 let color_output = std::io::stderr().is_terminal();
269 let mut failed = false;
270 for item in book.iter() {
271 if let BookItem::Chapter(ref ch) = *item {
272 let chapter_path = match ch.path {
273 Some(ref path) if !path.as_os_str().is_empty() => path,
274 _ => continue,
275 };
276
277 if let Some(chapter) = chapter {
278 if ch.name != chapter && chapter_path.to_str() != Some(chapter) {
279 if chapter == "?" {
280 info!("Skipping chapter '{}'...", ch.name);
281 }
282 continue;
283 }
284 }
285 chapter_found = true;
286 info!("Testing chapter '{}': {:?}", ch.name, chapter_path);
287
288 let path = temp_dir.path().join(chapter_path);
290 fs::write(&path, &ch.content)?;
291
292 let mut cmd = Command::new("rustdoc");
293 cmd.current_dir(temp_dir.path())
294 .arg(chapter_path)
295 .arg("--test")
296 .args(&library_args);
297
298 if let Some(edition) = self.config.rust.edition {
299 match edition {
300 RustEdition::E2015 => {
301 cmd.args(["--edition", "2015"]);
302 }
303 RustEdition::E2018 => {
304 cmd.args(["--edition", "2018"]);
305 }
306 RustEdition::E2021 => {
307 cmd.args(["--edition", "2021"]);
308 }
309 RustEdition::E2024 => {
310 cmd.args(["--edition", "2024"]);
311 }
312 _ => panic!("RustEdition {edition:?} not covered"),
313 }
314 }
315
316 if color_output {
317 cmd.args(["--color", "always"]);
318 }
319
320 debug!("running {:?}", cmd);
321 let output = cmd
322 .output()
323 .with_context(|| "failed to execute `rustdoc`")?;
324
325 if !output.status.success() {
326 failed = true;
327 eprintln!(
328 "ERROR rustdoc returned an error:\n\
329 \n--- stdout\n{}\n--- stderr\n{}",
330 String::from_utf8_lossy(&output.stdout),
331 String::from_utf8_lossy(&output.stderr)
332 );
333 }
334 }
335 }
336 if failed {
337 bail!("One or more tests failed");
338 }
339 if let Some(chapter) = chapter {
340 if !chapter_found {
341 bail!("Chapter not found: {}", chapter);
342 }
343 }
344 Ok(())
345 }
346
347 pub fn build_dir_for(&self, backend_name: &str) -> PathBuf {
372 let build_dir = self.root.join(&self.config.build.build_dir);
373
374 if self.renderers.len() <= 1 {
375 build_dir
376 } else {
377 build_dir.join(backend_name)
378 }
379 }
380
381 pub fn source_dir(&self) -> PathBuf {
383 self.root.join(&self.config.book.src)
384 }
385
386 pub fn theme_dir(&self) -> PathBuf {
388 self.config
389 .html_config()
390 .unwrap_or_default()
391 .theme_dir(&self.root)
392 }
393}
394
395#[derive(Deserialize)]
397struct OutputConfig {
398 command: Option<String>,
399}
400
401fn determine_renderers(config: &Config) -> Result<IndexMap<String, Box<dyn Renderer>>> {
403 let mut renderers = IndexMap::new();
404
405 let outputs = config.outputs::<OutputConfig>()?;
406 renderers.extend(outputs.into_iter().map(|(key, table)| {
407 let renderer = if key == "html" {
408 Box::new(HtmlHandlebars::new()) as Box<dyn Renderer>
409 } else if key == "markdown" {
410 Box::new(MarkdownRenderer::new()) as Box<dyn Renderer>
411 } else {
412 let command = table.command.unwrap_or_else(|| format!("mdbook-{key}"));
413 Box::new(CmdRenderer::new(key.clone(), command))
414 };
415 (key, renderer)
416 }));
417
418 if renderers.is_empty() {
420 renderers.insert("html".to_string(), Box::new(HtmlHandlebars::new()));
421 }
422
423 Ok(renderers)
424}
425
426const DEFAULT_PREPROCESSORS: &[&str] = &["links", "index"];
427
428fn is_default_preprocessor(pre: &dyn Preprocessor) -> bool {
429 let name = pre.name();
430 name == LinkPreprocessor::NAME || name == IndexPreprocessor::NAME
431}
432
433#[derive(Deserialize)]
435struct PreprocessorConfig {
436 command: Option<String>,
437 #[serde(default)]
438 before: Vec<String>,
439 #[serde(default)]
440 after: Vec<String>,
441 #[serde(default)]
442 optional: bool,
443}
444
445fn determine_preprocessors(
447 config: &Config,
448 root: &Path,
449) -> Result<IndexMap<String, Box<dyn Preprocessor>>> {
450 let mut preprocessor_names = TopologicalSort::<String>::new();
453
454 if config.build.use_default_preprocessors {
455 for name in DEFAULT_PREPROCESSORS {
456 preprocessor_names.insert(name.to_string());
457 }
458 }
459
460 let preprocessor_table = config.preprocessors::<PreprocessorConfig>()?;
461
462 for (name, table) in preprocessor_table.iter() {
463 preprocessor_names.insert(name.to_string());
464
465 let exists = |name| {
466 (config.build.use_default_preprocessors && DEFAULT_PREPROCESSORS.contains(&name))
467 || preprocessor_table.contains_key(name)
468 };
469
470 for after in &table.before {
471 if !exists(&after) {
472 warn!(
475 "preprocessor.{}.after contains \"{}\", which was not found",
476 name, after
477 );
478 } else {
479 preprocessor_names.add_dependency(name, after);
480 }
481 }
482
483 for before in &table.after {
484 if !exists(&before) {
485 warn!(
487 "preprocessor.{}.before contains \"{}\", which was not found",
488 name, before
489 );
490 } else {
491 preprocessor_names.add_dependency(before, name);
492 }
493 }
494 }
495
496 let mut preprocessors = IndexMap::with_capacity(preprocessor_names.len());
498 for mut names in std::iter::repeat_with(|| preprocessor_names.pop_all())
500 .take_while(|names| !names.is_empty())
501 {
502 names.sort();
510 for name in names {
511 let preprocessor: Box<dyn Preprocessor> = match name.as_str() {
512 "links" => Box::new(LinkPreprocessor::new()),
513 "index" => Box::new(IndexPreprocessor::new()),
514 _ => {
515 let table = &preprocessor_table[&name];
518 let command = table
519 .command
520 .to_owned()
521 .unwrap_or_else(|| format!("mdbook-{name}"));
522 Box::new(CmdPreprocessor::new(
523 name.clone(),
524 command,
525 root.to_owned(),
526 table.optional,
527 ))
528 }
529 };
530 preprocessors.insert(name, preprocessor);
531 }
532 }
533
534 if preprocessor_names.is_empty() {
537 Ok(preprocessors)
538 } else {
539 Err(Error::msg("Cyclic dependency detected in preprocessors"))
540 }
541}
542
543fn preprocessor_should_run(
550 preprocessor: &dyn Preprocessor,
551 renderer: &dyn Renderer,
552 cfg: &Config,
553) -> Result<bool> {
554 if cfg.build.use_default_preprocessors && is_default_preprocessor(preprocessor) {
556 return preprocessor.supports_renderer(renderer.name());
557 }
558
559 let key = format!("preprocessor.{}.renderers", preprocessor.name());
560 let renderer_name = renderer.name();
561
562 match cfg.get::<Vec<String>>(&key) {
563 Ok(Some(explicit_renderers)) => {
564 Ok(explicit_renderers.iter().any(|name| name == renderer_name))
565 }
566 Ok(None) => preprocessor.supports_renderer(renderer_name),
567 Err(e) => bail!("failed to get `{key}`: {e}"),
568 }
569}