Skip to content

Commit

Permalink
Merge pull request #37 from inbo/post_processor
Browse files Browse the repository at this point in the history
Post processor
  • Loading branch information
ThierryO authored Sep 11, 2017
2 parents dfc2e66 + d3b65e0 commit ed2526c
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 94 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
^.*\.Rproj$
^\.Rproj\.user$
^wercker\.yml$
^\.zenodo\.json$
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ importFrom(rmarkdown,output_format)
importFrom(rmarkdown,pandoc_options)
importFrom(rmarkdown,pandoc_path_arg)
importFrom(rmarkdown,pandoc_variable_arg)
importFrom(rmarkdown,pdf_document)
importFrom(rmarkdown,render)
importFrom(utils,file_test)
importFrom(utils,install.packages)
Expand Down
109 changes: 86 additions & 23 deletions R/rsos_article.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,98 @@
#' }
#'
#' @export
rsos_article <- function(..., keep_tex = TRUE, citation_package = "natbib") {
inherit_pdf_document(
...,
keep_tex = keep_tex,
citation_package = citation_package,
template = find_resource("rsos_article", "template.tex")
)
}
#' @importFrom rmarkdown output_format knitr_options pandoc_options pandoc_variable_arg includes_to_pandoc_args
rsos_article <- function(
...,
keep_tex = TRUE,
pandoc_args = NULL,
includes = NULL,
fig_crop = TRUE
) {

extra <- list(...)

find_file <- function(template, file) {
template <- system.file(
"rmarkdown", "templates", template, file,
"rmarkdown/templates/rsos_article/resources/template.tex",
package = "INBOmd"
)
if (template == "") {
stop("Couldn't find template file ", template, "/", file, call. = FALSE)
args <- c(
"--template", template,
"--latex-engine", "xelatex",
pandoc_variable_arg("documentclass", "article"),
pandoc_args,
"--natbib",
includes_to_pandoc_args(includes)
)

if (length(extra) > 0) {
args <- c(
args,
sapply(
names(extra),
function(x){
pandoc_variable_arg(x, extra[[x]])
}
)
)
}

template
}
opts_chunk <- list(
latex.options = "{}",
dev = "pdf",
fig.align = "center",
dpi = 300,
fig.width = 4.5,
fig.height = 2.9
)
crop <- fig_crop &&
!identical(.Platform$OS.type, "windows") &&
nzchar(Sys.which("pdfcrop"))
if (crop) {
knit_hooks <- list(crop = knitr::hook_pdfcrop)
opts_chunk$crop <- TRUE
} else {
knit_hooks <- NULL
}

find_resource <- function(template, file) {
find_file(template, file.path("resources", file))
}
post_processor <- function(
metadata, input_file, output_file, clean, verbose
) {
text <- readLines(output_file, warn = FALSE)

#'@importFrom rmarkdown pdf_document
# Call rmarkdown::pdf_documet and mark the return value as inheriting pdf_document
inherit_pdf_document <- function(...) {
fmt <- rmarkdown::pdf_document(...)
fmt$inherits <- "pdf_document"
fmt
# set correct text in fmtext environment
end_first_page <- grep("^\\\\EndFirstPage", text) #nolint
if (length(end_first_page)) {
maketitle <- grep("\\\\maketitle", text) #nolint
text <- c(
text[1:(maketitle - 1)],
"\\begin{fmtext}",
text[(maketitle + 1):(end_first_page - 1)],
"\\end{fmtext}",
"\\maketitle",
text[(end_first_page + 1):length(text)]
)
}
writeLines(enc2utf8(text), output_file, useBytes = TRUE)
output_file
}

output_format(
knitr = knitr_options(
opts_knit = list(
width = 96,
concordance = TRUE
),
opts_chunk = opts_chunk,
knit_hooks = knit_hooks
),
pandoc = pandoc_options(
to = "latex",
latex_engine = "xelatex",
args = args,
keep_tex = keep_tex
),
post_processor = post_processor,
clean_supporting = !keep_tex
)
}
10 changes: 2 additions & 8 deletions inst/rmarkdown/templates/rsos_article/resources/template.tex
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,8 @@
%% Some pieces required from the pandoc template
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

%%%%%%%%%% Insert the texts which can accomdate on firstpage in the tag "fmtext" %%%%%

\begin{fmtext}
$first_page_text$
\end{fmtext}

%%%%%%%%%%%%%%% End of first page %%%%%%%%%%%%%%%%%%%%%
\providecommand{\EndFirstPage}{%
}

\maketitle

Expand Down
120 changes: 61 additions & 59 deletions inst/rmarkdown/templates/rsos_article/skeleton/skeleton.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -29,64 +29,6 @@ keywords:
abstract: |
The abstract text goes here. The abstract text goes here. The abstract text goes here. The abstract text goes here. The abstract text goes here. The abstract text goes here. The abstract text goes here. The abstract text goes here.
first_page_text: |
# Insert A head here
This demo file is intended to serve as a "starter file"" for articles
submitted to the Royal Society Open Science journal using `RMarkdown`.
The RMarkdown argument `first_page_text` should hold all plain text to be
displayed on the first page of the page. Warning: excess text will be hidden
behind the copyright box. The example below contains line 1 to 19 in the code.
Line 14 to 17 are hidden.
## Insert B head here
Subsection text here.
### Insert C head here
Subsubsection text here.
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
Line 12
Line 13
Line 14
Line 15
Line 16
Line 17
Line 18
Line 19
## Remove this if not required
ethics: |
Please provide details on the ethics.
Expand Down Expand Up @@ -118,9 +60,69 @@ bibliography: sample.bib
## change to true to add optional line numbering
lineno: false

output: INBOmd::rsos_article
output:
bookdown::pdf_book:
base_format: INBOmd::rsos_article
---

# Insert A head here

This demo file is intended to serve as a "starter file"" for articles
submitted to the Royal Society Open Science journal using `RMarkdown`.

The RMarkdown argument `first_page_text` should hold all plain text to be
displayed on the first page of the page. Warning: excess text will be hidden
behind the copyright box. The example below contains line 1 to 19 in the code.
Line 14 to 17 are hidden.

## Insert B head here

Subsection text here.

### Insert C head here

Subsubsection text here.

Line 1

Line 2

Line 3

Line 4

Line 5

Line 6

Line 7

Line 8

Line 9

Line 10

Line 11

Line 12

Line 13

Line 14

Line 15

Line 16

Line 17

Line 18

Line 19

\EndFirstPage

# Lists

* one
Expand Down
12 changes: 9 additions & 3 deletions man/rsos_article.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ed2526c

Please sign in to comment.