-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathknit
executable file
·46 lines (39 loc) · 1.57 KB
/
knit
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
#!/usr/bin/Rscript
## ./knit [epub|github|html|pdf] to generate different types of output
library(knitr)
library(formatR)
library(methods)
## global chunk options: use high quality Cairo PNG device
pdf.options(pointsize = 10) # smaller pointsize for recording plots
opts_chunk$set(cache=TRUE, fig.path='figure/', dev='Cairo_png',
fig.width=5, fig.height=5, cache.path = 'cache-local/', par=TRUE)
## chunk hooks
knit_hooks$set(par=function(before, options, envir){
if (before && options$fig.show!='none')
par(mar=c(4,4,.1,.1), cex.lab=.95, cex.axis=.9, mgp=c(2,.7,0), tcl=-.3)
})
local({
fmt = commandArgs(TRUE)
if (length(fmt) == 0L) fmt = 'epub'
if (fmt %in% c('github', 'html')) {
# upload to Imgur when output is for github or html
opts_knit$set(upload.fun = imgur_upload)
opts_chunk$set(cache.path = 'cache-upload/')
} else if (fmt == 'pdf') {
opts_chunk$set(cache.path = 'cache-pdf/')
# use high quality tikz graphics for PDF output
opts_chunk$set(dev = 'tikz', fig.width=4, fig.height=4)
x = readLines('template.tex')
options(tikzDocumentDeclaration = x[1], tikzDefaultEngine = 'xetex')
assign('tikzPackages', c(x[2:13], '\n'), envir = knitr:::.knitEnv)
}
# knit all md files under ./source
for (f in list.files('./source/', pattern = '^\\d{2}-.*\\.md$', full.names = TRUE)) {
unlink(basename(f))
knit(f, output = basename(f))
# make sure you do not do stupid things (edit output instead of source)
Sys.chmod(basename(f), mode = '0444')
}
})
## convert to an ebook (EPUB) or other formats
source('mdconvert.sh')