-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathblog_stuff.R
323 lines (284 loc) · 9.9 KB
/
blog_stuff.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#' Open a file in the Operating System
#'
#' Open a file in the operating system, using it's default program.
#' \code{sys_open} should work on Ubuntu (and other Linux variants), OSX and
#' Windows.
#'
#' @param f The path of the file to open
#' @return Nothing. Used for it's side effect.
#'
#' @author Based very heavily on the function \code{openFileInOS} from the
#' package \code{pander} (v0.5.2), written by Gergely Daroczi
#' (\email{daroczig@@rapporter.net}), itself based on the \code{convert}
#' function in the package \code{ascii}, written by David Hajage
#' (\email{dhajage@@gmail.com}).
#'
#' @export
sys_open <- function (f){
if (missing(f))
stop("No file to open!")
f <- normalizePath(f)
if (!file.exists(f))
stop("File not found!")
if (grepl("w|W", .Platform$OS.type)) {
shell.exec(f)
}
else {
if (grepl("darwin", version$os)) {
system(paste(shQuote("open"), shQuote(f)), wait = FALSE,
ignore.stderr = TRUE)
} else {
system(paste(shQuote("/usr/bin/xdg-open"), shQuote(f)),
wait = FALSE, ignore.stdout = TRUE)
}
}
}
#' Sanitise a String (URL/filename safe)
#'
#' Sanitise a string (downcasing, and removing puctuation and spaces), so that
#' it can safely be used in a URL or file path. Note: For URLs, hyphens, as
#' opposed to underscores are preferred by search bots.
#'
#' @param x The string to be santised
#' @param sep_char The character to use in place of spaces/punctuation found in
#' \code{x}
#' @param ext A file extenstion to be appended to the end of the result
#'
#' @return \code{character}
#'
#' @export
#' @aliases filenamize
filenamise <- function(x, sep_char = "_", ext = ""){
paste0(
gsub(
paste0(sep_char, "$|^", sep_char), "",
gsub(
paste0(sep_char, "+"), sep_char,
gsub("[[:space:]]|[[:punct:]]", sep_char, tolower(x))
)
),
ext
)
}
#' @export
#' @name filenamise
filenamize <- filenamise
#' @export
#' @name filenamise
filenameize <- filenamise
#' @export
#' @name filenamise
filenameise <- filenamise
#' File Structure for a Jekyll Blog Post
#'
#' A function to set-up the file structure for a Jekyll blog post. Assumes that
#' the current working directory is the root directory of the Jekyll site.
#'
#' @param title The title of the blog post
#' @param serve Should \code{\link{blog_serve}} be run once the files have been
#' set-up? Defatuls to \code{TRUE}.
#' @param dir The directory the post (or subdirectory) should reside in
#' @param subdir Should the post live in a subdirectory? Defaults to \code{TRUE}
#' @param skeleton_file The filepath of a skeleton blog post which will be used
#' as the basis for the basis for the newly created file
#'
#' @details { \code{new_post} will create a .R file, and a .Rmd file (by default
#' in a subdirectory), with names created by running \code{title} through
#' \code{\link{filenamise}}. The .R file will contain a short note mentioning
#' that it accompanies the .Rmd file, which will contain the same text as the
#' file supplied by \code{skeleton_post} paramter. Both files will be opened
#' using \code{\link{sys_open}}. }
#'
#' @export
new_post <- function(title = "new post", serve = TRUE, dir = "_source",
subdir = TRUE, skeleton_file = ".skeleton_post"){
if(!dir.exists(dir)){
stop("The directory '", dir, "' doesn't exist. Are you running R in
the right directory?")
}
# Sanitise the post title
fname <- filenamise(title, sep_char = "-")
if(subdir){
fpath <- file.path(dir, fname)
dir.create(fpath)
} else {
fpath <- dir
}
rmd_name <- file.path(fpath, paste0(Sys.Date(), "-", fname, ".Rmd"))
r_name <- file.path(fpath, paste0(fname, ".R"))
# Read in the skeleton post
# If it doesn't exist, emit a warning and use the package default
if(!file.exists(skeleton_file)){
message("File .skeleton_post does not exist. Using package default")
skeleton_file <- system.file("skeleton_post.Rmd", package = "brocks")
}
post <- readLines(skeleton_file)
post[grepl("title: ", post)] <- paste0("title: ", title)
writeLines(post, rmd_name)
# Write out an empty R file as well, in case that's useful
writeLines(
c("# This R file accomanies the .Rmd blog post", paste("#", rmd_name), ""),
r_name
)
sys_open(r_name)
sys_open(rmd_name)
if(serve)
blog_serve()
}
#' Serve or Compile a Jekyll Blog
#'
#' Serve or Compile a Jekyll Blog. A small wrapper around
#' servr::\code{\link{jekyll}}, which by default also looks for subdirectories
#' witin the main source directory.
#'
#' @param input passed to servr::\code{\link{jekyll}}
#' @param output passed to servr::\code{\link{jekyll}}
#' @param ... passed to servr::\code{\link{jekyll}}
#'
#' @export
blog_serve <- function(
input = c(".", list.dirs("_source")),
output = c(".", rep("_posts", length(list.dirs("_source")))),
...
){
servr::jekyll(input = input, output = output, serve = TRUE, ...)
}
#' @rdname blog_serve
#' @export
blog_gen <- function(
input = c(".", list.dirs("_source")),
output = c(".", rep("_posts", length(list.dirs("_source")))),
...
){
servr::jekyll(input = input, output = output, serve = FALSE, ...)
}
#' @rdname blog_serve
#' @export
site_gen <- function(
input = c('.', list.dirs('_dashboards'), list.dirs('_source')),
output = c('.', rep('.', length(list.dirs('_dashboards'))),
rep('_posts', length(list.dirs('_source')))),
...
){
servr::jekyll(input = input, output = output, serve = FALSE, ...)
}
#' @rdname blog_serve
#' @export
site_serve <- function(
input = c('.', list.dirs('_dashboards'), list.dirs('_source')),
output = c('.', rep('.', length(list.dirs('_dashboards'))),
rep('_posts', length(list.dirs('_source')))),
...
){
servr::jekyll(input = input, output = output, serve = TRUE, ...)
}
#' Push a blog post live (possibly)
#'
#' I use this function to push blog posts live. This is an incredibyly lazy
#' wrapper for \code{blog_gen();system(command)}, where \code{command} is by
#' default how I upload changes to my website.
#'
#' @param command Something which will be executed by \code{\link{system}}
#'
#'
#' @return Used for its side effects.
#' @export
blog_push <- function(command = 'bash _deploy.sh staging') {
blog_gen()
system(command)
}
#' Set some knitr chunk options which may work well for blogging
#'
#' A small wrapper around knitr's \code{\link[knitr]{opts_chunk}}$set, with some
#' defaults which I've found work well for blog posts. All messages from R are
#' surpressed, and the quality of the plots is increased to 6" X 6" 300 dpi
#' \code{\link{png}}s.
#'
#' @param ... passed to knitr::\code{\link[knitr]{opts_chunk}}$set
#'
#' @export
blog_opts <- function(...){
knitr::opts_chunk$set(
echo = FALSE,
warning = FALSE,
error = FALSE,
message = FALSE,
device = 'png',
fig.height = 6,
fig.width = 6,
dpi = 300,
...
)
}
#' Configure htmlwidgets dependencies for a knitr-jekyll blog
#'
#' Unlike static image plots, the outputs of htmlwidgets dependencies also have
#' Javascript and CSS dependencies, which are not by default processed by knitr.
#' \code{htmlwdigets_deps} provides a system to add the dependencies to a Jekyll
#' blog. Further details are available in the following blog post:
#' \url{http://brendanrocks.com/htwmlwidgets-knitr-jekyll/}.
#'
#' @param a The file path for the input file being knit
#' @param knit_meta The dependencies object.
#' @param lib_dir The directory where the htmlwidgets dependency source code can
#' be found (e.g. JavaScript and CSS files)
#' @param includes_dir The directory to add the HTML file to
#' @param always Should dependency files always be produced, even if htmlwidgets
#' are not being used?
#'
#' @return Used for it's side effects.
#' @export
htmlwidgets_deps <- function(a, knit_meta = knitr::knit_meta(),
lib_dir = "htmlwidgets_deps",
includes_dir = "_includes/htmlwidgets/",
always = FALSE) {
# If the directories don't exist, create them
dir.create(lib_dir, showWarnings = FALSE, recursive = TRUE)
dir.create(includes_dir, showWarnings = FALSE, recursive = TRUE)
# Copy the libraries from the R packages to the 'htmlwidgets_deps' dir, and
# obtain the html code required to import them
deps_str <- html_dependencies_to_string(knit_meta, lib_dir, ".")
# *Sometimes* Jekyll markdown posts are prefixed with a 12 char ISO date and
# hypen, before becoming html posts. Remove, if present.
lose_date <- function(x) {
gsub("^[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}-", "", x)
}
# Write the html dependency import code to a file, to be imported by the
# liquid templates
deps_file <- paste0(
includes_dir,
gsub(".Rmd$", ".html", lose_date(basename(a[1])))
)
# Write out the file if either, the dependencies string has anything to add,
# or, if the always parameter has been set to TRUE (useful for those building
# with GitHub pages)
if(always | !grepl("^[[:space:]]*$", deps_str))
writeLines(deps_str, deps_file)
}
#' @keywords internal
#' Adapted from rmarkdown:::html_dependencies_as_string
html_dependencies_to_string <- function (dependencies, lib_dir, output_dir) {
# Flatten and resolve html deps
dependencies <- html_dependency_resolver(
flatten_html_dependencies(dependencies)
)
if (!is.null(lib_dir)) {
dependencies <- lapply(
dependencies, htmltools::copyDependencyToDir, lib_dir
)
dependencies <- lapply(
dependencies, htmltools::makeDependencyRelative, output_dir
)
}
# A function to add Jekyll boilerplate
prepend_baseurl <- function(path){
# If the url doesn't start "/", make sure that it does
path <- ifelse(!grepl("^/", path), paste0("/", path), path)
paste0('{{ "', path, '" | prepend: site.baseurl }}')
}
htmltools::renderDependencies(
dependencies, "file",
encodeFunc = identity,
hrefFilter = prepend_baseurl
)
}