Skip to content

Commit

Permalink
Fix {{#include}} directives for default language
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruin0x11 committed Sep 15, 2021
1 parent 98c3a04 commit 8d1c086
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 15 deletions.
1 change: 1 addition & 0 deletions guide/src/en/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [Syntax highlighting](format/theme/syntax-highlighting.md)
- [Editor](format/theme/editor.md)
- [MathJax Support](format/mathjax.md)
- [Localization](format/localization.md)
- [mdBook-specific features](format/mdbook.md)
- [Continuous Integration](continuous-integration.md)
- [For Developers](for_developers/README.md)
Expand Down
1 change: 1 addition & 0 deletions guide/src/en/format/configuration/localization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Localization
36 changes: 26 additions & 10 deletions src/book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,26 +215,35 @@ impl MDBook {

/// Run the entire build process for a particular [`Renderer`].
pub fn execute_build_process(&self, renderer: &dyn Renderer) -> Result<()> {
let preprocess_ctx = PreprocessorContext::new(
self.root.clone(),
self.build_opts.clone(),
self.config.clone(),
renderer.name().to_string(),
);

let preprocessed_books = match &self.book {
LoadedBook::Localized(ref books) => {
let mut new_books = HashMap::new();

for (ident, book) in books.0.iter() {
for (language_ident, book) in books.0.iter() {
let preprocess_ctx = PreprocessorContext::new(
self.root.clone(),
Some(language_ident.clone()),
self.build_opts.clone(),
self.config.clone(),
renderer.name().to_string(),
);

let preprocessed_book =
self.preprocess(&preprocess_ctx, renderer, book.clone())?;
new_books.insert(ident.clone(), preprocessed_book);
new_books.insert(language_ident.clone(), preprocessed_book);
}

LoadedBook::Localized(LocalizedBooks(new_books))
}
LoadedBook::Single(ref book) => {
let preprocess_ctx = PreprocessorContext::new(
self.root.clone(),
None,
self.build_opts.clone(),
self.config.clone(),
renderer.name().to_string(),
);

LoadedBook::Single(self.preprocess(&preprocess_ctx, renderer, book.clone())?)
}
};
Expand Down Expand Up @@ -273,10 +282,17 @@ impl MDBook {
self
}

fn test_book(&self, book: &Book, temp_dir: &TempDir, library_args: &Vec<&str>) -> Result<()> {
fn test_book(
&self,
book: &Book,
temp_dir: &TempDir,
library_args: &Vec<&str>,
language_ident: Option<String>,
) -> Result<()> {
// FIXME: Is "test" the proper renderer name to use here?
let preprocess_context = PreprocessorContext::new(
self.root.clone(),
language_ident,
self.build_opts.clone(),
self.config.clone(),
"test".to_string(),
Expand Down
9 changes: 5 additions & 4 deletions src/cmd/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ pub fn execute(args: &ArgMatches) -> Result<()> {

trigger_on_change(&book, |paths, book_dir| {
info!("Files changed: {:?}\nBuilding book...\n", paths);
let result = MDBook::load_with_build_opts(&book_dir, build_opts.clone()).and_then(|mut b| {
update_config(&mut b);
b.build()
});
let result =
MDBook::load_with_build_opts(&book_dir, build_opts.clone()).and_then(|mut b| {
update_config(&mut b);
b.build()
});

if let Err(e) = result {
error!("Unable to build the book");
Expand Down
6 changes: 5 additions & 1 deletion src/preprocess/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ impl Preprocessor for LinkPreprocessor {
}

fn run(&self, ctx: &PreprocessorContext, mut book: Book) -> Result<Book> {
let src_dir = ctx.source_dir();
let src_dir = ctx
.config
.get_localized_src_path(ctx.language_ident.as_ref())
.unwrap();
let src_dir = ctx.root.join(src_dir);

book.for_each_mut(|section: &mut BookItem| {
if let BookItem::Chapter(ref mut ch) = *section {
Expand Down
5 changes: 5 additions & 0 deletions src/preprocess/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ use std::path::PathBuf;
pub struct PreprocessorContext {
/// The location of the book directory on disk.
pub root: PathBuf,
/// The language of the book being built. Is only `Some` if the book is part
/// of a multilingual build output.
pub language_ident: Option<String>,
/// The build options passed from the frontend.
pub build_opts: BuildOpts,
/// The book configuration (`book.toml`).
Expand All @@ -41,12 +44,14 @@ impl PreprocessorContext {
/// Create a new `PreprocessorContext`.
pub(crate) fn new(
root: PathBuf,
language_ident: Option<String>,
build_opts: BuildOpts,
config: Config,
renderer: String,
) -> Self {
PreprocessorContext {
root,
language_ident,
build_opts,
config,
renderer,
Expand Down

0 comments on commit 8d1c086

Please sign in to comment.