Skip to content

Commit

Permalink
Basic support for litedown
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroen committed Dec 29, 2024
1 parent 1e7d58d commit d7a1747
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
28 changes: 21 additions & 7 deletions R/articles.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 <- read_yaml_font_matter(file)$options$meta
has_plugins <- setdiff('@default', c(meta$css ,meta$js))
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)
})

Expand All @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions inst/rmd-template/litedown.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="generator" content="$generator$">

<title>$plain-title$</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css" data-external="1">
<link rel="stylesheet" href="https://r-universe.dev/static/article-styles.css?nocache=1" data-external="1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@xiee/[email protected]/css/prism-xcode.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css">

$header-includes$
</head>

<body>
<div class="container" id="topcontainer">
<script>if (window.self != window.top) try {document.getElementById('topcontainer').className = "container-fluid article-in-iframe"; } catch(e) {}</script>

$include-before$

<div class="frontmatter">
$title_$
$subtitle_$
$author_$
$date_$
</div>

$abstract_$

<div class="$body-class$">
$body$
</div>

$include-after$

</div>

<script src="https://r-universe.dev/static/article.js" data-external="1"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/components/prism-core.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/plugins/autoloader/prism-autoloader.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/combine/npm/[email protected]/dist/katex.min.js,npm/[email protected]/dist/contrib/auto-render.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/@xiee/[email protected]/js/render-katex.min.js" defer></script>

</body>

</html>
6 changes: 5 additions & 1 deletion inst/rmd-template/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
$if(pagetitle)$<title>$pagetitle$</title>$endif$
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css" data-external="1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" data-external="1">
<link rel="stylesheet" href="https://r-universe.dev/static/article-styles.css" data-external="1">
<link rel="stylesheet" href="https://r-universe.dev/static/article-styles.css?nocache=1" data-external="1">
</head>
<body>
<div class="container" id="topcontainer">
<script>if (window.self != window.top) try {document.getElementById('topcontainer').className = "container-fluid article-in-iframe"; } catch(e) {}</script>

<div class="frontmatter">
$if(title)$<h1 class="title">$title$</h1>$endif$
</div>

$if(toc)$
<div class="row">
<div class="col-xl-10">$body$</div>
Expand Down

0 comments on commit d7a1747

Please sign in to comment.