| 1 |
//! The high-level interface for loading and rendering books. |
| 2 |
|
| 3 |
use crate::builtin_preprocessors::{CmdPreprocessor, IndexPreprocessor, LinkPreprocessor}; |
| 4 |
use crate::builtin_renderers::{CmdRenderer, MarkdownRenderer}; |
| 5 |
use crate::init::BookBuilder; |
| 6 |
use crate::load::{load_book, load_book_from_disk}; |
| 7 |
use anyhow::{Context, Error, Result, bail}; |
| 8 |
use indexmap::IndexMap; |
| 9 |
use mdbook_core::book::{Book, BookItem, BookItems}; |
| 10 |
use mdbook_core::config::{Config, RustEdition}; |
| 11 |
use mdbook_core::utils::fs; |
| 12 |
use mdbook_html::HtmlHandlebars; |
| 13 |
use mdbook_preprocessor::{Preprocessor, PreprocessorContext}; |
| 14 |
use mdbook_renderer::{RenderContext, Renderer}; |
| 15 |
use mdbook_summary::Summary; |
| 16 |
use serde::Deserialize; |
| 17 |
use std::ffi::OsString; |
| 18 |
use std::io::IsTerminal; |
| 19 |
use std::path::{Path, PathBuf}; |
| 20 |
use std::process::Command; |
| 21 |
use tempfile::Builder as TempFileBuilder; |
| 22 |
use topological_sort::TopologicalSort; |
| 23 |
use tracing::{debug, info, trace, warn}; |
| 24 |
|
| 25 |
#[cfg(test)] |
| 26 |
mod tests; |
| 27 |
|
| 28 |
/// The object used to manage and build a book. |
| 29 |
pub struct MDBook { |
| 30 |
/// The book's root directory. |
| 31 |
pub root: PathBuf, |
| 32 |
|
| 33 |
/// The configuration used to tweak now a book is built. |
| 34 |
pub config: Config, |
| 35 |
|
| 36 |
/// A representation of the book's contents in memory. |
| 37 |
pub book: Book, |
| 38 |
|
| 39 |
/// Renderers to execute. |
| 40 |
renderers: IndexMap<String, Box<dyn Renderer>>, |
| 41 |
|
| 42 |
/// Pre-processors to be run on the book. |
| 43 |
preprocessors: IndexMap<String, Box<dyn Preprocessor>>, |
| 44 |
} |
| 45 |
|
| 46 |
impl MDBook { |
| 47 |
/// Load a book from its root directory on disk. |
| 48 |
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 |
/// Load a book from its root directory using a custom `Config`. |
| 71 |
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 |
/// Load a book from its root directory using a custom `Config` and a custom summary. |
| 90 |
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 |
/// Returns a flat depth-first iterator over the [`BookItem`]s of the book. |
| 113 |
/// |
| 114 |
/// ```no_run |
| 115 |
/// # use mdbook_driver::MDBook; |
| 116 |
/// # use mdbook_driver::book::BookItem; |
| 117 |
/// # let book = MDBook::load("mybook").unwrap(); |
| 118 |
/// for item in book.iter() { |
| 119 |
/// match *item { |
| 120 |
/// BookItem::Chapter(ref chapter) => {}, |
| 121 |
/// BookItem::Separator => {}, |
| 122 |
/// BookItem::PartTitle(ref title) => {} |
| 123 |
/// _ => {} |
| 124 |
/// } |
| 125 |
/// } |
| 126 |
/// |
| 127 |
/// // would print something like this: |
| 128 |
/// // 1. Chapter 1 |
| 129 |
/// // 1.1 Sub Chapter |
| 130 |
/// // 1.2 Sub Chapter |
| 131 |
/// // 2. Chapter 2 |
| 132 |
/// // |
| 133 |
/// // etc. |
| 134 |
/// ``` |
| 135 |
pub fn iter(&self) -> BookItems<'_> { |
| 136 |
self.book.iter() |
| 137 |
} |
| 138 |
|
| 139 |
/// `init()` gives you a `BookBuilder` which you can use to setup a new book |
| 140 |
/// and its accompanying directory structure. |
| 141 |
/// |
| 142 |
/// The `BookBuilder` creates some boilerplate files and directories to get |
| 143 |
/// you started with your book. |
| 144 |
/// |
| 145 |
/// ```text |
| 146 |
/// book-test/ |
| 147 |
/// ├── book |
| 148 |
/// └── src |
| 149 |
/// ├── chapter_1.md |
| 150 |
/// └── SUMMARY.md |
| 151 |
/// ``` |
| 152 |
/// |
| 153 |
/// It uses the path provided as the root directory for your book, then adds |
| 154 |
/// in a `src/` directory containing a `SUMMARY.md` and `chapter_1.md` file |
| 155 |
/// to get you started. |
| 156 |
pub fn init<P: Into<PathBuf>>(book_root: P) -> BookBuilder { |
| 157 |
BookBuilder::new(book_root) |
| 158 |
} |
| 159 |
|
| 160 |
/// Tells the renderer to build our book and put it in the build directory. |
| 161 |
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 |
/// Run preprocessors and return the final book. |
| 172 |
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 |
/// Run the entire build process for a particular [`Renderer`]. |
| 189 |
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 |
/// You can change the default renderer to another one by using this method. |
| 212 |
/// The only requirement is that your renderer implement the [`Renderer`] |
| 213 |
/// trait. |
| 214 |
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 |
/// Register a [`Preprocessor`] to be used when rendering the book. |
| 221 |
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 |
/// Run `rustdoc` tests on the book, linking against the provided libraries. |
| 228 |
pub fn test(&mut self, library_paths: Vec<&str>) -> Result<()> { |
| 229 |
// test_chapter with chapter:None will run all tests. |
| 230 |
self.test_chapter(library_paths, None) |
| 231 |
} |
| 232 |
|
| 233 |
/// Run `rustdoc` tests on a specific chapter of the book, linking against the provided libraries. |
| 234 |
/// If `chapter` is `None`, all tests will be run. |
| 235 |
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 |
// FIXME: Is "test" the proper renderer name to use here? |
| 257 |
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 |
// write preprocessed file to tempdir |
| 289 |
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 |
/// The logic for determining where a backend should put its build |
| 348 |
/// artefacts. |
| 349 |
/// |
| 350 |
/// If there is only 1 renderer, put it in the directory pointed to by the |
| 351 |
/// `build.build_dir` key in [`Config`]. If there is more than one then the |
| 352 |
/// renderer gets its own directory within the main build dir. |
| 353 |
/// |
| 354 |
/// i.e. If there were only one renderer (in this case, the HTML renderer): |
| 355 |
/// |
| 356 |
/// - build/ |
| 357 |
/// - index.html |
| 358 |
/// - ... |
| 359 |
/// |
| 360 |
/// Otherwise if there are multiple: |
| 361 |
/// |
| 362 |
/// - build/ |
| 363 |
/// - epub/ |
| 364 |
/// - my_awesome_book.epub |
| 365 |
/// - html/ |
| 366 |
/// - index.html |
| 367 |
/// - ... |
| 368 |
/// - latex/ |
| 369 |
/// - my_awesome_book.tex |
| 370 |
/// |
| 371 |
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 |
/// Get the directory containing this book's source files. |
| 382 |
pub fn source_dir(&self) -> PathBuf { |
| 383 |
self.root.join(&self.config.book.src) |
| 384 |
} |
| 385 |
|
| 386 |
/// Get the directory containing the theme resources for the book. |
| 387 |
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 |
/// An `output` table. |
| 396 |
#[derive(Deserialize)] |
| 397 |
struct OutputConfig { |
| 398 |
command: Option<String>, |
| 399 |
} |
| 400 |
|
| 401 |
/// Look at the `Config` and try to figure out what renderers to use. |
| 402 |
fn 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 we couldn't find anything, add the HTML renderer as a default |
| 419 |
if renderers.is_empty() { |
| 420 |
renderers.insert("html".to_string(), Box::new(HtmlHandlebars::new())); |
| 421 |
} |
| 422 |
|
| 423 |
Ok(renderers) |
| 424 |
} |
| 425 |
|
| 426 |
const DEFAULT_PREPROCESSORS: &[&str] = &["links", "index"]; |
| 427 |
|
| 428 |
fn is_default_preprocessor(pre: &dyn Preprocessor) -> bool { |
| 429 |
let name = pre.name(); |
| 430 |
name == LinkPreprocessor::NAME || name == IndexPreprocessor::NAME |
| 431 |
} |
| 432 |
|
| 433 |
/// A `preprocessor` table. |
| 434 |
#[derive(Deserialize)] |
| 435 |
struct 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 |
|
| 445 |
/// Look at the `MDBook` and try to figure out what preprocessors to run. |
| 446 |
fn determine_preprocessors( |
| 447 |
config: &Config, |
| 448 |
root: &Path, |
| 449 |
) -> Result<IndexMap<String, Box<dyn Preprocessor>>> { |
| 450 |
// Collect the names of all preprocessors intended to be run, and the order |
| 451 |
// in which they should be run. |
| 452 |
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 |
// Only warn so that preprocessors can be toggled on and off (e.g. for |
| 473 |
// troubleshooting) without having to worry about order too much. |
| 474 |
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 |
// See equivalent warning above for rationale |
| 486 |
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 |
// Now that all links have been established, queue preprocessors in a suitable order |
| 497 |
let mut preprocessors = IndexMap::with_capacity(preprocessor_names.len()); |
| 498 |
// `pop_all()` returns an empty vector when no more items are not being depended upon |
| 499 |
for mut names in std::iter::repeat_with(|| preprocessor_names.pop_all()) |
| 500 |
.take_while(|names| !names.is_empty()) |
| 501 |
{ |
| 502 |
// The `topological_sort` crate does not guarantee a stable order for ties, even across |
| 503 |
// runs of the same program. Thus, we break ties manually by sorting. |
| 504 |
// Careful: `str`'s default sorting, which we are implicitly invoking here, uses code point |
| 505 |
// values ([1]), which may not be an alphabetical sort. |
| 506 |
// As mentioned in [1], doing so depends on locale, which is not desirable for deciding |
| 507 |
// preprocessor execution order. |
| 508 |
// [1]: https://doc.rust-lang.org/stable/std/cmp/trait.Ord.html#impl-Ord-14 |
| 509 |
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 |
// The only way to request a custom preprocessor is through the `preprocessor` |
| 516 |
// table, so it must exist, be a table, and contain the key. |
| 517 |
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 `pop_all` returns an empty vector and `len` is not 0, there are cyclic dependencies." |
| 535 |
// Normally, `len() == 0` is equivalent to `is_empty()`, so we'll use that. |
| 536 |
if preprocessor_names.is_empty() { |
| 537 |
Ok(preprocessors) |
| 538 |
} else { |
| 539 |
Err(Error::msg("Cyclic dependency detected in preprocessors")) |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
/// Check whether we should run a particular `Preprocessor` in combination |
| 544 |
/// with the renderer, falling back to `Preprocessor::supports_renderer()` |
| 545 |
/// method if the user doesn't say anything. |
| 546 |
/// |
| 547 |
/// The `build.use-default-preprocessors` config option can be used to ensure |
| 548 |
/// default preprocessors always run if they support the renderer. |
| 549 |
fn preprocessor_should_run( |
| 550 |
preprocessor: &dyn Preprocessor, |
| 551 |
renderer: &dyn Renderer, |
| 552 |
cfg: &Config, |
| 553 |
) -> Result<bool> { |
| 554 |
// default preprocessors should be run by default (if supported) |
| 555 |
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 |
} |