From 4a634f08da14826ed9b0bbea26278977387a6c5e Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Tue, 16 May 2017 13:05:21 +0800 Subject: [PATCH 1/3] Updated google analytics to index.hbs and hbs_renderer.rs --- src/renderer/html_handlebars/hbs_renderer.rs | 3 +++ src/theme/index.hbs | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 4fac6869..1d84c73d 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -193,6 +193,9 @@ fn make_data(book: &MDBook) -> Result data.insert("livereload".to_owned(), json!(livereload)); } + // Add google analytics tag + data.insert("google_analytics".to_owned(), json!("INSERT_GA_ID_HERE")); + let mut chapters = vec![]; for item in book.iter() { diff --git a/src/theme/index.hbs b/src/theme/index.hbs index a99752da..b3985fbc 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -111,6 +111,19 @@ {{{livereload}}} + {{#if google_analytics}} + + {{/if}} + + From ada1f29b34d049be85407ee6a98e4f2798e63d7c Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Tue, 16 May 2017 13:15:04 +0800 Subject: [PATCH 2/3] Added a google_analytics field to BookConfig This commit: - Adds an Option 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 --- src/book/bookconfig.rs | 7 +++++++ src/book/bookconfig_test.rs | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/book/bookconfig.rs b/src/book/bookconfig.rs index 53544eb0..9b1b33a5 100644 --- a/src/book/bookconfig.rs +++ b/src/book/bookconfig.rs @@ -21,6 +21,7 @@ pub struct BookConfig { pub indent_spaces: i32, multilingual: bool, + pub google_analytics: Option, } impl BookConfig { @@ -37,6 +38,7 @@ impl BookConfig { indent_spaces: 4, // indentation used for SUMMARY.md multilingual: false, + google_analytics: None, } } @@ -154,6 +156,11 @@ impl BookConfig { 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 } diff --git a/src/book/bookconfig_test.rs b/src/book/bookconfig_test.rs index 61cfa6a9..b3a24385 100644 --- a/src/book/bookconfig_test.rs +++ b/src/book/bookconfig_test.rs @@ -347,3 +347,25 @@ fn it_parses_json_arrays_to_toml() { 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)); +} From 94dce4f796e8152aa0ffa9835034c170f9956b2c Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Tue, 16 May 2017 13:24:05 +0800 Subject: [PATCH 3/3] Added google_analytics so it can be inserted into handlebars --- src/book/mod.rs | 3 +++ src/renderer/html_handlebars/hbs_renderer.rs | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 85d20a79..5e413c87 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -36,6 +36,8 @@ pub struct MDBook { /// Should `mdbook build` create files referenced from SUMMARY.md if they /// don't exist pub create_missing: bool, + + pub google_analytics: Option, } impl MDBook { @@ -84,6 +86,7 @@ impl MDBook { livereload: None, create_missing: true, + google_analytics: None, } } diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 1d84c73d..e9250a3d 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -194,7 +194,9 @@ fn make_data(book: &MDBook) -> Result } // Add google analytics tag - data.insert("google_analytics".to_owned(), json!("INSERT_GA_ID_HERE")); + if let Some(ref ga) = book.google_analytics { + data.insert("google_analytics".to_owned(), json!(ga)); + } let mut chapters = vec![];