more simple language declaration in book.toml
This commit is contained in:
parent
cb861a7354
commit
8441bb99d2
|
@ -109,7 +109,9 @@ impl BookConfig {
|
|||
/// name = "Marcus Aurelius Antoninus"
|
||||
/// ```
|
||||
///
|
||||
pub fn parse_from_btreemap(&mut self, config: &BTreeMap<String, toml::Value>) -> &mut Self {
|
||||
pub fn parse_from_btreemap(&mut self,
|
||||
default_language_code: String,
|
||||
config: &BTreeMap<String, toml::Value>) -> &mut Self {
|
||||
|
||||
// Paths
|
||||
|
||||
|
@ -134,6 +136,8 @@ impl BookConfig {
|
|||
.collect::<Vec<Author>>()
|
||||
};
|
||||
|
||||
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<toml::Table> for Author {
|
|||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Language {
|
||||
pub name: String,
|
||||
pub code: String,
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
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<toml::Table> 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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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" }
|
||||
|
|
|
@ -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("<html lang=\"en\">"));
|
||||
assert!(s.contains("<h1>The Pool of Tears</h1>"));
|
||||
assert!(s.contains("<base href=\"../\">"));
|
||||
assert!(s.contains("li><a href=\"en/tears.html\" class=\"active\"><strong>2.</strong> The Pool of Tears</a></li>"));
|
||||
|
@ -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("<html lang=\"fr\">"));
|
||||
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("tears.html");
|
||||
s = utils::fs::file_to_string(&chapter_path).unwrap();
|
||||
assert!(s.contains("<html lang=\"hu\">"));
|
||||
assert!(s.contains("<h1>Könnytó</h1>"));
|
||||
assert!(s.contains("<base href=\"../\">"));
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue