From 43daa194508726230ad4b81eebb4cda318cb3268 Mon Sep 17 00:00:00 2001
From: David Dorchies <david.dorchies@inrae.fr>
Date: Tue, 26 Mar 2024 18:44:10 +0100
Subject: [PATCH 1/5] feat: add_article

Refs #29
---
 DESCRIPTION                       |  2 ++
 NAMESPACE                         |  1 +
 R/add_article.R                   | 44 +++++++++++++++++++++++++++++++
 R/add_report.R                    |  6 ++---
 R/create_reports.R                |  9 ++++++-
 R/list_reports.R                  |  9 ++++++-
 R/render_report.R                 | 23 ++++++++++++----
 man/add_article.Rd                | 38 ++++++++++++++++++++++++++
 man/create_reports.Rd             |  2 +-
 tests/testthat/test-add_article.R | 23 ++++++++++++++++
 10 files changed, 145 insertions(+), 12 deletions(-)
 create mode 100644 R/add_article.R
 create mode 100644 man/add_article.Rd
 create mode 100644 tests/testthat/test-add_article.R

diff --git a/DESCRIPTION b/DESCRIPTION
index ea0976f..f4e591a 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -23,6 +23,7 @@ Imports:
     pkgload,
     readr,
     rlang,
+    rticles,
     stringr,
     this.path,
     urltools,
@@ -31,6 +32,7 @@ Imports:
     yaml,
     zlib
 Suggests:
+    rmarkdown,
     testthat (>= 3.0.0),
     tinytex
 Config/testthat/edition: 3
diff --git a/NAMESPACE b/NAMESPACE
index ba18baf..43ac795 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -1,6 +1,7 @@
 # Generated by roxygen2: do not edit by hand
 
 export("%>%")
+export(add_article)
 export(add_gitignore)
 export(add_report)
 export(build_site)
diff --git a/R/add_article.R b/R/add_article.R
new file mode 100644
index 0000000..e17d3ce
--- /dev/null
+++ b/R/add_article.R
@@ -0,0 +1,44 @@
+#' Add a new article in a fairify project
+#'
+#' This function prepares an article with a template provided by the **rticles**
+#' package.
+#'
+#' @details
+#' This function uses [rmardown::draft] for creating the new document based on
+#' a template, so its uses is theoritically not limited to the templates
+#' provided by the **rticles** package.
+#'
+#' @inheritParams add_report
+#' @inheritParams rmarkdown::draft
+#'
+#' @return Used for side effect.
+#' @export
+#'
+#' @examples
+add_article <- function(name,
+                        template,
+                        package = "rticles",
+                        path = pkgload::pkg_path(),
+                        edit = FALSE) {
+  stopifnot(template %in% rticles::journals())
+
+  # reports folder handling
+  check_reports_folder(path)
+
+  destpath <- file.path(path, "reports", name)
+  if (dir.exists(destpath))
+    stop("The folder ",
+         destpath,
+         " exists already. Delete it first or choose another.")
+  dir.create(destpath)
+
+  owd <- setwd(destpath)
+  on.exit(setwd(owd))
+  rmarkdown::draft("index.Rmd",
+                   template = template,
+                   package = package,
+                   create_dir = FALSE,
+                   edit = edit)
+
+  invisible()
+}
diff --git a/R/add_report.R b/R/add_report.R
index a71d30a..4db5cc7 100644
--- a/R/add_report.R
+++ b/R/add_report.R
@@ -16,10 +16,8 @@ add_report <- function(name,
                        lang = "english",
                        path = pkgload::pkg_path()) {
   # reports folder handling
-  if (!dir.exists(file.path(path, "reports"))) {
-    message("Creating reports folder in: ", path, "...")
-    create_reports(path)
-  }
+  check_reports_folder(path)
+
   # Checking template existence
   template_location <- get_template_location(template)
 
diff --git a/R/create_reports.R b/R/create_reports.R
index 8836679..59979a2 100644
--- a/R/create_reports.R
+++ b/R/create_reports.R
@@ -16,7 +16,7 @@
 #' # Add a new report
 #' report_path <- add_report("my_report", path = path)
 #' list.files(report_path)
-create_reports <- function(path = ".", git = TRUE) {
+create_reports <- function(path = pkgload::pkg_path(), git = TRUE) {
   # Create reports folder
   reports_path <- file.path(path, "reports")
   dir.create(reports_path, showWarnings = FALSE)
@@ -29,3 +29,10 @@ create_reports <- function(path = ".", git = TRUE) {
   }
   invisible(file.path(path, "reports"))
 }
+
+check_reports_folder <- function(path) {
+  if (!dir.exists(file.path(path, "reports"))) {
+    message("Creating reports folder in: ", path, "...")
+    create_reports(path)
+  }
+}
diff --git a/R/list_reports.R b/R/list_reports.R
index ad8ded2..5544346 100644
--- a/R/list_reports.R
+++ b/R/list_reports.R
@@ -46,7 +46,14 @@ get_report_header <- function(path, index = "index.Rmd") {
   f <- file.path(path, index)
   if (file.exists(f)) {
     l <- read_rmd(file.path(path, index))
-    return(l$header)
+    header <- lapply(l$header, function(x) {
+      if (!is.list(x)) {
+        return(x)
+      } else {
+        return(NULL)
+      }
+    })
+    return(header)
   } else {
     warning("File ", f, " not found, the folder ", basename(path), " is ignored")
     return(NULL)
diff --git a/R/render_report.R b/R/render_report.R
index 005c03f..48a686e 100644
--- a/R/render_report.R
+++ b/R/render_report.R
@@ -24,15 +24,19 @@ render_report <- function(input,
   on.exit({setwd(owd)})
   input <- getwd()
 
-  add_before_chapter_script(input)
+  if (file.exists(file.path(input, "_bookdown.yml"))) {
+    add_before_chapter_script(input)
+  }
 
   if (output_format == "bookdown::pdf_book") {
     if (!requireNamespace("tinytex", quietly = TRUE))
       install.packages("tinytex")
-    babel_lang <-
-      yaml::read_yaml("_bookdown.yml")$babel_lang
-    if (!is.null(babel_lang)) {
-      tinytex_install_babel_language_support(babel_lang)
+    if (file.exists(file.path(input, "_bookdown.yml"))) {
+      babel_lang <-
+        yaml::read_yaml("_bookdown.yml")$babel_lang
+      if (!is.null(babel_lang)) {
+        tinytex_install_babel_language_support(babel_lang)
+      }
     }
   }
   message("output_format=", output_format)
@@ -52,6 +56,15 @@ render_report <- function(input,
     fun = bookdown::render_book,
     args = args
   )
+  output_index <- file.path(output_dir,
+                            paste0("index.",
+                                   ifelse(output_format == "bookdown::pdf_book",
+                                          "pdf",
+                                          "html")))
+  output_main <- sub("index", "_main", output_index)
+  if (file.exists(output_main)) {
+    file.rename(output_main, output_index)
+  }
 
   invisible()
 }
diff --git a/man/add_article.Rd b/man/add_article.Rd
new file mode 100644
index 0000000..9200ff5
--- /dev/null
+++ b/man/add_article.Rd
@@ -0,0 +1,38 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/add_article.R
+\name{add_article}
+\alias{add_article}
+\title{Add a new article in a fairify project}
+\usage{
+add_article(
+  name,
+  template,
+  package = "rticles",
+  path = pkgload::pkg_path(),
+  edit = FALSE
+)
+}
+\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{package}{(Optional) Name of package where the template is located.}
+
+\item{path}{Path of the fairify project}
+
+\item{edit}{\code{TRUE} to edit the template immediately}
+}
+\value{
+Used for side effect.
+}
+\description{
+This function prepares an article with a template provided by the \strong{rticles}
+package.
+}
+\details{
+This function uses \link[rmardown:draft]{rmardown::draft} for creating the new document based on
+a template, so its uses is theoritically not limited to the templates
+provided by the \strong{rticles} package.
+}
diff --git a/man/create_reports.Rd b/man/create_reports.Rd
index 107ace1..06e7c63 100644
--- a/man/create_reports.Rd
+++ b/man/create_reports.Rd
@@ -4,7 +4,7 @@
 \alias{create_reports}
 \title{Set reports folder structure}
 \usage{
-create_reports(path = ".", git = TRUE)
+create_reports(path = pkgload::pkg_path(), git = TRUE)
 }
 \arguments{
 \item{path}{Destination folder for reports and templates}
diff --git a/tests/testthat/test-add_article.R b/tests/testthat/test-add_article.R
new file mode 100644
index 0000000..50e7a38
--- /dev/null
+++ b/tests/testthat/test-add_article.R
@@ -0,0 +1,23 @@
+path <- helper_create_fairify()
+add_article(name = "myarticle",
+            template = "rjournal",
+            path = path)
+
+test_that("add_article works", {
+  expect_true(file.exists(file.path(path, "reports/myarticle/index.Rmd")))
+})
+
+test_that("render_reports works with articles", {
+  render_reports(reports_dir = file.path(path, "reports"),
+                 output_format = "bookdown::gitbook")
+  render_reports(reports_dir = file.path(path, "reports"),
+                 output_format = "bookdown::pdf_book")
+})
+
+test_that("list_reports works with articles", {
+  expect_equal(nrow(list_reports(file.path(path, "reports"))), 1)
+})
+
+test_that("build_site works with articles",  {
+  fairify::build_site(pkg = path)
+})
-- 
GitLab


From 25e533b5ad5cf6d781c8c4d05d0772c4f14d2b09 Mon Sep 17 00:00:00 2001
From: David Dorchies <david.dorchies@inrae.fr>
Date: Tue, 26 Mar 2024 18:44:31 +0100
Subject: [PATCH 2/5] docs: improve add_article doc

Refs #29
---
 R/add_article.R       | 10 +++++-----
 R/create_reports.R    |  4 ++++
 man/add_article.Rd    | 19 +++++++++++++++++--
 man/add_report.Rd     |  4 ++++
 man/create_reports.Rd |  4 ++++
 5 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/R/add_article.R b/R/add_article.R
index e17d3ce..5a7e34f 100644
--- a/R/add_article.R
+++ b/R/add_article.R
@@ -4,17 +4,18 @@
 #' package.
 #'
 #' @details
-#' This function uses [rmardown::draft] for creating the new document based on
+#' This function uses [rmarkdown::draft] for creating the new document based on
 #' a template, so its uses is theoritically not limited to the templates
 #' provided by the **rticles** package.
 #'
 #' @inheritParams add_report
 #' @inheritParams rmarkdown::draft
 #'
-#' @return Used for side effect.
+#' @return The path of the added article
 #' @export
 #'
-#' @examples
+#' @inherit create_reports examples
+#'
 add_article <- function(name,
                         template,
                         package = "rticles",
@@ -39,6 +40,5 @@ add_article <- function(name,
                    package = package,
                    create_dir = FALSE,
                    edit = edit)
-
-  invisible()
+  return(destpath)
 }
diff --git a/R/create_reports.R b/R/create_reports.R
index 59979a2..b4073dd 100644
--- a/R/create_reports.R
+++ b/R/create_reports.R
@@ -16,6 +16,10 @@
 #' # Add a new report
 #' report_path <- add_report("my_report", path = path)
 #' list.files(report_path)
+#'
+#' # Add a new article
+#' article_path <- add_article("my_article", template = "rjournal", path = path)
+#' list.files(article_path)
 create_reports <- function(path = pkgload::pkg_path(), git = TRUE) {
   # Create reports folder
   reports_path <- file.path(path, "reports")
diff --git a/man/add_article.Rd b/man/add_article.Rd
index 9200ff5..a4124eb 100644
--- a/man/add_article.Rd
+++ b/man/add_article.Rd
@@ -25,14 +25,29 @@ the "templates" folder of the fairify project}
 \item{edit}{\code{TRUE} to edit the template immediately}
 }
 \value{
-Used for side effect.
+The path of the added article
 }
 \description{
 This function prepares an article with a template provided by the \strong{rticles}
 package.
 }
 \details{
-This function uses \link[rmardown:draft]{rmardown::draft} for creating the new document based on
+This function uses \link[rmarkdown:draft]{rmarkdown::draft} for creating the new document based on
 a template, so its uses is theoritically not limited to the templates
 provided by the \strong{rticles} package.
 }
+\examples{
+# Create structure for reports
+path <- tempfile(pattern = "report-example")
+dir.create(path)
+create_reports(path, git = FALSE)
+list.files(path, recursive = TRUE)
+
+# Add a new report
+report_path <- add_report("my_report", path = path)
+list.files(report_path)
+
+# Add a new article
+article_path <- add_article("my_article", template = "rjournal", path = path)
+list.files(article_path)
+}
diff --git a/man/add_report.Rd b/man/add_report.Rd
index 27b30e0..da84182 100644
--- a/man/add_report.Rd
+++ b/man/add_report.Rd
@@ -37,4 +37,8 @@ list.files(path, recursive = TRUE)
 # Add a new report
 report_path <- add_report("my_report", path = path)
 list.files(report_path)
+
+# Add a new article
+article_path <- add_article("my_article", template = "rjournal", path = path)
+list.files(article_path)
 }
diff --git a/man/create_reports.Rd b/man/create_reports.Rd
index 06e7c63..76ab98f 100644
--- a/man/create_reports.Rd
+++ b/man/create_reports.Rd
@@ -27,4 +27,8 @@ list.files(path, recursive = TRUE)
 # Add a new report
 report_path <- add_report("my_report", path = path)
 list.files(report_path)
+
+# Add a new article
+article_path <- add_article("my_article", template = "rjournal", path = path)
+list.files(article_path)
 }
-- 
GitLab


From d3744e634f82c8d4676b4ed379c583ec59147c4b Mon Sep 17 00:00:00 2001
From: David Dorchies <david.dorchies@inrae.fr>
Date: Tue, 26 Mar 2024 21:26:58 +0100
Subject: [PATCH 3/5] refactor: role of render_report_setup

Refs #29
---
 R/add_report.R    |  2 +-
 R/render_report.R | 31 +++++++++++++++++++++----------
 2 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/R/add_report.R b/R/add_report.R
index 4db5cc7..e9436a0 100644
--- a/R/add_report.R
+++ b/R/add_report.R
@@ -76,7 +76,7 @@ add_report <- function(name,
                    file.path(destpath, "_fairify.yml"))
   # Setting the setup R script
   writeLines(
-    c("# Fairify configuration: don't change the following line",
+    c("# This script is run before every chapter",
       "fairify::render_report_setup(this.path::this.dir())",
       "# pkgload::load_all() # Uncomment to load current fairyfied project code and data\n",
       "# Add your own settings below to append or overwrite template settings"),
diff --git a/R/render_report.R b/R/render_report.R
index 48a686e..e90a4a8 100644
--- a/R/render_report.R
+++ b/R/render_report.R
@@ -24,7 +24,7 @@ render_report <- function(input,
   on.exit({setwd(owd)})
   input <- getwd()
 
-  if (file.exists(file.path(input, "_bookdown.yml"))) {
+  if (file.exists(file.path(input, "setup.R"))) {
     add_before_chapter_script(input)
   }
 
@@ -46,6 +46,21 @@ render_report <- function(input,
     unlink(file.path(input, c(".Rprofile", "templates")),
            recursive = TRUE)
   }, add = TRUE)
+  if (clean_cache) {
+    clean_cache_report(input)
+  }
+  file_bookdown <- file.path(input, "_bookdown.yml")
+  book_filename <- NULL
+  if (file.exists(file_bookdown)) {
+    book_filename <-
+      yaml::read_yaml(file_bookdown)$book_filename
+  }
+  if (is.null(book_filename)) book_filename <- "_main"
+  unlink(file.path(input, paste0(book_filename, ".*")))
+  file_fairify <- file.path(input, "_fairify.yml")
+  if (file.exists(file_fairify)) {
+    copy_templates(input)
+  }
   args <- list(
     input = file.path(input, input_index),
     output_format =  output_format,
@@ -72,16 +87,12 @@ render_report <- function(input,
 #' @rdname render_reports
 #' @export
 render_report_setup <- function(input = getwd(), clean_cache = FALSE) {
-  if (clean_cache) {
-    clean_cache_report(input)
-  }
-  copy_templates(input)
-  cfg_bookdown <-
-    yaml::read_yaml(file.path(input, "_bookdown.yml"))
-  unlink(file.path(input, paste0(cfg_bookdown$book_filename, ".*")))
   options(knitr.duplicate.label = "allow")
-  template <- yaml::read_yaml(file.path(input, "_fairify.yml"))$template
-  source_template_setup(template)
+  file_fairify <- file.path(input, "_fairify.yml")
+  if (file.exists(file_fairify)) {
+    template <- yaml::read_yaml(file_fairify)$template
+    source_template_setup(template)
+  }
 }
 
 copy_templates <- function(input) {
-- 
GitLab


From 316c7a591e89c48ed82c4f2f27092232cd1f6811 Mon Sep 17 00:00:00 2001
From: David Dorchies <david.dorchies@inrae.fr>
Date: Tue, 26 Mar 2024 21:27:29 +0100
Subject: [PATCH 4/5] fix: list_reports for articles

Refs #29
---
 R/list_reports.R | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/R/list_reports.R b/R/list_reports.R
index 5544346..a3ba392 100644
--- a/R/list_reports.R
+++ b/R/list_reports.R
@@ -48,7 +48,7 @@ get_report_header <- function(path, index = "index.Rmd") {
     l <- read_rmd(file.path(path, index))
     header <- lapply(l$header, function(x) {
       if (!is.list(x)) {
-        return(x)
+        return(paste(x, collapse = ", "))
       } else {
         return(NULL)
       }
-- 
GitLab


From 9d3cd1e88612ddf3e7a11995ad20dd6bfa1287c4 Mon Sep 17 00:00:00 2001
From: David Dorchies <david.dorchies@inrae.fr>
Date: Tue, 26 Mar 2024 21:28:05 +0100
Subject: [PATCH 5/5] fix: filenames for having pdf link in gitbook

Refs #29
---
 R/render_report.R                 | 9 ++-------
 tests/testthat/test-add_article.R | 3 +++
 2 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/R/render_report.R b/R/render_report.R
index e90a4a8..d57c1fe 100644
--- a/R/render_report.R
+++ b/R/render_report.R
@@ -71,14 +71,9 @@ render_report <- function(input,
     fun = bookdown::render_book,
     args = args
   )
-  output_index <- file.path(output_dir,
-                            paste0("index.",
-                                   ifelse(output_format == "bookdown::pdf_book",
-                                          "pdf",
-                                          "html")))
-  output_main <- sub("index", "_main", output_index)
+  output_main <- file.path(output_dir, "_main.html")
   if (file.exists(output_main)) {
-    file.rename(output_main, output_index)
+    file.rename(output_main, file.path(output_dir, "index.html"))
   }
 
   invisible()
diff --git a/tests/testthat/test-add_article.R b/tests/testthat/test-add_article.R
index 50e7a38..c098309 100644
--- a/tests/testthat/test-add_article.R
+++ b/tests/testthat/test-add_article.R
@@ -10,8 +10,10 @@ test_that("add_article works", {
 test_that("render_reports works with articles", {
   render_reports(reports_dir = file.path(path, "reports"),
                  output_format = "bookdown::gitbook")
+  expect_true(file.exists(file.path(path, "public/reports/myarticle/index.html")))
   render_reports(reports_dir = file.path(path, "reports"),
                  output_format = "bookdown::pdf_book")
+  expect_true(file.exists(file.path(path, "public/reports/myarticle/_main.pdf")))
 })
 
 test_that("list_reports works with articles", {
@@ -20,4 +22,5 @@ test_that("list_reports works with articles", {
 
 test_that("build_site works with articles",  {
   fairify::build_site(pkg = path)
+  expect_true(file.exists(file.path(path, "public/index.html")))
 })
-- 
GitLab