This switches to using the tracing crate instead of log. Tracing provides a lot of nice features which we can take advantage of moving forward. This also adjusts the output fairly significantly. This includes: - Switched the environment variable from RUST_LOG to MDBOOK_LOG. - Dropped the timestamp. I experimented with various different time displays, but ultimately decided to omit it for now. I don't think I've ever found it to be useful, and it takes up a very significant amount of space. It could potentially be useful for basic profiling, but I think there are other, better mechanisms for that. We could consider leveraging tracing itself for doing some basic profiling (like using something like tracing-chrome). - Dropped the target unless MDBOOK_LOG is set. The target tends to be pretty noisy, and doesn't really convey much information unless you are debugging or otherwise trying to adjust the log output. - Added color. - Slightly reworked the way the error cause trace is displayed. - Slightly changed the way html5ever filtering is done, as well as add handlebars to the list since they both are very noisy. You can override this now by explicitly listing them as targets. I still expect that mdbook will eventually change how it displays things to the console, possibly switching away from tracing and printing things itself. However, that is a larger project for the future.
105 lines
3.1 KiB
Rust
105 lines
3.1 KiB
Rust
use anyhow::Result;
|
|
use mdbook_core::book::{Book, BookItem};
|
|
use mdbook_preprocessor::{Preprocessor, PreprocessorContext};
|
|
use regex::Regex;
|
|
use std::{path::Path, sync::LazyLock};
|
|
use tracing::warn;
|
|
|
|
/// A preprocessor for converting file name `README.md` to `index.md` since
|
|
/// `README.md` is the de facto index file in markdown-based documentation.
|
|
#[derive(Default)]
|
|
#[non_exhaustive]
|
|
pub struct IndexPreprocessor;
|
|
|
|
impl IndexPreprocessor {
|
|
/// Name of this preprocessor.
|
|
pub const NAME: &'static str = "index";
|
|
|
|
/// Create a new `IndexPreprocessor`.
|
|
pub fn new() -> Self {
|
|
IndexPreprocessor
|
|
}
|
|
}
|
|
|
|
impl Preprocessor for IndexPreprocessor {
|
|
fn name(&self) -> &str {
|
|
Self::NAME
|
|
}
|
|
|
|
fn run(&self, ctx: &PreprocessorContext, mut book: Book) -> Result<Book> {
|
|
let source_dir = ctx.root.join(&ctx.config.book.src);
|
|
book.for_each_mut(|section: &mut BookItem| {
|
|
if let BookItem::Chapter(ref mut ch) = *section {
|
|
if let Some(ref mut path) = ch.path {
|
|
if is_readme_file(&path) {
|
|
let mut index_md = source_dir.join(path.with_file_name("index.md"));
|
|
if index_md.exists() {
|
|
warn_readme_name_conflict(&path, &&mut index_md);
|
|
}
|
|
|
|
path.set_file_name("index.md");
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
Ok(book)
|
|
}
|
|
}
|
|
|
|
fn warn_readme_name_conflict<P: AsRef<Path>>(readme_path: P, index_path: P) {
|
|
let file_name = readme_path.as_ref().file_name().unwrap_or_default();
|
|
let parent_dir = index_path
|
|
.as_ref()
|
|
.parent()
|
|
.unwrap_or_else(|| index_path.as_ref());
|
|
warn!(
|
|
"It seems that there are both {:?} and index.md under \"{}\".",
|
|
file_name,
|
|
parent_dir.display()
|
|
);
|
|
warn!(
|
|
"mdbook converts {:?} into index.html by default. It may cause",
|
|
file_name
|
|
);
|
|
warn!("unexpected behavior if putting both files under the same directory.");
|
|
warn!("To solve the warning, try to rearrange the book structure or disable");
|
|
warn!("\"index\" preprocessor to stop the conversion.");
|
|
}
|
|
|
|
fn is_readme_file<P: AsRef<Path>>(path: P) -> bool {
|
|
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)^readme$").unwrap());
|
|
|
|
RE.is_match(
|
|
path.as_ref()
|
|
.file_stem()
|
|
.and_then(std::ffi::OsStr::to_str)
|
|
.unwrap_or_default(),
|
|
)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn file_stem_exactly_matches_readme_case_insensitively() {
|
|
let path = "path/to/Readme.md";
|
|
assert!(is_readme_file(path));
|
|
|
|
let path = "path/to/README.md";
|
|
assert!(is_readme_file(path));
|
|
|
|
let path = "path/to/rEaDmE.md";
|
|
assert!(is_readme_file(path));
|
|
|
|
let path = "path/to/README.markdown";
|
|
assert!(is_readme_file(path));
|
|
|
|
let path = "path/to/README";
|
|
assert!(is_readme_file(path));
|
|
|
|
let path = "path/to/README-README.md";
|
|
assert!(!is_readme_file(path));
|
|
}
|
|
}
|