Skip to content

Commit

Permalink
Rewrite links in Markdown to point to fallback if missing in translation
Browse files Browse the repository at this point in the history
It will follow relative links to other pages and embedded images.
  • Loading branch information
Ruin0x11 committed Sep 15, 2021
1 parent ee740ac commit c72ce18
Show file tree
Hide file tree
Showing 14 changed files with 296 additions and 102 deletions.
9 changes: 5 additions & 4 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,11 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
info!("Building book...");

// FIXME: This area is really ugly because we need to re-set livereload :(
let result = MDBook::load(&book_dir).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 load the book");
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
pub fn execute(args: &ArgMatches) -> Result<()> {
let book_dir = get_book_dir(args);
let build_opts = get_build_opts(args);
let mut book = MDBook::load_with_build_opts(&book_dir, build_opts)?;
let mut book = MDBook::load_with_build_opts(&book_dir, build_opts.clone())?;

let update_config = |book: &mut MDBook| {
if let Some(dest_dir) = args.value_of("dest-dir") {
Expand All @@ -50,7 +50,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {

trigger_on_change(&book, |paths, book_dir| {
info!("Files changed: {:?}\nBuilding book...\n", paths);
let result = MDBook::load(&book_dir).and_then(|mut b| {
let result = MDBook::load_with_build_opts(&book_dir, build_opts.clone()).and_then(|mut b| {
update_config(&mut b);
b.build()
});
Expand Down
48 changes: 40 additions & 8 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ impl HtmlHandlebars {

let mut is_index = true;
for item in book.iter() {
let ctx = RenderItemContext {
let item_ctx = RenderItemContext {
handlebars: &handlebars,
destination: destination.to_path_buf(),
data: data.clone(),
is_index,
html_config: html_config.clone(),
edition: ctx.config.rust.edition,
};
self.render_item(item, ctx, &mut print_content)?;
self.render_item(item, item_ctx, src_dir, &ctx.config, &mut print_content)?;
is_index = false;
}

Expand All @@ -138,6 +138,7 @@ impl HtmlHandlebars {
&html_config,
src_dir,
destination,
language_ident,
handlebars,
&mut data,
)?;
Expand Down Expand Up @@ -193,6 +194,8 @@ impl HtmlHandlebars {
&self,
item: &BookItem,
mut ctx: RenderItemContext<'_>,
src_dir: &PathBuf,
cfg: &Config,
print_content: &mut String,
) -> Result<()> {
// FIXME: This should be made DRY-er and rely less on mutable state
Expand All @@ -216,11 +219,29 @@ impl HtmlHandlebars {
.insert("git_repository_edit_url".to_owned(), json!(edit_url));
}

let fallback_path = cfg.default_language().map(|lang_ident| {
let mut fallback = PathBuf::from(utils::fs::path_to_root(&path));
fallback.push("../");
fallback.push(lang_ident.clone());
fallback
});

let content = ch.content.clone();
let content = utils::render_markdown(&content, ctx.html_config.curly_quotes);
let content = utils::render_markdown_with_path(
&content,
ctx.html_config.curly_quotes,
Some(&path),
Some(&src_dir),
&fallback_path,
);

let fixed_content =
utils::render_markdown_with_path(&ch.content, ctx.html_config.curly_quotes, Some(path));
let fixed_content = utils::render_markdown_with_path(
&ch.content,
ctx.html_config.curly_quotes,
Some(&path),
Some(&src_dir),
&fallback_path,
);
if !ctx.is_index {
// Add page break between chapters
// See https://developer.mozilla.org/en-US/docs/Web/CSS/break-before and https://developer.mozilla.org/en-US/docs/Web/CSS/page-break-before
Expand Down Expand Up @@ -298,6 +319,7 @@ impl HtmlHandlebars {
html_config: &HtmlConfig,
src_dir: &PathBuf,
destination: &PathBuf,
language_ident: &Option<String>,
handlebars: &mut Handlebars<'_>,
data: &mut serde_json::Map<String, serde_json::Value>,
) -> Result<()> {
Expand All @@ -321,16 +343,26 @@ impl HtmlHandlebars {
let html_content_404 = utils::render_markdown(&content_404, html_config.curly_quotes);

let mut data_404 = data.clone();
let base_url = if let Some(site_url) = &html_config.site_url {
site_url
let mut base_url = if let Some(site_url) = &html_config.site_url {
site_url.clone()
} else {
debug!(
"HTML 'site-url' parameter not set, defaulting to '/'. Please configure \
this to ensure the 404 page work correctly, especially if your site is hosted in a \
subdirectory on the HTTP server."
);
"/"
String::from("/")
};

// Set the subdirectory to the currently localized version if using a
// multilingual output format.
if let LoadedBook::Localized(_) = ctx.book {
if let Some(lang_ident) = language_ident {
base_url.push_str(lang_ident);
base_url.push_str("/");
}
}

data_404.insert("base_url".to_owned(), json!(base_url));
// Set a dummy path to ensure other paths (e.g. in the TOC) are generated correctly
data_404.insert("path".to_owned(), json!("404.md"));
Expand Down
Loading

0 comments on commit c72ce18

Please sign in to comment.