Added a google_analytics field to BookConfig

This commit:
- Adds an Option<String> field to the BookConfig which should
  contain your google analytics ID
- Allows the google analytics ID to be extracted from the config
  file (key is google_analytics_id)
- Adds a test to make sure the field is populated from a config
  file correctly
This commit is contained in:
Michael-F-Bryan 2017-05-16 13:15:04 +08:00
parent 4a634f08da
commit ada1f29b34
2 changed files with 29 additions and 0 deletions

View File

@ -21,6 +21,7 @@ pub struct BookConfig {
pub indent_spaces: i32, pub indent_spaces: i32,
multilingual: bool, multilingual: bool,
pub google_analytics: Option<String>,
} }
impl BookConfig { impl BookConfig {
@ -37,6 +38,7 @@ impl BookConfig {
indent_spaces: 4, // indentation used for SUMMARY.md indent_spaces: 4, // indentation used for SUMMARY.md
multilingual: false, multilingual: false,
google_analytics: None,
} }
} }
@ -154,6 +156,11 @@ impl BookConfig {
self.set_theme_path(&theme_path); self.set_theme_path(&theme_path);
} }
// Google analytics code
if let Some(id) = config.get("google_analytics_id") {
self.google_analytics = Some(id.to_string().replace("\"", ""));
}
self self
} }

View File

@ -347,3 +347,25 @@ fn it_parses_json_arrays_to_toml() {
assert_eq!(format!("{:#?}", result), expected); assert_eq!(format!("{:#?}", result), expected);
} }
#[test]
fn it_fetches_google_analytics_from_toml() {
let text = r#"
title = "mdBook Documentation"
description = "Create book from markdown files. Like Gitbook but implemented in Rust"
author = "Mathieu David"
google_analytics_id = "123456"
"#;
let mut config = BookConfig::new(Path::new("."));
config.parse_from_toml_string(&text.to_string());
let mut expected = BookConfig::new(Path::new("."));
expected.title = "mdBook Documentation".to_string();
expected.author = "Mathieu David".to_string();
expected.description = "Create book from markdown files. Like Gitbook but implemented in Rust".to_string();
expected.google_analytics = Some("123456".to_string());
assert_eq!(format!("{:#?}", config), format!("{:#?}", expected));
}