From 8441bb99d2980e7e37d5140cf1e43d42d1d34742 Mon Sep 17 00:00:00 2001 From: Gambhiro Date: Wed, 18 Jan 2017 14:58:38 +0000 Subject: [PATCH] more simple language declaration in book.toml --- src/book/bookconfig.rs | 37 +++++++++++++++---- src/book/mod.rs | 8 +++- src/renderer/html_handlebars/hbs_renderer.rs | 2 +- src/tests/book-wonderland-multilang/book.toml | 9 +++-- src/tests/hbs_renderer_multilang.rs | 3 ++ src/tests/mdbook_test.rs | 10 +++-- 6 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/book/bookconfig.rs b/src/book/bookconfig.rs index 158b5f38..984a8eee 100644 --- a/src/book/bookconfig.rs +++ b/src/book/bookconfig.rs @@ -109,7 +109,9 @@ impl BookConfig { /// name = "Marcus Aurelius Antoninus" /// ``` /// - pub fn parse_from_btreemap(&mut self, config: &BTreeMap) -> &mut Self { + pub fn parse_from_btreemap(&mut self, + default_language_code: String, + config: &BTreeMap) -> &mut Self { // Paths @@ -134,6 +136,8 @@ impl BookConfig { .collect::>() }; + self.language = Language::new(&default_language_code); + if let Some(a) = config.get("title") { self.title = a.to_string().replace("\"", ""); } @@ -152,6 +156,14 @@ impl BookConfig { } } + if let Some(a) = config.get("language_code") { + self.language.code = a.to_string().replace("\"", ""); + } + + if let Some(a) = config.get("language_name") { + self.language.name = Some(a.to_string().replace("\"", "")); + } + // Author name as a hash key. if let Some(a) = config.get("author") { if let Some(b) = a.as_str() { @@ -286,24 +298,31 @@ impl From for Author { #[derive(Debug, Clone)] pub struct Language { - pub name: String, pub code: String, + pub name: Option, } impl Default for Language { fn default() -> Self { Language { - name: String::from("English"), code: String::from("en"), + name: Some(String::from("English")), } } } impl Language { - pub fn new(name: &str, code: &str) -> Language { + pub fn new(code: &str) -> Language { Language{ - name: name.to_string(), code: code.to_string(), + name: None, + } + } + + pub fn new_with_name(code: &str, name: &str) -> Language { + Language{ + code: code.to_string(), + name: Some(name.to_string()), } } } @@ -311,12 +330,14 @@ impl Language { impl From for Language { fn from(data: toml::Table) -> Language { let mut language = Language::default(); - if let Some(x) = data.get("name") { - language.name = x.to_string().replace("\"", ""); - } if let Some(x) = data.get("code") { language.code = x.to_string().replace("\"", ""); } + if let Some(x) = data.get("name") { + language.name = Some(x.to_string().replace("\"", "")); + } else { + language.name = None; + } language } } diff --git a/src/book/mod.rs b/src/book/mod.rs index a0a244f4..c216c35e 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -321,7 +321,10 @@ impl MDBook { book.config.dest = book.config.dest.join(key); } book.config.is_multilang = is_multilang; - book.config.parse_from_btreemap(&d); + book.config.parse_from_btreemap(key.to_owned(), &d); + // the language code and translation key must agree + // even after parsing the user's settings + book.config.language.code = key.to_owned(); if book.config.is_main_book { has_main_book_already = true; } @@ -379,7 +382,8 @@ impl MDBook { } else { let mut book = Book::new(&self.project_root); - book.config.parse_from_btreemap(&config); + // take "en" as default code, will override if user sets it + book.config.parse_from_btreemap("en".to_owned(), &config); let key = book.config.language.code.clone(); self.translations.insert(key, book); } diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 94359c3a..e3fc7de9 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -356,7 +356,7 @@ fn make_data(book: &Book, // Book data - data.insert("language".to_owned(), "en".to_json()); + data.insert("language".to_owned(), book.config.language.code.to_json()); data.insert("page-title".to_owned(), format!("{} - {}", chapter.title, book.config.title).to_json()); data.insert("chapter-title".to_owned(), chapter.title.to_json()); data.insert("description".to_owned(), book.config.description.to_json()); diff --git a/src/tests/book-wonderland-multilang/book.toml b/src/tests/book-wonderland-multilang/book.toml index be3214b9..032739a1 100644 --- a/src/tests/book-wonderland-multilang/book.toml +++ b/src/tests/book-wonderland-multilang/book.toml @@ -1,16 +1,19 @@ # Source: https://en.wikisource.org/wiki/Alice%27s_Adventures_in_Wonderland_(1866)" +# The language code will be the translation key. + [[translations.en]] title = "Alice's Adventures in Wonderland" author = "Lewis Carroll" -language = { name = "English", code = "en" } [[translations.fr]] title = "Alice au pays des merveilles" author = "Lewis Carroll" -language = { name = "Français", code = "fr" } + +# Optionally you can provide the language name. + +language_name = "Français" [[translations.hu]] title = "Alice Csodaországban" author = "Lewis Carroll" -language = { name = "Magyar", code = "hu" } diff --git a/src/tests/hbs_renderer_multilang.rs b/src/tests/hbs_renderer_multilang.rs index a9d58e28..cdd7744e 100644 --- a/src/tests/hbs_renderer_multilang.rs +++ b/src/tests/hbs_renderer_multilang.rs @@ -35,6 +35,7 @@ fn it_renders_multilanguage_book() { chapter_path = book_path.join("tears.html"); s = utils::fs::file_to_string(&chapter_path).unwrap(); + assert!(s.contains("")); assert!(s.contains("

The Pool of Tears

")); assert!(s.contains("")); assert!(s.contains("li>2. The Pool of Tears")); @@ -42,12 +43,14 @@ fn it_renders_multilanguage_book() { book_path = proj.translations.get("fr").unwrap().config.get_dest(); chapter_path = book_path.join("tears.html"); s = utils::fs::file_to_string(&chapter_path).unwrap(); + assert!(s.contains("")); assert!(s.contains("

La mare aux larmes

")); assert!(s.contains("")); book_path = proj.translations.get("hu").unwrap().config.get_dest(); chapter_path = book_path.join("tears.html"); s = utils::fs::file_to_string(&chapter_path).unwrap(); + assert!(s.contains("")); assert!(s.contains("

Könnytó

")); assert!(s.contains("")); diff --git a/src/tests/mdbook_test.rs b/src/tests/mdbook_test.rs index b5c6076a..18e2be31 100644 --- a/src/tests/mdbook_test.rs +++ b/src/tests/mdbook_test.rs @@ -10,8 +10,7 @@ use serde_json; use utils; use book::MDBook; use book::book::Book; -use book::bookconfig::BookConfig; -use book::bookconfig::Author; +use book::bookconfig::{BookConfig, Author, Language}; use book::chapter::Chapter; use book::toc::{TocItem, TocContent}; @@ -36,6 +35,7 @@ fn it_parses_simple_json_config() { book.config.title = "mdBook Documentation".to_string(); book.config.description = Some("Create books from markdown files.".to_string()); book.config.authors = vec![Author::new("Mathieu David")]; + book.config.language = Language::new("en"); expected.translations.insert("en".to_string(), book); } @@ -63,6 +63,7 @@ author = "Mathieu David" book.config.title = "mdBook Documentation".to_string(); book.config.description = Some("Create books from markdown files.".to_string()); book.config.authors = vec![Author::new("Mathieu David")]; + book.config.language = Language::new("en"); expected.translations.insert("en".to_string(), book); } @@ -92,6 +93,7 @@ name = "Mathieu David" book.config.title = "mdBook Documentation".to_string(); book.config.description = Some("Create books from markdown files.".to_string()); book.config.authors = vec![Author::new("Mathieu David")]; + book.config.language = Language::new("en"); expected.translations.insert("en".to_string(), book); } @@ -144,6 +146,7 @@ name = "Kosztolányi Dezső" conf.dest = expected.get_project_root().join("book").join("en"); conf.is_multilang = true; conf.is_main_book = true; + conf.language = Language::new("en"); let mut book = Book::default(); book.config = conf; @@ -160,6 +163,7 @@ name = "Kosztolányi Dezső" conf.dest = expected.get_project_root().join("book").join("hu"); conf.is_multilang = true; conf.is_main_book = false; + conf.language = Language::new("hu"); let mut book = Book::default(); book.config = conf; @@ -219,7 +223,7 @@ name = "Kosztolányi Dezső" book.config.title = "Alice Csodaországban".to_string(); book.config.authors = vec![Author::new("Lewis Carroll")]; book.config.translators = Some(vec![Author::new("Kosztolányi Dezső")]); - book.config.language.name = "Hungarian".to_string(); + book.config.language.name = Some("Hungarian".to_string()); book.config.language.code = "hu".to_string(); expected.translations.insert("hu".to_string(), book);