From 077df1eef69adc4e8a2b72f3caf892bbe7e1e5bf Mon Sep 17 00:00:00 2001 From: Jeroen Ooms Date: Sat, 28 Dec 2024 15:36:48 +0100 Subject: [PATCH] Basic support for litedown --- R/articles.R | 28 +++++++++++++----- inst/rmd-template/litedown.html | 50 +++++++++++++++++++++++++++++++++ inst/rmd-template/template.html | 6 +++- 3 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 inst/rmd-template/litedown.html diff --git a/R/articles.R b/R/articles.R index eb278a1..1002872 100644 --- a/R/articles.R +++ b/R/articles.R @@ -106,14 +106,24 @@ replace_rmarkdown_engine <- function(){ # Litedown (TODO: do not override slides) setHook(packageEvent("litedown", "onLoad"), function(...) { - message("Found litedown! Replacing vignette theme...") + message("Found litedown! Enabling r-universe template") old_engine <- tools::vignetteEngine('vignette', package='litedown') tools::vignetteEngine('vignette', package = 'litedown', weave = function(file, encoding, ...){ - mdfile <- file.path(tempdir(), paste0(tools::file_path_sans_ext(file), '.md')) - load_custom_output_package(file) - litedown::fuse(file, mdfile, ...) - htmlfile <- render_article(mdfile) - file.copy(htmlfile, '.', overwrite = TRUE) + meta <- buildtools:::read_yaml_font_matter(file)$options$meta + has_plugins <- setdiff(c(meta$css ,meta$js), c('@default')) + if(length(has_plugins)){ + # TODO: for now we just live this alone to avoid js/css conflicts + return(old_engine$weave(file, ...)) + } else { + mdfile <- file.path(tempdir(), paste0(tools::file_path_sans_ext(file), '.md')) + htmlfile <- file.path(tempdir(), paste0(tools::file_path_sans_ext(file), '.html')) + load_custom_output_package(file) + litedown::fuse(file, mdfile, ...) + options(litedown.html.template = template_file('litedown.html')) + on.exit(options(litedown.html.template = NULL)) + litedown::mark(mdfile, htmlfile) + file.copy(htmlfile, '.', overwrite = TRUE) + } }, tangle = old_engine$tangle, pattern = old_engine$pattern) }) @@ -125,10 +135,14 @@ replace_rmarkdown_engine <- function(){ }) } +read_yaml_font_matter <- function(rmd_file){ + rmarkdown:::output_format_from_yaml_front_matter(readLines(rmd_file, n = 100)) +} + # If a package uses a custom 'output' it may also assume functions from this package load_custom_output_package <- function(rmd_file){ try({ - name <- rmarkdown:::output_format_from_yaml_front_matter(readLines(rmd_file, n = 100))$name + name <- read_yaml_font_matter(rmd_file)$name if(length(name) && grepl("::", name, fixed = TRUE)){ pkg <- strsplit(name, '::', fixed = TRUE)[[1]][1] require(pkg, character.only = TRUE) diff --git a/inst/rmd-template/litedown.html b/inst/rmd-template/litedown.html new file mode 100644 index 0000000..6e66d7a --- /dev/null +++ b/inst/rmd-template/litedown.html @@ -0,0 +1,50 @@ + + + + + + + +$plain-title$ + + + + + + +$header-includes$ + + + +
+ + +$include-before$ + +
+$title_$ +$subtitle_$ +$author_$ +$date_$ +
+ +$abstract_$ + +
+$body$ +
+ +$include-after$ + +
+ + + + + + + + + + + diff --git a/inst/rmd-template/template.html b/inst/rmd-template/template.html index d84d402..09ba456 100644 --- a/inst/rmd-template/template.html +++ b/inst/rmd-template/template.html @@ -9,12 +9,16 @@ $if(pagetitle)$$pagetitle$$endif$ - +
+ +
$if(title)$

$title$

$endif$ +
+ $if(toc)$
$body$