У меня все получилось. Мое чтение говорит о том, что использование lapply
или других в семействе apply
является более вычислительно эффективным в R, но я взломал гайку с помощью for
циклов ранее.
В сценарии build.R
:
#Ensure working directory is the site root
library(R.utils)
library(xfun)
#Move everything to a safe space
copyDirectory(from="content", to="working", recursive=TRUE)
#Draws the glossary from a separate .md file, and separates it out into terms and definitions
glossary <- lapply(strsplit(readLines(con = "content/glossary.md", warn = FALSE), "## |##"), function(x){x[!x ==""]})
glossary <- glossary[lapply(glossary, length)>0] #Tidy up
terms <- glossary[seq_along(glossary) %% 2 > 0] #Separates the terms
defs <- glossary[seq_along(glossary) %% 2 == 0] #And the defs. terms[1] corresponds to defs[1].
#Get files for site
files <- list.files(path="working", pattern = "*.md|*.rmd|*.rmarkdown", recursive = TRUE)
#Replacing bit
setwd("working")
for (file in seq_along(files)) {
for (term in seq_along(terms)) {
gsub_file(files[file], sprintf("%s", terms[term]), sprintf("<span title=\"%s\" class=\"glossary\">%s</span>", defs[term], terms[term]), fixed = TRUE)
}
}
setwd("..")
#Build the site from the new glossaried markdown
blogdown::build_dir('working')