forked from shawnohare/hugo-tufte
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.R
248 lines (224 loc) · 8.6 KB
/
build.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# What I want:
# - read all the Rmd files in content/post
# - render them with a modified tufte::tufte_html
# - hugo should take it from there?
# From the blogdown book - this is the passage we need to follow in order
# to fix the first render/cache issue which appends "#fig:fig..." to
# figure captions in the margin
# For method = 'custom', build_site() will not process any R Markdown files,
# nor will it call Hugo to build the site. No matter which method you choose
# to use, build_site() will always look for an R script /R/build.R and execute
# it if it exists. This gives you the complete freedom to do anything you want
# for the website. For example, you can call knitr::knit() to compile Rmd to
# Markdown (*.md) in this R script instead of using rmarkdown::render(). This
# feature is designed for advanced users who are really familiar with the
# knitr package48 and Hugo or other static website generators (see Chapter 5).
#
# When R/build.R exists and method = 'html', the R Markdown files are knitted first,
# then the script R/build.R is executed, and lastly Hugo is called to build the website.
library(blogdown)
library(tufte)
library(xfun)
`%n%` <- knitr:::`%n%`
gsub_fixed <- function(...) gsub(..., fixed = TRUE)
pandoc2.0 <- function() rmarkdown::pandoc_available("2.0")
# Get the unexported functions blogdown and tufte
r1 <- unclass(lsf.str(envir = asNamespace("blogdown"), all = T))
r1 <- r1[-grep("%", r1)]
for (name in r1) eval(parse(text = paste0(name, "<-blogdown:::", name)))
r2 <- unclass(lsf.str(envir = asNamespace("tufte"), all = T))
for (name in r2) eval(parse(text = paste0(name, "<-tufte:::", name)))
tufte_hugo_html <- function(..., margin_references = TRUE) {
format <- rmarkdown::html_document(...)
pandoc2 <- pandoc2.0()
# when fig.margin = TRUE, set fig.beforecode = TRUE so plots are moved before
# code blocks, and they can be top-aligned
ohooks <- knitr::opts_hooks$set(fig.margin = function(options) {
if (isTRUE(options$fig.margin)) options$fig.beforecode <- TRUE
options
})
# make sure the existing post processor is called first in our new processor
post_processor <- format$post_processor
format$post_processor <- function(metadata, input, output, clean, verbose) {
if (is.function(post_processor)) {
output <- post_processor(metadata, input, output, clean, verbose)
}
knitr::opts_hooks$restore(ohooks)
x <- xfun::read_utf8(output)
fn_label <- paste0(knitr::opts_knit$get("rmarkdown.pandoc.id_prefix"), "fn")
footnotes <- parse_footnotes(x, fn_label)
notes <- footnotes$items
# replace footnotes with sidenotes
for (i in seq_along(notes)) {
num <- sprintf(
'<a href="#%s%d" class="%s" id="%sref%d"><sup>%d</sup></a>',
fn_label, i, if (pandoc2) "footnote-ref" else "footnoteRef", fn_label, i, i
)
con <- sprintf(paste0(
'<label for="tufte-sn-%d" class="margin-toggle sidenote-number"></label>',
'<input type="checkbox" id="tufte-sn-%d" class="margin-toggle">',
'<span class="sidenote"> %s</span>'
), i, i, notes[i])
x <- gsub_fixed(num, con, x)
}
# remove footnotes at the bottom
if (length(footnotes$range)) x <- x[-footnotes$range]
# replace citations with margin notes
if (margin_references) x <- margin_references(x)
# place figure captions in margin notes
x[x == '<p class="caption">'] <- '<p class="caption marginnote shownote">'
# move </caption> to the same line as <caption>; the previous line should
# start with <table
for (i in intersect(grep("^<caption>", x), grep("^<table", x) + 1)) {
j <- 0
while (!grepl("</caption>$", x[i])) {
j <- j + 1
x[i] <- paste0(x[i], x[i + j])
x[i + j] <- ""
}
}
# place table captions in the margin
r <- "^<caption>(.+)</caption>$"
for (i in grep(r, x)) {
# the previous line should be <table> or <table class=...>
if (!grepl("^<table( class=.+)?>$", x[i - 1])) next
cap <- gsub(r, "\\1", x[i])
x[i] <- x[i - 1]
x[i - 1] <- paste0(
"<p><!--\n<caption>-->", '<span class="marginnote shownote">',
cap, "</span><!--</caption>--></p>"
)
}
# add an incremental number to the id of <label> and <input> for margin notes
r <- '(<label|<input type="checkbox") (id|for)(="tufte-mn)-(" )'
m <- gregexpr(r, x)
j <- 1
regmatches(x, m) <- lapply(regmatches(x, m), function(z) {
n <- length(z)
if (n == 0) return(z)
if (n %% 2 != 0) warning("The number of labels is different with checkboxes")
for (i in seq(1, n, 2)) {
if (i + 1 > n) break
z[i + (0:1)] <- gsub(r, paste0("\\1 \\2\\3-", j, "\\4"), z[i + (0:1)])
j <<- j + 1
}
z
})
xfun::write_utf8(x, output)
output
}
if (is.null(format$knitr$knit_hooks)) format$knitr$knit_hooks <- list()
format$knitr$knit_hooks$plot <- function(x, options) {
# make sure the plot hook always generates HTML code instead of ![]()
if (is.null(options$out.extra)) options$out.extra <- ""
fig_margin <- isTRUE(options$fig.margin)
fig_fullwd <- isTRUE(options$fig.fullwidth)
if (fig_margin || fig_fullwd) {
if (is.null(options$fig.cap)) options$fig.cap <- " " # empty caption
} else if (is.null(options$fig.topcaption)) {
# for normal figures, place captions at the top of images
options$fig.topcaption <- TRUE
}
res <- knitr::hook_plot_md(x, options)
if (fig_margin) {
res <- gsub_fixed('<p class="caption">', '<!--\n<p class="caption marginnote">-->', res)
res <- gsub_fixed("</p>", "<!--</p>-->", res)
res <- gsub_fixed("</div>", "<!--</div>--></span></p>", res)
res <- gsub_fixed(
'<div class="figure">', paste0(
"<p>", '<span class="marginnote shownote">', '\n<!--\n<div class="figure">-->'
), res
)
}
if (fig_fullwd) {
res <- gsub_fixed('<div class="figure">', '<div class="figure fullwidth">', res)
res <- gsub_fixed('<p class="caption">', '<p class="caption marginnote shownote">', res)
}
res
}
knitr::knit_engines$set(marginfigure = function(options) {
options$type <- "marginnote"
if (is.null(options$html.tag)) options$html.tag <- "span"
options$html.before <- marginnote_html()
eng_block <- knitr::knit_engines$get("block")
eng_block(options)
})
format$inherits <- "html_document"
format
}
tufte_html_page <- function(..., number_sections = FALSE, self_contained = FALSE,
highlight = NULL, template = NULL, post_processor = NULL) {
if (identical(template, "default")) {
stop('blogdown::html_page() does not support template = "default"')
}
if (identical(highlight, "textmate")) {
stop('blogdown::html_page() does not support highlight = "textmate"')
}
if (is.character(post_processor)) {
post_processor <- eval(parse(text = post_processor))
}
rmarkdown::output_format(
knitr = NULL,
pandoc = NULL,
clean_supporting = self_contained,
post_processor = post_processor,
base_format = tufte_hugo_html(
...,
number_sections = number_sections, theme = NULL,
self_contained = self_contained, highlight = highlight,
template = template %n% pkg_file("resources", "template-minimal.html")
)
)
}
build_rmds <- function(files) {
if (length(files) == 0) {
return()
}
lib1 <- by_products(files, c("_files", "_cache"))
lib2 <- gsub("^content", "blogdown", lib1)
i <- grep("_files$", lib2)
lib2[i] <- gsub("^blogdown", "static", lib2[i])
dirs_rename(lib2, lib1)
on.exit(dirs_rename(lib1, lib2), add = TRUE)
root <- getwd()
base <- site_base_dir()
shared_yml <- file.path(root, "_output.yml")
copied_yaml <- character()
on.exit(unlink(copied_yaml), add = TRUE)
copy_output_yml <- function(to) {
if (!file.exists(shared_yml)) {
return()
}
if (file.exists(copy <- file.path(to, "_output.yml"))) {
return()
}
if (file.copy(shared_yml, copy)) {
copied_yaml <<- c(copied_yaml, copy)
}
}
for (f in files) {
d <- dirname(f)
out <- output_file(f, to_md <- is_rmarkdown(f))
copy_output_yml(d)
message("Rendering ", f)
rmarkdown::render(f, tufte_html_page(), envir = globalenv(), quiet = TRUE,
encoding = "UTF-8", run_pandoc = !to_md, clean = !to_md
)
x <- read_utf8(out)
x <- encode_paths(
x, by_products(f, "_files"), d, base,
to_md
)
if (to_md) {
write_utf8(x, out)
}
else {
if (getOption("blogdown.widgetsID", TRUE)) {
x <- clean_widget_html(x)
}
prepend_yaml(f, out, x)
}
}
}
rmds <- list_rmds("content")
build_rmds(rmds)