Skip to content
Snippets Groups Projects
Commit 84a4372f authored by David Dorchies's avatar David Dorchies :zany_face:
Browse files

feat: latex templates open to customization

Fix #11
parent 17df5622
No related branches found
No related tags found
No related merge requests found
#' Add a new report in a fairify project
#'
#' @param name Name of the report (sub-folder name in "reports")
#' @param template The report template chosen between the folders located in
#' the "templates" folder of the fairify project
#' @param path Path of the fairify project
#'
#' @return The path of the added report
......@@ -8,7 +10,7 @@
#'
#' @inherit create_reports examples
#'
add_report <- function(name, path = ".") {
add_report <- function(name, template = "inrae", path = ".") {
if (!dir.exists(file.path(path, "reports"))) {
stop("Report structure not found, call `create_reports` first!")
}
......@@ -17,17 +19,26 @@ add_report <- function(name, path = ".") {
stop("The folder ",
destpath,
" exists already. Delete it first or choose another.")
dirtmp <- tempfile()
dir.create(dirtmp, recursive = TRUE)
b <- file.copy(
from = file.path(pkg_sys("bookdown_template")),
to = file.path(path, "reports"),
to = dirtmp,
recursive = TRUE,
copy.date = TRUE
)
if (any(!b))
stop("The copy of the template report failed")
if (any(!b)) stop("The creation of the report failed")
file.rename(
file.path(path, "reports", "bookdown_template"),
file.path(dirtmp, "bookdown_template"),
destpath
)
lapply(c("in_header", "before_body", "after_body"), function(x) {
tex_content <- c(sprintf("\\input{../../templates/%s/%s}", template, x),
"\n",
"% Add your own settings below to append or overwrite template settings")
writeLines(tex_content, file.path(destpath, sprintf("%s.tex", x)))
})
return(destpath)
}
......@@ -15,7 +15,7 @@
#' list.files(path, recursive = TRUE)
#'
#' # Add a new report
#' report_path <- add_report("my_report", path)
#' report_path <- add_report("my_report", path = path)
#' list.files(report_path)
create_reports <- function(path = ".", overwrite = FALSE, git = TRUE) {
# Copy templates
......
......@@ -9,8 +9,9 @@ bookdown::gitbook:
download: ["pdf"]
bookdown::pdf_book:
includes:
in_header: ../../templates/umr_geau/preamble.tex
after_body: ../../templates/inrae/after_body.tex
in_header: in_header.tex
before_body: before_body.tex
after_body: after_body.tex
latex_engine: pdflatex
citation_package: biblatex
keep_tex: yes
......
\input{../../templates/inrae/after_body}
......@@ -27,7 +27,7 @@ Use the function `fairify::add_report` for creating a new report.
```{r}
# Create a report
fairify::add_report("my_report", path)
fairify::add_report("my_report", path = path)
# Add a chapter
writeLines(
......
......@@ -4,11 +4,14 @@
\alias{add_report}
\title{Add a new report in a fairify project}
\usage{
add_report(name, path = ".")
add_report(name, template = "inrae", path = ".")
}
\arguments{
\item{name}{Name of the report (sub-folder name in "reports")}
\item{template}{The report template chosen between the folders located in
the "templates" folder of the fairify project}
\item{path}{Path of the fairify project}
}
\value{
......@@ -25,6 +28,6 @@ create_reports(path, git = FALSE)
list.files(path, recursive = TRUE)
# Add a new report
report_path <- add_report("my_report", path)
report_path <- add_report("my_report", path = path)
list.files(report_path)
}
......@@ -27,6 +27,6 @@ create_reports(path, git = FALSE)
list.files(path, recursive = TRUE)
# Add a new report
report_path <- add_report("my_report", path)
report_path <- add_report("my_report", path = path)
list.files(report_path)
}
......@@ -18,6 +18,12 @@ A purged \link{character} string
Purge a string from all accents and special characters
}
\examples{
purge_string("Là chaîne pl€ine dé &#/* $péciaux", "_")
\dontrun{
tmp <- tempfile()
download.file("https://raw.githubusercontent.com/bits/UTF-8-Unicode-Test-Documents/master/UTF-8_sequence_unseparated/utf8_sequence_0-0xff_assigned_printable_unseparated.txt", tmp)
txt_utf8 <- readLines(tmp)
txt_utf8
purge_string(txt_utf8)
}
}
test_that("add_report should raise error if reports does not exists", {
path <- tempfile(pattern = "dir")
dir.create(path, recursive = TRUE)
expect_error(add_report("test", path))
expect_error(add_report("test", path = path))
})
test_that("add_report should create a report", {
path <- tempfile(pattern = "dir")
dir.create(path, recursive = TRUE)
create_reports(path)
add_report("test", path)
add_report("test", path = path)
expect_true(
all(file.exists(
file.path(path,
......
......@@ -2,7 +2,7 @@ skip_on_ci()
test_that("build_site works", {
path <- helper_create_fairify()
add_report("my_report", path)
add_report("my_report", path = path)
build_site(path)
site_path <- file.path(path, "public")
expect_true(dir.exists(site_path))
......
......@@ -2,14 +2,22 @@ skip_on_ci()
test_that("render_reports works", {
path <- helper_create_fairify()
add_report("my_report", path)
templates <- list.dirs(file.path(path, "templates"), full.names = FALSE)[-1]
lapply(templates,
function(x) {
add_report(paste0("my_report_", x),
template = x,
path = path)
})
render_reports(reports_dir = file.path(path, "reports"),
output_format = "bookdown::gitbook")
report_path <- file.path(path, "public/reports/my_report")
expect_true(dir.exists(report_path))
expect_true(file.exists(file.path(report_path, "index.html")))
render_reports(reports_dir = file.path(path, "reports"),
output_format = "bookdown::pdf_book")
expect_true(file.exists(file.path(path, "public/reports/my_report/report.pdf")))
lapply(paste0("my_report_", templates), function(x) {
report_path <- file.path(path, "public/reports", x)
expect_true(dir.exists(report_path))
expect_true(file.exists(file.path(report_path, "index.html")))
expect_true(file.exists(file.path(report_path, "report.pdf")))
})
unlink(path, recursive = TRUE)
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment