-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add index preprocessor README.md is a de facto index file in markdown-based documentation. Hence, we respect to README.md and convert it into index.html. * Fix warning for unused variables * Update tests for config * Match file stem case-insensitively for IndexPreprocessor * Add tests for IndexPreprocessor * Update book example to fit index preprocessor
- Loading branch information
1 parent
69fef40
commit 6959964
Showing
15 changed files
with
162 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,26 @@ | ||
# Summary | ||
|
||
- [mdBook](README.md) | ||
- [Command Line Tool](cli/cli-tool.md) | ||
- [Command Line Tool](cli/README.md) | ||
- [init](cli/init.md) | ||
- [build](cli/build.md) | ||
- [watch](cli/watch.md) | ||
- [serve](cli/serve.md) | ||
- [test](cli/test.md) | ||
- [clean](cli/clean.md) | ||
- [Format](format/format.md) | ||
- [Format](format/README.md) | ||
- [SUMMARY.md](format/summary.md) | ||
- [Configuration](format/config.md) | ||
- [Theme](format/theme/theme.md) | ||
- [Theme](format/theme/README.md) | ||
- [index.hbs](format/theme/index-hbs.md) | ||
- [Syntax highlighting](format/theme/syntax-highlighting.md) | ||
- [Editor](format/theme/editor.md) | ||
- [MathJax Support](format/mathjax.md) | ||
- [mdBook specific features](format/mdbook.md) | ||
- [For Developers](for_developers/index.md) | ||
- [For Developers](for_developers/README.md) | ||
- [Preprocessors](for_developers/preprocessors.md) | ||
- [Alternate Backends](for_developers/backends.md) | ||
|
||
----------- | ||
|
||
[Contributors](misc/contributors.md) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use std::path::Path; | ||
use regex::Regex; | ||
|
||
use errors::*; | ||
|
||
use super::{Preprocessor, PreprocessorContext}; | ||
use book::{Book, BookItem}; | ||
|
||
/// A preprocessor for converting file name `README.md` to `index.md` since | ||
/// `README.md` is the de facto index file in a markdown-based documentation. | ||
pub struct IndexPreprocessor; | ||
|
||
impl IndexPreprocessor { | ||
/// Create a new `IndexPreprocessor`. | ||
pub fn new() -> Self { | ||
IndexPreprocessor | ||
} | ||
} | ||
|
||
impl Preprocessor for IndexPreprocessor { | ||
fn name(&self) -> &str { | ||
"index" | ||
} | ||
|
||
fn run(&self, ctx: &PreprocessorContext, book: &mut Book) -> Result<()> { | ||
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 is_readme_file(&ch.path) { | ||
let index_md = source_dir | ||
.join(ch.path.with_file_name("index.md")); | ||
if index_md.exists() { | ||
warn_readme_name_conflict(&ch.path, &index_md); | ||
} | ||
|
||
ch.path.set_file_name("index.md"); | ||
} | ||
} | ||
}); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
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(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 { | ||
lazy_static! { | ||
static ref RE: Regex = Regex::new(r"(?i)^readme$").unwrap(); | ||
} | ||
RE.is_match( | ||
path.as_ref() | ||
.file_stem() | ||
.and_then(|s| s.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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Root README |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# This dummy book is for testing the conversion of README.md to index.html by IndexPreprocessor | ||
|
||
[Root README](README.md) | ||
|
||
- [1st README](first/README.md) | ||
- [2nd README](second/README.md) | ||
- [2nd index](second/index.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# First README |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Second README |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Second index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters