cross-link translations by translation_id and src_path
This commit is contained in:
parent
4973c02554
commit
badc921429
|
@ -496,6 +496,46 @@ impl MDBook {
|
|||
None => {},
|
||||
}
|
||||
|
||||
// Find a translation for the links that are still None
|
||||
|
||||
for (key, trl) in final_links.clone().iter() {
|
||||
match trl.link {
|
||||
Some(_) => { continue; },
|
||||
None => {},
|
||||
}
|
||||
|
||||
let b: &Book = self.translations.get(key).unwrap();
|
||||
let flat_toc = toc::flat_toc(&b.toc);
|
||||
|
||||
for item in flat_toc.iter() {
|
||||
match *item {
|
||||
TocItem::Numbered(ref i) |
|
||||
TocItem::Unnumbered(ref i) |
|
||||
TocItem::Unlisted(ref i) => {
|
||||
|
||||
// Note that this will also add a link to itself, which is good.
|
||||
|
||||
if content.is_it_a_translation_of(i) {
|
||||
if let Some(mut a) = i.chapter.get_dest_path() {
|
||||
// Join the path to the language code, i.e. en/tears.html
|
||||
a = PathBuf::from(key.to_string()).join(a);
|
||||
let path = String::from(a.to_str().unwrap());
|
||||
|
||||
final_links.insert(
|
||||
key.to_owned(),
|
||||
TranslationLink::new_with_link(key.to_owned(), path)
|
||||
);
|
||||
} else {
|
||||
debug!("It's a translation but dest_path is not set: {:#?}", i);
|
||||
}
|
||||
break;
|
||||
}
|
||||
},
|
||||
TocItem::Spacer => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let a: Vec<TranslationLink> = final_links.values().map(|x| x.clone()).collect();
|
||||
newcontent.chapter.translation_links = Some(a);
|
||||
|
||||
|
@ -640,3 +680,4 @@ impl MDBook {
|
|||
// }
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -55,6 +55,28 @@ impl TocContent {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn is_it_a_translation_of(&self, checking: &TocContent) -> bool {
|
||||
// if the user has set the same translation_id on them
|
||||
if let Some(ref a) = self.chapter.translation_id {
|
||||
if let Some(ref b) = checking.chapter.translation_id {
|
||||
if a == b {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if src_path matches
|
||||
if let Some(ref a) = self.chapter.get_src_path() {
|
||||
if let Some(ref b) = checking.chapter.get_src_path() {
|
||||
if a == b {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
// TODO update
|
||||
|
||||
// /// This function takes a slice `&[x,y,z]` and returns the corresponding sub-chapter if it exists.
|
||||
|
@ -74,3 +96,22 @@ impl TocContent {
|
|||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
pub fn flat_toc(toc: &Vec<TocItem>) -> Vec<TocItem> {
|
||||
let mut flattened: Vec<TocItem> = vec![];
|
||||
for i in toc.iter() {
|
||||
match *i {
|
||||
TocItem::Numbered(ref x) |
|
||||
TocItem::Unnumbered(ref x) |
|
||||
TocItem::Unlisted(ref x) => {
|
||||
flattened.push(i.clone());
|
||||
if let Some(ref subs) = x.sub_items {
|
||||
let mut a = flat_toc(subs);
|
||||
flattened.append(&mut a);
|
||||
}
|
||||
},
|
||||
TocItem::Spacer => { flattened.push(i.clone()); },
|
||||
}
|
||||
}
|
||||
flattened
|
||||
}
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
+++
|
||||
translation_id = "caucus race"
|
||||
+++
|
||||
|
||||
# A Caucus-Race and a Long Tale
|
||||
|
||||
![Tail](images/Tail.png)
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
[Titre](titre.md)
|
||||
|
||||
- [Au fond du terrier](terrier.md)
|
||||
- [La mare aux larmes](larmes.md)
|
||||
- [La mare aux larmes](tears.md)
|
||||
- [La course cocasse](cocasse.md)
|
||||
- [L'habitation du lapin blanc]()
|
||||
- [Conseils d'une chenille]()
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
[Címoldal](cimoldal.md)
|
||||
|
||||
- [Lenn, a Nyuszi barlangjában](nyuszi.md)
|
||||
- [Könnytó](konnyto.md)
|
||||
- [Könnytó](tears.md)
|
||||
- [Körbecsukó meg az egér hosszú tarka farka](tarka-farka.md)
|
||||
- [Gyíkocska]()
|
||||
- [A hernyó tanácsot ad]()
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
+++
|
||||
translation_id = "caucus race"
|
||||
+++
|
||||
|
||||
# Körbecsukó meg az egér hosszú tarka farka
|
||||
|
||||
![Tarka-farka](images/Tail.png)
|
||||
|
|
|
@ -27,7 +27,7 @@ fn it_renders_multilanguage_book() {
|
|||
|
||||
chapter_path = proj.get_dest_base().join("index.html");
|
||||
s = utils::fs::file_to_string(&chapter_path).unwrap();
|
||||
assert!(s.contains("<title>Alice's Adventures in Wonderland</title>"));
|
||||
assert!(s.contains("<title>Titlepage - Alice's Adventures in Wonderland</title>"));
|
||||
assert!(s.contains("<h1>Alice's Adventures in Wonderland</h1>"));
|
||||
assert!(s.contains("<base href=\"\">"));
|
||||
|
||||
|
@ -40,13 +40,13 @@ fn it_renders_multilanguage_book() {
|
|||
assert!(s.contains("li><a href=\"en/tears.html\" class=\"active\"><strong>2.</strong> The Pool of Tears</a></li>"));
|
||||
|
||||
book_path = proj.translations.get("fr").unwrap().config.get_dest();
|
||||
chapter_path = book_path.join("larmes.html");
|
||||
chapter_path = book_path.join("tears.html");
|
||||
s = utils::fs::file_to_string(&chapter_path).unwrap();
|
||||
assert!(s.contains("<h1>La mare aux larmes</h1>"));
|
||||
assert!(s.contains("<base href=\"../\">"));
|
||||
|
||||
book_path = proj.translations.get("hu").unwrap().config.get_dest();
|
||||
chapter_path = book_path.join("konnyto.html");
|
||||
chapter_path = book_path.join("tears.html");
|
||||
s = utils::fs::file_to_string(&chapter_path).unwrap();
|
||||
assert!(s.contains("<h1>Könnytó</h1>"));
|
||||
assert!(s.contains("<base href=\"../\">"));
|
||||
|
@ -55,6 +55,15 @@ fn it_renders_multilanguage_book() {
|
|||
|
||||
assert!(proj.get_dest_base().join("images").join("Queen.jpg").exists());
|
||||
|
||||
// Test if default translation indexes are displayed
|
||||
|
||||
book_path = proj.translations.get("hu").unwrap().config.get_dest();
|
||||
chapter_path = book_path.join("tarka-farka.html");
|
||||
s = utils::fs::file_to_string(&chapter_path).unwrap();
|
||||
assert!(s.contains("<a href=\"en/index.html\">en</a>"));
|
||||
assert!(s.contains("<a href=\"hu/index.html\">hu</a>"));
|
||||
assert!(s.contains("<a href=\"fr/index.html\">fr</a>"));
|
||||
|
||||
// Test if translation links given in the TOML header were rendered
|
||||
|
||||
book_path = proj.translations.get("en").unwrap().config.get_dest();
|
||||
|
@ -63,14 +72,23 @@ fn it_renders_multilanguage_book() {
|
|||
assert!(s.contains("<a href=\"hu/nyuszi.html\">hu</a>"));
|
||||
assert!(s.contains("<a href=\"fr/terrier.html\">fr</a>"));
|
||||
|
||||
// Test if default translation links are set
|
||||
// Test if translation links by translation_id were found
|
||||
|
||||
book_path = proj.translations.get("hu").unwrap().config.get_dest();
|
||||
chapter_path = book_path.join("tarka-farka.html");
|
||||
book_path = proj.translations.get("en").unwrap().config.get_dest();
|
||||
chapter_path = book_path.join("long-tale.html");
|
||||
s = utils::fs::file_to_string(&chapter_path).unwrap();
|
||||
assert!(s.contains("<a href=\"en/index.html\">en</a>"));
|
||||
assert!(s.contains("<a href=\"hu/index.html\">hu</a>"));
|
||||
assert!(s.contains("<a href=\"fr/index.html\">fr</a>"));
|
||||
assert!(s.contains("<a href=\"en/long-tale.html\">en</a>"));
|
||||
assert!(s.contains("<span>fr</span>"));
|
||||
assert!(s.contains("<a href=\"hu/tarka-farka.html\">hu</a>"));
|
||||
|
||||
// Test if translation links by src_path were found
|
||||
|
||||
book_path = proj.translations.get("en").unwrap().config.get_dest();
|
||||
chapter_path = book_path.join("tears.html");
|
||||
s = utils::fs::file_to_string(&chapter_path).unwrap();
|
||||
assert!(s.contains("<a href=\"en/tears.html\">en</a>"));
|
||||
assert!(s.contains("<a href=\"fr/tears.html\">fr</a>"));
|
||||
assert!(s.contains("<a href=\"hu/tears.html\">hu</a>"));
|
||||
|
||||
// Test if print.html is produced for each translations
|
||||
|
||||
|
|
|
@ -107,6 +107,7 @@ indent_spaces = 2
|
|||
[[translations.en]]
|
||||
title = "Alice's Adventures in Wonderland"
|
||||
author = "Lewis Carroll"
|
||||
is_main_book = true
|
||||
|
||||
[[translations.hu]]
|
||||
title = "Alice Csodaországban"
|
||||
|
@ -168,10 +169,18 @@ name = "Kosztolányi Dezső"
|
|||
|
||||
// Hashmaps are unordered. They don't always print their keys in the same order.
|
||||
|
||||
assert_eq!(result.indent_spaces, expected.indent_spaces);
|
||||
|
||||
assert_eq!(
|
||||
format!("{:#?} {:#?} {:#?}", result.indent_spaces, result.translations.get("en").unwrap(), result.translations.get("hu").unwrap()),
|
||||
format!("{:#?} {:#?} {:#?}", expected.indent_spaces, expected.translations.get("en").unwrap(), expected.translations.get("hu").unwrap())
|
||||
format!("{:#?}", result.translations.get("en").unwrap()),
|
||||
format!("{:#?}", expected.translations.get("en").unwrap())
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
format!("{:#?}", result.translations.get("hu").unwrap()),
|
||||
format!("{:#?}", expected.translations.get("hu").unwrap())
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -46,6 +46,7 @@ fn it_parses_summary_to_tocitems() {
|
|||
"index.html"
|
||||
),
|
||||
translation_links: None,
|
||||
translation_id: None,
|
||||
authors: None,
|
||||
translators: None,
|
||||
description: None,
|
||||
|
@ -67,6 +68,7 @@ fn it_parses_summary_to_tocitems() {
|
|||
"README.html"
|
||||
),
|
||||
translation_links: None,
|
||||
translation_id: None,
|
||||
authors: None,
|
||||
translators: None,
|
||||
description: None,
|
||||
|
@ -92,6 +94,7 @@ fn it_parses_summary_to_tocitems() {
|
|||
"cli/cli-tool.html"
|
||||
),
|
||||
translation_links: None,
|
||||
translation_id: None,
|
||||
authors: None,
|
||||
translators: None,
|
||||
description: None,
|
||||
|
@ -111,6 +114,7 @@ fn it_parses_summary_to_tocitems() {
|
|||
"cli/init.html"
|
||||
),
|
||||
translation_links: None,
|
||||
translation_id: None,
|
||||
authors: None,
|
||||
translators: None,
|
||||
description: None,
|
||||
|
@ -137,6 +141,7 @@ fn it_parses_summary_to_tocitems() {
|
|||
"cli/build.html"
|
||||
),
|
||||
translation_links: None,
|
||||
translation_id: None,
|
||||
authors: None,
|
||||
translators: None,
|
||||
description: None,
|
||||
|
@ -163,6 +168,7 @@ fn it_parses_summary_to_tocitems() {
|
|||
"cli/watch.html"
|
||||
),
|
||||
translation_links: None,
|
||||
translation_id: None,
|
||||
authors: None,
|
||||
translators: None,
|
||||
description: None,
|
||||
|
@ -189,6 +195,7 @@ fn it_parses_summary_to_tocitems() {
|
|||
"cli/serve.html"
|
||||
),
|
||||
translation_links: None,
|
||||
translation_id: None,
|
||||
authors: None,
|
||||
translators: None,
|
||||
description: None,
|
||||
|
@ -215,6 +222,7 @@ fn it_parses_summary_to_tocitems() {
|
|||
"cli/test.html"
|
||||
),
|
||||
translation_links: None,
|
||||
translation_id: None,
|
||||
authors: None,
|
||||
translators: None,
|
||||
description: None,
|
||||
|
@ -250,6 +258,7 @@ fn it_parses_summary_to_tocitems() {
|
|||
"format/format.html"
|
||||
),
|
||||
translation_links: None,
|
||||
translation_id: None,
|
||||
authors: None,
|
||||
translators: None,
|
||||
description: None,
|
||||
|
@ -269,6 +278,7 @@ fn it_parses_summary_to_tocitems() {
|
|||
"format/summary.html"
|
||||
),
|
||||
translation_links: None,
|
||||
translation_id: None,
|
||||
authors: None,
|
||||
translators: None,
|
||||
description: None,
|
||||
|
@ -295,6 +305,7 @@ fn it_parses_summary_to_tocitems() {
|
|||
"format/config.html"
|
||||
),
|
||||
translation_links: None,
|
||||
translation_id: None,
|
||||
authors: None,
|
||||
translators: None,
|
||||
description: None,
|
||||
|
@ -321,6 +332,7 @@ fn it_parses_summary_to_tocitems() {
|
|||
"format/theme/theme.html"
|
||||
),
|
||||
translation_links: None,
|
||||
translation_id: None,
|
||||
authors: None,
|
||||
translators: None,
|
||||
description: None,
|
||||
|
@ -340,6 +352,7 @@ fn it_parses_summary_to_tocitems() {
|
|||
"format/theme/index-hbs.html"
|
||||
),
|
||||
translation_links: None,
|
||||
translation_id: None,
|
||||
authors: None,
|
||||
translators: None,
|
||||
description: None,
|
||||
|
@ -367,6 +380,7 @@ fn it_parses_summary_to_tocitems() {
|
|||
"format/theme/syntax-highlighting.html"
|
||||
),
|
||||
translation_links: None,
|
||||
translation_id: None,
|
||||
authors: None,
|
||||
translators: None,
|
||||
description: None,
|
||||
|
@ -404,6 +418,7 @@ fn it_parses_summary_to_tocitems() {
|
|||
"format/mathjax.html"
|
||||
),
|
||||
translation_links: None,
|
||||
translation_id: None,
|
||||
authors: None,
|
||||
translators: None,
|
||||
description: None,
|
||||
|
@ -430,6 +445,7 @@ fn it_parses_summary_to_tocitems() {
|
|||
"format/rust.html"
|
||||
),
|
||||
translation_links: None,
|
||||
translation_id: None,
|
||||
authors: None,
|
||||
translators: None,
|
||||
description: None,
|
||||
|
@ -465,6 +481,7 @@ fn it_parses_summary_to_tocitems() {
|
|||
"lib/lib.html"
|
||||
),
|
||||
translation_links: None,
|
||||
translation_id: None,
|
||||
authors: None,
|
||||
translators: None,
|
||||
description: None,
|
||||
|
@ -491,6 +508,7 @@ fn it_parses_summary_to_tocitems() {
|
|||
"misc/contributors.html"
|
||||
),
|
||||
translation_links: None,
|
||||
translation_id: None,
|
||||
authors: None,
|
||||
translators: None,
|
||||
description: None,
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
#[cfg(test)]
|
||||
|
||||
use book::chapter::Chapter;
|
||||
use book::toc::TocContent;
|
||||
use book::toc::{TocItem, TocContent, flat_toc};
|
||||
use parse::summary::parse_level;
|
||||
|
||||
#[test]
|
||||
fn it_should_produce_the_section_as_string() {
|
||||
|
@ -11,3 +12,68 @@ fn it_should_produce_the_section_as_string() {
|
|||
let expected = "1.9.4.".to_string();
|
||||
assert_eq!(result, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_flattens_toc() {
|
||||
let text = r#"
|
||||
# Summary
|
||||
|
||||
[Introduction](misc/introduction.md)
|
||||
|
||||
- [mdBook](README.md)
|
||||
- [Command Line Tool](cli/cli-tool.md)
|
||||
- [init](cli/init.md)
|
||||
- [build](cli/build.md)
|
||||
- [watch](cli/watch.md)
|
||||
- [serve](cli/serve.md)
|
||||
- [test](cli/test.md)
|
||||
- [Format](format/format.md)
|
||||
- [SUMMARY.md](format/summary.md)
|
||||
- [Configuration](format/config.md)
|
||||
- [Theme](format/theme/theme.md)
|
||||
- [index.hbs](format/theme/index-hbs.md)
|
||||
- [Syntax highlighting](format/theme/syntax-highlighting.md)
|
||||
- [MathJax Support](format/mathjax.md)
|
||||
- [Rust code specific features](format/rust.md)
|
||||
- [Rust Library](lib/lib.md)
|
||||
-----------
|
||||
[Contributors](misc/contributors.md)
|
||||
"#;
|
||||
|
||||
let toc = parse_level(&mut text.split('\n').collect(), 0, vec![0], true).unwrap();
|
||||
|
||||
let flat = flat_toc(&toc);
|
||||
|
||||
let result: Vec<String> = flat.iter().map(|x| {
|
||||
match *x {
|
||||
TocItem::Numbered(ref i) |
|
||||
TocItem::Unnumbered(ref i) |
|
||||
TocItem::Unlisted(ref i) => i.chapter.title.to_owned(),
|
||||
TocItem::Spacer => "spacer".to_string(),
|
||||
}
|
||||
}).collect::<Vec<String>>();
|
||||
|
||||
let expected = r#"[
|
||||
"Introduction",
|
||||
"mdBook",
|
||||
"Command Line Tool",
|
||||
"init",
|
||||
"build",
|
||||
"watch",
|
||||
"serve",
|
||||
"test",
|
||||
"Format",
|
||||
"SUMMARY.md",
|
||||
"Configuration",
|
||||
"Theme",
|
||||
"index.hbs",
|
||||
"Syntax highlighting",
|
||||
"MathJax Support",
|
||||
"Rust code specific features",
|
||||
"Rust Library",
|
||||
"spacer",
|
||||
"Contributors"
|
||||
]"#;
|
||||
|
||||
assert_eq!(format!("{:#?}", result), expected);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue