forked from rust-lang/mdBook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove 'default' property on languages, use book.language instead
- Loading branch information
Showing
9 changed files
with
89 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -255,7 +255,7 @@ impl Config { | |
|
||
/// Gets the language configured for a book. | ||
pub fn get_language<I: AsRef<str>>(&self, index: Option<I>) -> Result<Option<String>> { | ||
match self.language.default_language() { | ||
match self.default_language() { | ||
// Languages have been specified, assume directory structure with | ||
// language subfolders. | ||
Some(ref default) => match index { | ||
|
@@ -342,7 +342,7 @@ impl Config { | |
/// missing in a localization, any links to them will gracefully degrade to | ||
/// the files that exist in this directory. | ||
pub fn get_fallback_src_path(&self) -> PathBuf { | ||
match self.language.default_language() { | ||
match self.default_language() { | ||
// Languages have been specified, assume directory structure with | ||
// language subfolders. | ||
Some(default) => { | ||
|
@@ -358,6 +358,31 @@ impl Config { | |
} | ||
} | ||
|
||
/// If true, mdBook should assume there are subdirectories under src/ | ||
/// corresponding to the localizations in the config. If false, src/ is a | ||
/// single directory containing the summary file and the rest. | ||
pub fn has_localized_dir_structure(&self) -> bool { | ||
!self.language.0.is_empty() | ||
} | ||
|
||
/// Obtains the default language for this config. | ||
pub fn default_language(&self) -> Option<String> { | ||
if self.has_localized_dir_structure() { | ||
let language_ident = self | ||
.book | ||
.language | ||
.clone() | ||
.expect("Config has [language] table, but `book.language` not was declared"); | ||
self.language.0.get(&language_ident).expect(&format!( | ||
"Expected [language.{}] to be declared in book.toml", | ||
language_ident | ||
)); | ||
Some(language_ident) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn from_legacy(mut table: Value) -> Config { | ||
let mut cfg = Config::default(); | ||
|
||
|
@@ -455,12 +480,28 @@ impl<'de> Deserialize<'de> for Config { | |
|
||
if !language.0.is_empty() { | ||
let default_languages = language.0.iter().filter(|(_, lang)| lang.default).count(); | ||
|
||
if default_languages != 1 { | ||
if default_languages != 1 { | ||
return Err(D::Error::custom( | ||
"If [languages] table is specified, exactly one entry must be set as 'default'", | ||
"If the [language] table is specified, then `book.language` must be declared", | ||
)); | ||
} | ||
let language_ident = book.language.clone().unwrap(); | ||
if language.0.get(&language_ident).is_none() { | ||
use serde::de::Error; | ||
return Err(D::Error::custom(format!( | ||
"Expected [language.{}] to be declared in book.toml", | ||
language_ident | ||
))); | ||
} | ||
for (ident, language) in language.0.iter() { | ||
if language.name.is_empty() { | ||
use serde::de::Error; | ||
return Err(D::Error::custom(format!( | ||
"`name` property for [language.{}] must be non-empty", | ||
ident | ||
))); | ||
} | ||
} | ||
} | ||
|
||
Ok(Config { | ||
|
@@ -492,7 +533,8 @@ impl Serialize for Config { | |
} | ||
|
||
if !self.language.0.is_empty() { | ||
let language_config = Value::try_from(&self.language).expect("should always be serializable"); | ||
let language_config = | ||
Value::try_from(&self.language).expect("should always be serializable"); | ||
table.insert("language", language_config); | ||
} | ||
|
||
|
@@ -543,9 +585,7 @@ pub struct BookConfig { | |
pub description: Option<String>, | ||
/// Location of the book source relative to the book's root directory. | ||
pub src: PathBuf, | ||
/// Does this book support more than one language? (Deprecated.) | ||
pub multilingual: bool, | ||
/// The main language of the book. (Deprecated.) | ||
/// The main language of the book. | ||
pub language: Option<String>, | ||
} | ||
|
||
|
@@ -556,7 +596,6 @@ impl Default for BookConfig { | |
authors: Vec::new(), | ||
description: None, | ||
src: PathBuf::from("src"), | ||
multilingual: true, | ||
language: Some(String::from("en")), | ||
} | ||
} | ||
|
@@ -830,9 +869,6 @@ pub struct LanguageConfig(pub HashMap<String, Language>); | |
pub struct Language { | ||
/// Human-readable name of the language. | ||
pub name: String, | ||
/// If true, this language is the default. There must be exactly one default | ||
/// language in the config. | ||
pub default: bool, | ||
/// Localized title of the book. | ||
pub title: Option<String>, | ||
/// The authors of the translation. | ||
|
@@ -841,22 +877,7 @@ pub struct Language { | |
pub description: Option<String>, | ||
} | ||
|
||
impl LanguageConfig { | ||
/// If true, mdBook should assume there are subdirectories under src/ | ||
/// corresponding to the localizations in the config. If false, src/ is a | ||
/// single directory containing the summary file and the rest. | ||
pub fn has_localized_dir_structure(&self) -> bool { | ||
self.default_language().is_some() | ||
} | ||
|
||
/// Returns the default language specified in the config. | ||
pub fn default_language(&self) -> Option<&String> { | ||
self.0 | ||
.iter() | ||
.find(|(_, lang)| lang.default) | ||
.map(|(lang_ident, _)| lang_ident) | ||
} | ||
} | ||
impl LanguageConfig {} | ||
|
||
/// Allows you to "update" any arbitrary field in a struct by round-tripping via | ||
/// a `toml::Value`. | ||
|
@@ -923,7 +944,6 @@ mod tests { | |
[language.en] | ||
name = "English" | ||
default = true | ||
[language.ja] | ||
name = "日本語" | ||
|
@@ -940,7 +960,6 @@ mod tests { | |
title: Some(String::from("Some Book")), | ||
authors: vec![String::from("Michael-F-Bryan <[email protected]>")], | ||
description: Some(String::from("A completely useless book")), | ||
multilingual: true, | ||
src: PathBuf::from("source"), | ||
language: Some(String::from("ja")), | ||
}; | ||
|
@@ -981,7 +1000,6 @@ mod tests { | |
String::from("en"), | ||
Language { | ||
name: String::from("English"), | ||
default: true, | ||
title: None, | ||
description: None, | ||
authors: None, | ||
|
@@ -991,7 +1009,6 @@ mod tests { | |
String::from("ja"), | ||
Language { | ||
name: String::from("日本語"), | ||
default: false, | ||
title: Some(String::from("なんかの本")), | ||
description: Some(String::from("何の役にも立たない本")), | ||
authors: Some(vec![String::from("Ruin0x11")]), | ||
|
@@ -1354,26 +1371,23 @@ mod tests { | |
|
||
#[test] | ||
#[should_panic(expected = "Invalid configuration file")] | ||
fn validate_default_language() { | ||
fn validate_config_default_language_must_exist_in_languages_table() { | ||
let src = r#" | ||
[language.en] | ||
name = "English" | ||
[language.ja] | ||
name = "日本語" | ||
"#; | ||
|
||
Config::from_str(src).unwrap(); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "Invalid configuration file")] | ||
fn validate_default_language_2() { | ||
fn validate_language_config_must_have_name() { | ||
let src = r#" | ||
[language.en] | ||
name = "English" | ||
default = true | ||
[book] | ||
language = "en" | ||
[language.fr] | ||
name = "Français" | ||
default = true | ||
[language.en] | ||
"#; | ||
|
||
Config::from_str(src).unwrap(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters