forked from andrewheiss/hikmah-academic-quarto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.R
60 lines (52 loc) · 2.31 KB
/
render.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
library(tidyverse)
library(quarto)
library(magick)
files_to_render <- tribble(
~file, ~format, ~ext, ~suffix,
"hikmah-testing-default", "hikmah-pdf", "pdf", "",
"hikmah-testing-default", "hikmah-manuscript-pdf", "pdf", "-manuscript",
"hikmah-testing-default", "hikmah-manuscript-docx", "docx", "-manuscript",
"hikmah-testing-default", "hikmah-manuscript-odt", "odt", "-manuscript",
"hikmah-testing-custom", "hikmah-pdf", "pdf", "",
"hikmah-testing-custom", "hikmah-manuscript-pdf", "pdf", "-manuscript",
"hikmah-response-memo", "hikmah-response-typst", "pdf", ""
)
rendered_files <- files_to_render %>%
rowwise() %>%
mutate(rendered = pmap_chr(list(file, format, suffix, ext), ~{
quarto_render(
input = paste0(file, ".qmd"),
output_format = format,
output_file = paste0(file, suffix, ".", ext)
)
return(paste0(file, suffix, ".", ext))
})) %>%
ungroup() %>%
mutate(thumbnail_name = str_replace_all(rendered, "\\.", "-"))
thumbify <- function(ext, rendered, thumbnail_name) {
# docx and odt files aren't images, so we can't actually read them with
# image_read_pdf(). So we first convert them to PDFs using LibreOffice's
# terminal command (soffice) and store the resulting PDFs temporarily
if (ext %in% c("docx", "odt")) {
outdir_temp <- tempdir()
system2("/Applications/LibreOffice.app/Contents/MacOS/soffice",
c("--headless", "--convert-to", "pdf:writer_pdf_Export",
paste0("examples/", rendered), "--outdir", outdir_temp))
to_convert <- paste0(outdir_temp, "/", tools::file_path_sans_ext(basename(rendered)), ".pdf")
} else {
to_convert <- paste0("examples/", rendered)
}
# Convert each rendered PDF to a collage and save as PNG
image_read_pdf(to_convert) %>%
image_montage(geometry = "x2000+25+35", tile = "3", bg = "grey92", shadow = TRUE) %>%
image_convert(format = "png") %>%
image_write(paste0("examples/thumbnails/", thumbnail_name, ".png"))
return(paste0(thumbnail_name, ".png"))
}
# Create thumbnail collages for each rendered file
thumbified_files <- rendered_files %>%
rowwise() %>%
mutate(thumbnail = pmap_chr(list(ext, rendered, thumbnail_name), ~{
thumbify(ext, rendered, thumbnail_name)
})) %>%
ungroup()