From d93e7c45e869ecb974eab92a3ddc906229651180 Mon Sep 17 00:00:00 2001 From: froggleston Date: Wed, 22 May 2024 14:02:47 +0100 Subject: [PATCH 1/3] Add support for configurable analytics --- DESCRIPTION | 2 +- NEWS.md | 7 ++++++ R/build_html.R | 8 +++++++ R/utils-metadata.R | 2 ++ R/utils-tracker.R | 34 +++++++++++++++++++++++++++++ R/utils-xml.R | 2 ++ R/utils-yaml.R | 2 ++ tests/testthat/test-utils-tracker.R | 16 ++++++++++++++ 8 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 R/utils-tracker.R create mode 100644 tests/testthat/test-utils-tracker.R diff --git a/DESCRIPTION b/DESCRIPTION index 69c7ff2af..17e09e497 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: sandpaper Title: Create and Curate Carpentries Lessons -Version: 0.16.4 +Version: 0.16.5.9000 Authors@R: c( person(given = "Zhian N.", family = "Kamvar", diff --git a/NEWS.md b/NEWS.md index 1db105aa5..24a737a31 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,13 @@ * Fix for empty divs when checking for headers (reported: @dmgatti, #581; fixed @froggleston) +* Add support for including the Carpentries matomo + tracker, a custom user-supplied tracker script, or + no tracking + (reported: @tbyhdgs, @fiveop https://github.com/carpentries/varnish/issues/37, + @zkamvar https://github.com/carpentries/sandpaper/issues/438, + implemented: @froggleston + ) # sandpaper 0.16.4 (2024-04-10) diff --git a/R/build_html.R b/R/build_html.R index b007bed21..56cde4645 100644 --- a/R/build_html.R +++ b/R/build_html.R @@ -64,6 +64,10 @@ build_html <- function(template = "chapter", pkg, nodes, global_data, path_md, q global_data$instructor$set("json", fill_metadata_template(meta)) global_data$instructor$set("translate", translated) global_data$instructor$set("citation", meta$get()$citation) + + # add tracker script + global_data$instructor$set("analytics", processTracker(meta$get()$analytics)) + modified <- pkgdown::render_page(pkg, template, data = global_data$instructor$get(), @@ -80,6 +84,10 @@ build_html <- function(template = "chapter", pkg, nodes, global_data, path_md, q meta$set("url", paste0(base_url, this_page)) global_data$learner$set("json", fill_metadata_template(meta)) global_data$learner$set("citation", meta$get()$citation) + + # add tracker script + global_data$learner$set("analytics", processTracker(meta$get()$analytics)) + pkgdown::render_page(pkg, template, data = global_data$learner$get(), diff --git a/R/utils-metadata.R b/R/utils-metadata.R index 8ab718ef4..dd5627a7e 100644 --- a/R/utils-metadata.R +++ b/R/utils-metadata.R @@ -48,6 +48,8 @@ initialise_metadata <- function(path = ".") { this_metadata$set(c("date", "modified"), format(Sys.Date(), "%F")) this_metadata$set(c("date", "published"), format(Sys.Date(), "%F")) this_metadata$set("citation", path_citation(path)) + + this_metadata$set("analytics", cfg$analytics) } diff --git a/R/utils-tracker.R b/R/utils-tracker.R new file mode 100644 index 000000000..ac47512ab --- /dev/null +++ b/R/utils-tracker.R @@ -0,0 +1,34 @@ +processTracker <- function(string) { + string <- as.character(string) + + # default to whatever the user supplies + analytics_str <- string + + if (identical(string, "carpentries")) { + analytics_str <- ' + + + + ' + } else if (identical(string, "")) { + analytics_str <- "" + } + + return(analytics_str) +} diff --git a/R/utils-xml.R b/R/utils-xml.R index 4280f45c2..799be44c9 100644 --- a/R/utils-xml.R +++ b/R/utils-xml.R @@ -171,6 +171,7 @@ translate_overview <- function(nodes = NULL) { # @param translations a named vector of translated strings whose names are the # strings in English xml_text_translate <- function(nodes, translations) { + # removes whitespace in order to translate, but need to add it back for nested tags txt <- xml2::xml_text(nodes, trim = TRUE) xml2::xml_set_text(nodes, apply_translations(txt, translations)) return(invisible(nodes)) @@ -204,6 +205,7 @@ fix_callouts <- function(nodes = NULL) { # https://github.com/carpentries/sandpaper/issues/556 h3_text <- xml2::xml_find_all(h3, ".//text()") xml_text_translate(h3_text, translations) + xml2::xml_set_attr(h3, "class", "callout-title") inner_div <- xml2::xml_parent(h3) # remove the "section level3 callout-title" attrs diff --git a/R/utils-yaml.R b/R/utils-yaml.R index 8ab99ca1c..d76a99031 100644 --- a/R/utils-yaml.R +++ b/R/utils-yaml.R @@ -193,6 +193,8 @@ create_pkgdown_yaml <- function(path) { beta = usr$life_cycle == "beta", stable = usr$life_cycle == "stable", doi = doi, + # Enable tracking? + analytics = if (is.null(usr$analytics)) NULL else (siQuote(usr$analytics)), NULL ) ) diff --git a/tests/testthat/test-utils-tracker.R b/tests/testthat/test-utils-tracker.R new file mode 100644 index 000000000..649a278d6 --- /dev/null +++ b/tests/testthat/test-utils-tracker.R @@ -0,0 +1,16 @@ +test_that("analytics carpentries tracker code generation works", { + yaml <- " + analytics: carpentries + " + + expectation <- " + + " + pgy <- politely_get_yaml(yaml) + YML <- yaml::yaml.load(pgy) + + tracker_str <- processTracker(siQuote(YML$analytics)) + + expect_true(length(tracker_str) > 0) + expect_false(identical(tracker_str, "carpentries")) +}) \ No newline at end of file From 8c129ae378a7cc7327cc14d0cd116a8c949c3e70 Mon Sep 17 00:00:00 2001 From: froggleston Date: Wed, 22 May 2024 14:12:47 +0100 Subject: [PATCH 2/3] Remove expectation from test --- tests/testthat/test-utils-tracker.R | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/testthat/test-utils-tracker.R b/tests/testthat/test-utils-tracker.R index 649a278d6..ce3d60ec4 100644 --- a/tests/testthat/test-utils-tracker.R +++ b/tests/testthat/test-utils-tracker.R @@ -3,9 +3,6 @@ test_that("analytics carpentries tracker code generation works", { analytics: carpentries " - expectation <- " - - " pgy <- politely_get_yaml(yaml) YML <- yaml::yaml.load(pgy) From bcb2d49ba1b784e558d7cc18158ac252473b7bf3 Mon Sep 17 00:00:00 2001 From: froggleston Date: Wed, 22 May 2024 14:25:29 +0100 Subject: [PATCH 3/3] Attempt to fix tracker test --- tests/testthat/test-utils-tracker.R | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/testthat/test-utils-tracker.R b/tests/testthat/test-utils-tracker.R index ce3d60ec4..fc833db49 100644 --- a/tests/testthat/test-utils-tracker.R +++ b/tests/testthat/test-utils-tracker.R @@ -1,13 +1,10 @@ test_that("analytics carpentries tracker code generation works", { - yaml <- " - analytics: carpentries - " + tracker_yaml <- "analytics: carpentries" - pgy <- politely_get_yaml(yaml) - YML <- yaml::yaml.load(pgy) + YML <- yaml::yaml.load(tracker_yaml) tracker_str <- processTracker(siQuote(YML$analytics)) expect_true(length(tracker_str) > 0) expect_false(identical(tracker_str, "carpentries")) -}) \ No newline at end of file +})