-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmake_config.R
335 lines (272 loc) · 9.27 KB
/
make_config.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
324
325
326
327
328
329
330
331
332
333
334
335
# TODO:
# - pdf/tex:
# - enable additional argument for entry in list of figures per latex \caption[short]{long}
# - md:
# - link toc to headers
# - see rmarkdown::includes
# load libraries ----
library(knitr)
library(rmarkdown)
library(brew)
# set variables ----
# strings for substitution
title = 'Spatio-Temporal Decision Frameworks for Minimizing Impact of Human Activities on Marine Megafauna'
author = 'Benjamin D. Best'
supervisor = 'Patrick N. Halpin'
department = 'Marine Science and Conservation'
school = 'Duke University'
member1 = 'Dean L. Urban'
member2 = '[TBD]'
member3 = 'Falk Huettman'
# paths to Rmd and bibliography files. see other cite styles at zotero.org/styles.
cite_bib = 'dissertation.bib'
cite_style_pdf = 'csl/apa-no-doi-no-issue.csl'
cite_style_other = 'csl/apa-single-spaced.csl'
dir_notgit = '~/Dropbox/dissertation'
files_keep = c('dissertation.brew.tex','dissertation.tex','dissertation.md','README.md')
files_pdf = list(
preamble = c('a_abstract', 'a_acknowledgements'),
body = c('a_intro','c_sdm','c_siting','c_range','c_routing','c_migration','x_conclusion', 'x_appendix'),
epilogue = c('x_biography'))
files_other = c('a_title', 'a_abstract', files_pdf$body)
file_html_head = 'a_title'
# helper functions ----
# figures
fig = local({
# initialize
if (!exists('doc_type')) doc_type = 'html'
i = 0
figs = numeric(0)
function(short, img, long) {
# set counter
i <<- i + 1
figs <<- c(figs, setNames(i, short))
# return text
if (doc_type=='pdf'){
#sprintf('![%s](%s)<a name="%s"></a>', long, img, short))
return(sprintf(paste(
'\\begin{figure}[htbp]',
'\\centering',
'\\includegraphics[width=6in]{%s}',
'\\caption{%s}',
'\\label{%s}',
'\\end{figure}', collapse='\n'),
img, long, short))
} else if (doc_type=='md'){
return(sprintf('<a name="%s"></a>\n\n![Figure %d. %s](%s) <br />\\\n_Figure %d. %s_', short, i, long, img, i, long))
} else {
return(sprintf('<a name="%s"></a>\n\n![Figure %d. %s](%s)', short, i, long, img))
}
}
})
# tables
tbl = local({
# initialize
if (!exists('doc_type')) doc_type = 'html'
j = 0
tbls = numeric(0)
function(short, long) {
# set counter
j <<- j + 1
tbls <<- c(tbls, setNames(j, short))
# return text
if (doc_type=='pdf'){
return(sprintf('Table: %s <a name="%s"></a>', long, short))
} else {
return(sprintf('Table %d: %s <a name="%s"></a>', j, long, short))
}
}
})
# reset counters for tables and figures
reset_fig_tbl = function(doc_type){
environment(fig)$i = 0
environment(tbl)$j = 0
environment(fig)$figs = numeric(0)
environment(tbl)$tbls = numeric(0)
environment(fig)$doc_type = doc_type
environment(tbl)$doc_type = doc_type
}
# reference table or figure
ref = function(short){
# NOTE: figure or table has to preceed ref. for now.
pfx = substr(short, 1, 3)
if (!exists('doc_type')) stop('Need to set global variable doc_type for ref() to work.')
labels = c(fig='Figure', tbl='Table')
if (!pfx %in% names(labels)) stop(sprintf("The function ref failed because the prefix '%s' for ref('%s') is not of the allowed short figure/table prefixes: %s.", pfx, short, paste(names(labels), collapse=', ')))
if (doc_type == 'pdf'){
return(sprintf('%s \\ref{%s}', labels[pfx], short))
} else {
return(sprintf('[%s %s](#%s)', labels[pfx], environment(fig)$figs[short], short))
}
}
# move and open file
mv_open = function(f, dir=dir_notgit, mv_f=T, open_f=T){
# move to dropbox folder, outside of git versioning
if (mv_f){
p = file.path(dir, f)
file.copy(f, p, overwrite=T)
unlink(f)
} else {
p = f
}
# open
if (open_f){
system(paste('open', p))
}
}
# render functions ----
cat_Rmd = function(
files_Rmd = sprintf('%s.Rmd', files_other),
out_Rmd = 'dissertation.Rmd'){
doc_type <<- 'Rmd'
if (file.exists(out_Rmd)) unlink(out_Rmd)
for (f_Rmd in files_Rmd){
# strip YAML metadata header
o = rmarkdown:::partition_yaml_front_matter(readLines(f_Rmd))
# cat into single file
cat(paste(c(o$body,'',''), collapse='\n'), file=out_Rmd, append=T)
}
# add references
cat('\n\n# References {-}\n <!-- adding blank content for References to show up in toc -->', file=out_Rmd, append=T)
}
render_md = function(
in_Rmd = 'dissertation.Rmd',
out_md = sprintf('%s.md', tools::file_path_sans_ext(in_Rmd)),
open = F,
cite_style = cite_style_other){
doc_type <<- 'md'
reset_fig_tbl(doc_type)
# Rmd to md
render(
in_Rmd, md_document(
variant='markdown_github',
toc=T, toc_depth=3,
pandoc_args=c(
'--bibliography', cite_bib,
'--csl', cite_style)),
out_md, quiet=F)
if (open) system(paste0('open', out_md))
}
render_html = function(
in_Rmd = 'dissertation.Rmd',
out_html = sprintf('%s.html', tools::file_path_sans_ext(in_Rmd)),
move = F,
open = T,
cite_style = cite_style_other,
header = file_html_head){
doc_type <<- 'html'
reset_fig_tbl(doc_type)
cat('render_html:\n',' in_Rmd:',in_Rmd,'\n',' header:',header,'\n')
tmp_Rmd = tempfile('tempfile_', tmpdir=dirname(in_Rmd), fileext='.Rmd')
if (in_Rmd != 'dissertation.Rmd'){
cat('header:',header,'\n')
# add title header
title = in_Rmd
file.copy(sprintf('%s.Rmd', header), tmp_Rmd)
# copy body
cat(readLines(in_Rmd), file=tmp_Rmd, sep='\n', append=T)
# add references
cat('\n\n# References {-}\n <!-- adding blank content for References to show up in toc -->', file=tmp_Rmd, append=T)
} else {
file.copy(in_Rmd, tmp_Rmd)
}
# Rmd to html
render(
tmp_Rmd, output_format=html_document(
number_sections=T, fig_width = 7, fig_height = 5, fig_retina = 2, fig_caption = T, smart=T,
self_contained=T, theme='default',
highlight='default', mathjax='default', template='default',
toc=T, toc_depth=3,
pandoc_args=c(
'--bibliography', cite_bib,
'--csl', cite_style)),
out_html, clean=T, quiet=F)
# move to dropbox and open
unlink(tmp_Rmd)
mv_open(out_html, mv_f=move, open_f=open)
}
render_docx = function(
in_Rmd = 'dissertation.Rmd',
out_word = sprintf('%s.docx', tools::file_path_sans_ext(in_Rmd)),
open = T,
cite_style = cite_style_other){
doc_type <<- 'word'
reset_fig_tbl(doc_type)
# Rmd to docx
render(
in_Rmd, output_format=word_document(
fig_caption = T, fig_width = 7, fig_height = 5,
highlight='default', reference_docx='default',
pandoc_args=c(
'--bibliography', cite_bib,
'--csl', cite_style)),
out_word, quiet=F)
# move to dropbox and open
mv_open(out_word, open_f=open)
# manual post-hoc
cat('to be handled by dissertation_quick_fixes macro (see dissertation/README):
- resize all pictures to 6 inch width
- Insert > Index and Tables > Table of Contents
- Add page numbering\n')
cat('to do manually:
- style Title and Subtitle, center subtitle
- Insert Watermark > text DRAFT, orientation 45, transparency 60
- View > Sidebar > Document Map Pane\n')
}
render_pdf = function(
files = files_pdf,
out_pdf = 'dissertation.pdf',
cite_style = cite_style_pdf,
open = T,
cleanup = T,
debug = F){
doc_type <<- 'pdf'
reset_fig_tbl(doc_type)
# Rmd to md
for (f_Rmd in sprintf('%s.Rmd', unlist(files))){
knit(f_Rmd)
}
# cat md body
body_md = 'body.md'
if (file.exists(body_md)) unlink(body_md)
for (f_md in sprintf('%s.md', files$body)){
o = rmarkdown:::partition_yaml_front_matter(readLines(f_md))
cat(paste(c(o$body,'',''), collapse='\n'), file=body_md, append=T)
}
# add references, single-spaced, hanging indent
cat(
'
# References {-}
<!-- add hanging indent, per https://groups.google.com/d/msg/pandoc-discuss/4SKA5E11rO4/fDGiNSOsIMkJ, https://groups.google.com/d/msg/pandoc-discuss/SUZ08-Kc6Og/ce0icuemwkkJ -->
\\noindent
\\vspace{-2em}
\\setlength{\\parindent}{-0.3in}
\\setlength{\\leftskip}{0.3in}
\\setlength{\\parskip}{10pt}
<!-- single space -->
\\normalbaselines
', file=body_md, append=T)
# md to tex
for (f in c(files$preamble, 'body', files$epilogue)){
system(paste(
'pandoc', paste0(f, '.md'), '--chapters',
'--bibliography', cite_bib, '--csl', cite_style,
'--from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash',
'--latex-engine=xelatex --to latex --output', paste0(f, '.tex')))
}
# substitute variables: dissertation.brew.tex -> dissertation.tex
brew('dissertation.brew.tex', 'dissertation.tex')
# note: any errors hang RStudio, so better to run from Terminal or with Complile PDF button in RStudio with dissertation.tex open
if (!debug) {
system('pdflatex dissertation.tex; pdflatex dissertation.tex')
# move to dropbox and open
mv_open('dissertation.pdf', , open_f=open)
# clean up
if (cleanup){
f_del = setdiff(list.files(pattern='^.*\\.(aux|lof|log|lot|out|toc|tex|md)$'), files_keep)
for (f in f_del) unlink(f)
}
} else {
cat('In debug=T mode, so next run on terminal:n\n pdflatex dissertation.tex; pdflatex dissertation.tex\n\n')
}
}