-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.R
153 lines (139 loc) · 5.32 KB
/
format.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
#!/usr/bin/env Rscript
if (!requireNamespace("stringr", quietly = TRUE))
stop("Please install the package stringr and re-run")
library(stringr)
args <- commandArgs(trailingOnly = TRUE)
if (length(args) > 1) {
stop("Usage: Rscript format.R filename")
} else if (length(args) == 1) {
fname <- args
} else {
fname <- "workflowr-publication.tex"
}
lines_in <- readLines(fname)
lines_out <- lines_in
# Format function names (brute force b/c getting clever with regex was hard to
# maintain), e.g. wflow_start() -> \texttt{wflow\_start()}
lines_out <- str_replace_all(lines_out,
"wflow_start\\(\\)",
"\\\\texttt{wflow\\\\_start()}")
lines_out <- str_replace_all(lines_out,
"wflow_build\\(\\)",
"\\\\texttt{wflow\\\\_build()}")
lines_out <- str_replace_all(lines_out,
"wflow_publish\\(\\)",
"\\\\texttt{wflow\\\\_publish()}")
lines_out <- str_replace_all(lines_out,
"wflow_status\\(\\)",
"\\\\texttt{wflow\\\\_status()}")
lines_out <- str_replace_all(lines_out,
"wflow_git_push\\(\\)",
"\\\\texttt{wflow\\\\_git\\\\_push()}")
lines_out <- str_replace_all(lines_out,
"wflow_use_github\\(\\)",
"\\\\texttt{wflow\\\\_use\\\\_github()}")
lines_out <- str_replace_all(lines_out,
"wflow_use_gitlab\\(\\)",
"\\\\texttt{wflow\\\\_use\\\\_gitlab()}")
lines_out <- str_replace_all(lines_out,
"wflow_site\\(\\)",
"\\\\texttt{wflow\\\\_site()}")
lines_out <- str_replace_all(lines_out,
"wflow_git_pull\\(\\)",
"\\\\texttt{wflow\\\\_git\\\\_pull()}")
lines_out <- str_replace_all(lines_out,
"wflow_html\\(\\)",
"\\\\texttt{wflow\\\\_html()}")
lines_out <- str_replace_all(lines_out,
"wflow_git_remote\\(\\)",
"\\\\texttt{wflow\\\\_git\\\\_remote()}")
lines_out <- str_replace_all(lines_out,
"wflow_git_config\\(\\)",
"\\\\texttt{wflow\\\\_git\\\\_config()}")
lines_out <- str_replace_all(lines_out,
"render\\(\\)",
"\\\\texttt{render()}")
lines_out <- str_replace_all(lines_out,
"render_site\\(\\)",
"\\\\texttt{render\\\\_site()}")
lines_out <- str_replace_all(lines_out,
"html_document\\(\\)",
"\\\\texttt{html\\\\_document()}")
lines_out <- str_replace_all(lines_out,
"sessionInfo\\(\\)",
"\\\\texttt{sessionInfo()}")
lines_out <- str_replace_all(lines_out,
"set.seed\\(\\)",
"\\\\texttt{set.seed()}")
# Replace \verb|| with \texttt{}
# Note: we had started with \verb and then transitioned to \texttt
lines_out <- str_replace_all(lines_out, "\\\\verb\\|([^\\|]+)\\|", "\\\\texttt\\{\\1}")
# Replace # with \# for LaTeX
lines_out <- str_replace_all(lines_out, "#", "\\\\#")
# Replace & with \& for LaTeX
lines_out <- str_replace_all(lines_out, "&", "\\\\&")
# Remove empty [] where figures were in Word document
lines_out <- str_replace_all(lines_out, "^\\[\\]$", "")
# Fix \texttt{\_workflowr.yml} split over 2 lines
lines_out <- str_replace(lines_out, "\\\\texttt\\{\\\\$", "")
lines_out <- str_replace(lines_out, "^_workflowr.yml", "\\\\texttt\\{\\\\_workflowr.yml")
# Reference figures by labels
lines_out <- str_replace_all(lines_out, "FIGURE 1", "Figure \\\\ref\\{fig:start}\\")
lines_out <- str_replace_all(lines_out, "FIGURE 2", "Figure \\\\ref\\{fig:build}\\")
lines_out <- str_replace_all(lines_out, "FIGURE 3", "Figure \\\\ref\\{fig:publish}\\")
lines_out <- str_replace_all(lines_out, "FIGURE 4", "Figure \\\\ref\\{fig:status}\\")
# Bold names of software
software <- c(
"adapr",
"archivist",
"blogdown",
"bookdown",
"cacher",
"callr",
"checkpoint",
"conda",
"devtools",
"Docker",
"drake",
"Git LFS", # Needs to come before Git
"Git",
"git2r",
"glue",
"GNU Make",
"Homebrew",
"knitr",
"Kubernetes",
"libgit2",
"msigdbr",
"packrat",
"pandoc",
"pkgdown",
"ProjectTemplate",
"rmarkdown",
"rprojroot",
"rrtools",
"RStudio",
"RSuite",
"Seurat",
"Singularity",
"Snakemake",
"Sumatra",
"switchr",
"usethis",
"workflowr",
"Workflowr"
)
for (s in software) {
lines_out <- str_replace_all(lines_out,
sprintf("[:space:](%s)\\b", s),
" \\\\textbf\\{\\1}")
# Handle the edge case where the package is the start of a new line
lines_out <- str_replace_all(lines_out,
sprintf("^(%s)\\b", s),
" \\\\textbf\\{\\1}")
# Handle the edge case where software is in parentheses
lines_out <- str_replace_all(lines_out,
sprintf("\\((%s)\\b", s),
" \\(\\\\textbf\\{\\1}")
}
writeLines(lines_out, con = fname)