-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathconvertL2M.r
More file actions
310 lines (272 loc) · 10.4 KB
/
convertL2M.r
File metadata and controls
310 lines (272 loc) · 10.4 KB
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
# Function to partially convert a LaTeX knitr file to Markdown
#
# Usage:
# require(Hmisc)
# getRs('convertL2M.r') # gets from github
# convertL2M('my.Rnw', 'my.Rmd')
# Add internalize='directory.../shared.R' to move external R code
# chunks to inside the new code chunks
#
# Must be done manually:
# before running: make sure chunk headers are all on one input line
# look for \ commands split over multiple lines, especially \emph{}
# Fix all \item entries to occupy a single text line
# Likewise for \footnote \chapter \section \subsection \subsubsection
# If you define \label{} for a section heading make sure it is on the
# same line as the \section{} or similar command
# After running:
# Edit lines marked with <!-- ?---> and look for closing } to edit
# Edit translations from \begin{tabular} to markdown tables to fine tune
# Edit \hspace to ~ within equations, edit \textbf
# Put $$ on same line as equation
# Add fig.align='left', lang='markdown' in knitrSet()
# Change author names @ref ?
# See ~/r/rmarkdown/style.txt (insert in new .Rmd file) and bookfun.r
# This function uses translation regular expressions in
# https://github.com/harrelfe/rscripts/blob/master/convertL2M.rex
## Notes
## If cache=TRUE figure captions may not be created
## Set ipacue=FALSE to not generate `r ipacue()` at each
## top-level \being{itemize,enumerate,description}
convertL2M <- function(file, out='', transtab=NULL, ipacue=TRUE, rsetup=TRUE,
internalize=NULL, verbose=FALSE) {
if(! length(transtab))
transtab <- 'https://raw.githubusercontent.com/harrelfe/rscripts/master/convertL2M.rex'
trans <- readLines(transtab)
n <- length(trans)
if((n %% 3) != 0)
stop('convertL2M.rex has # lines that is not a multiple of 3')
typet <- trans[seq(1, n, by=3)] # blank or w (warning)
from <- trans[seq(2, n, by=3)]
to <- trans[seq(3, n, by=3)]
nt <- length(to)
w <- readLines(file)
s <- trimws(w, which='right') # lines in file with no white space on right
## Intercept \cite{foo1,foo2,foo3} -> [@foo1; @foo2; @foo3]
s <- gsub('~\\\\cite', '\\\\cite', s)
j <- grep('\\cite[a-z]*\\{', s)
if(any(j)) for(k in j) {
x <- s[k]
inside <- sub('.*\\\\cite[a-z]*\\{(.*?)\\}.*', '\\1', x)
tto <- sub(',', '; @', paste0('[@', inside, ']'))
ex <- paste0('\\\\cite[a-z]*\\{', inside, '\\}')
x <- sub(ex, tto, x)
s[k] <- x
}
## Do main translations
for(i in 1 : nt)
if(typet[i] != 'w') s <- gsub(from[i], to[i], s)
## Delayed conversion of back ticks to handle regular LaTeX string quoting
s <- gsub('BACKTICK', '`', s)
## For some reason neither \n nor \\n worked in convertL2M.dat
s <- gsub('NEWLINE', '\n', s)
st <- trimws(s) # version of s with no white space on left either
nestingi <- cumsum(st == '\\bi') - cumsum(st == '\\ei')
nestinge <- cumsum(st == '\\be') - cumsum(st == '\\ee')
n <- length(nestingi)
if(nestingi[n] != 0) warning('itemize environment not closed')
if(nestinge[n] != 0) warning('enumerate environment not closed')
## Compute degree of nesting ignoring whether iti was \bi or \be
nestingie <- cumsum(st %in% c('\\bi', '\\be')) -
cumsum(st %in% c('\\ei', '\\ee'))
## If ipacue=TRUE, for every top level \bi or \be insert a new
## line with `r ipacue()` at end of next line
if(ipacue) {
i <- which(st %in% c('\\bi', '\\be') & nestingie == 1)
if(any(i)) for(j in i) s[j + 1] <- paste(s[j + 1], '`r ipacue()`')
}
## For every \bi find line number of matching \ei
## For every line determine current environment
openedi <- grep('\\\\bi$', st)
closedi <- integer(0)
for(i in openedi) {
ns <- nestingi[i]
## match is first \ei with nesting level ns - 1
poss <- (i + 1) : n
closedi <- c(closedi, min(poss[nestingi[poss] == ns -1]))
}
## Likewise for \be \ee
openede <- grep('\\\\be$', st)
closede <- integer(0)
for(i in openede) {
ns <- nestinge[i]
## match is first \ee with nesting level ns - 1
poss <- (i + 1) : n
closede <- c(closede, min(poss[nestinge[poss] == ns -1]))
}
## Put all line numbers of openings into a single sorted vector
## along with indicator of i vs e
opened <- c(openedi, openede)
closed <- c(closedi, closede)
ttype <- c(rep('i', length(openedi)), rep('e', length(openede)))
i <- order(opened)
opened <- opened[i]
closed <- closed[i]
ttype <- ttype[i]
item <- grep('\\\\item', st)
n <- length(st)
type <- rep(' ', n)
mx <- function(x, cond) if(any(cond)) max(x[cond]) else 0
## For each line containing \item find the last opened environment
## that was not closed between the opening and the \item
for(j in item) {
## Find latest environment that was not closed before current line
lasto <- mx(opened, opened < j & closed > j)
if(lasto == 0) stop(paste('\\item encountered that is not after beginning of an itemize or enumerate environment', '\nj=', j, '\nitem[j]=', item[j]))
type[j] <- ttype[which(opened == lasto)]
}
## cat(paste(type, nestingi, nestinge, st), sep='\n', file='/tmp/a')
if(any(type[item] == ' ')) {
warning(paste('could not determine environment in lines',
paste(item[type[item] == ' '], collapse=' ')))
cat('Offending lines including 1 line before and 1 after each:\n')
lines <- item[type[item] == ' ']
lines <- sort(c(lines - 1, lines, lines + 1))
cat(st[lines], sep='\n')
}
nesti <- c('* ', ' + ', ' - ')
neste <- c('1. ', ' i) ', ' A. ')
if(max(nestingi, nestinge) > 3)
stop('does not handle itemized or enumerated lists deeper than 3 levels')
z <- s
for(i in item) {
## cat(i, type[i], nestinge[i], neste[nestinge[i]], '\n', z[i], '\n')
if(type[i] != ' ')
z[i] <-
switch(type[i],
i = gsub('\\\\item\\s+', nesti[nestingi[i]], trimws(z[i])),
e = gsub('\\\\item\\s+', neste[nestinge[i]], trimws(z[i]))
)
}
if(length(internalize)) {
ext <- readLines(internalize)
ext <- sub('#\\s*Fig.*\\s\\(\\*.*\\*\\)', '', ext)
ext <- ext[ext != '']
le <- length(ext)
ecn <- character(le)
for(j in 1 : le) {
# cat(j, grepl('^##\\s@knitr', ext[j]), '\n')
# Carry forward chunk names, extracted from ## @knitr chunkname
if(grepl('^##\\s@knitr', ext[j]))
ecn[j : le] <- sub('##\\s@knitr\\s(.*)', '\\1', ext[j])
}
ext <- split(ext, ecn)
ext <- lapply(ext, function(x) x[-1]) # remove first entry
if(verbose) cat('\nExternal chunks found:',
paste(names(ext), collapse=', '), '\nFirst chunk:\n',
paste(ext[[1]], collapse='\n'), '\n')
}
notchunkheader <- ! grepl('^```\\{r', z)
z[notchunkheader] <- gsub('\\\\\\\\', '<br>', z[notchunkheader])
# For every chunk header whose name is in internalize, expand the
# chunk to include lines in internalize
if(length(internalize)) {
cnames <- character(0)
for(j in 1 : length(z)) {
if(grepl('^\n*```\\{r', z[j])) {
cname <- sub('^\n*```\\{r\\s*', '', z[j])
cname <- sub('(\\w)[,}].*', '\\1', cname)
cname <- strsplit(cname, split=c('}', ','), fixed=TRUE)[[1]][1]
if(verbose) cat('Processing chunk', cname, '\n')
if(cname %in% names(ext)) {
cnames <- c(cnames, cname)
if(verbose) cat('Expanded into\n')
z[j] <- paste(z[j], paste(ext[[cname]], collapse='\n'), sep='\n')
if(verbose) cat(z[j], '\n')
}
}
}
cat('\nExternal chunks not used:',
paste(setdiff(names(ext), cnames), collapse=','), '\n')
# Convert z back into multiple lines
z <- unlist(strsplit(z, '\n'))
}
## For all array environments convert <br> back to \\
## Also replace {ccc} with correct number if needed
ar <- grep('\\\\begin\\{array', z)
are <- grep('\\\\end\\{array', z)
if(length(ar)) {
for(i in ar) {
## Find end of environment
j <- min(are[are >= i])
z[i : j] <- gsub('<br>', '\\\\\\\\', z[i : j])
## Find first line of table and count # fields
w <- z[i + 1]
w <- substring(w, 1 : nchar(w), 1 : nchar(w))
fields <- sum(w == '&') + 1
ccs <- paste(rep('c', fields), collapse='')
if(fields != 3)
z[i] <- gsub('\\{ccc\\}', paste0('{', ccs, '}'), z[i])
}
}
## Do simple fix on tabular environments
tab <- grep('\\\\begin\\{tabular', z)
tabe <- grep('\\\\end\\{tabular', z)
if(length(tab))
for(i in tab) {
z[i] <- ''
## Find end of environment
j <- min(tabe[tabe >= i])
z[i : j] <- gsub('\\\\\\\\', ' ', z[i : j])
w <- z[(i + 1) : (j - 1)]
fields <- length(strsplit(w[1], split='&')[[1]])
w <- paste('|', gsub('&', '|', w), '|')
w <- gsub('\\\\hline', '', w)
w <- gsub('<br>', '', w)
w <- gsub('~', '', w)
w[1] <- paste0(w[1], '\n|', paste(rep('-----', fields), collapse='|'),
'|')
z[(i + 1) : (j - 1)] <- w
z[j] <- ''
}
## Flag warnings but only outside array environment
wrn <- integer(0)
for(f in from[typet == 'w'])
wrn <- c(wrn, grep(f, z))
wrn <- setdiff(wrn,
c(grep('\\\\begin\\{array\\}', z),
grep('\\\\end\\{array\\}', z)))
if(length(wrn))
for(i in 1 : length(wrn))
if(any(ar >= wrn[i] & are <= wrn[i])) wrn[i] <- 0
wrn <- setdiff(wrn, 0)
if(length(wrn)) {
wrn <- unique(wrn)
z[wrn] <- paste(z[wrn], '<!-- ?--->')
}
## Remove now unnecessary environment begin and ends
z <- gsub('\\\\bi$', '', z)
z <- gsub('\\\\be$', '', z)
i <- c(grep('\\\\ei$', z), grep('\\\\ee$', z))
## if(length(i)) z <- z[- i]
if(length(i)) z[i] <- ''
## Remove any blank line preceeding a > 1st level list
p <- c('^ \\+ ', '^ -',
'^ i\\) ', '^ A\\. ')
for(k in p) {
i <- grep(k, z)
if(length(i))
for(j in i)
if(trimws(z[j-1]) == '') z[j-1] <- '**DELETE**'
}
z <- z[z != '**DELETE**']
bt <- paste(rep("`", 3), collapse='')
if(rsetup)
z <-
c("```{r include=FALSE}",
"require(Hmisc)",
"options(qproject='rms', prType='html')",
"getRs('reptools.r')",
"getRs('qbookfun.r')",
"hookaddcap()",
"knitr::set_alias(w = 'fig.width', h = 'fig.height', cap = 'fig.cap', scap ='fig.scap')",
"```",
"",
z,
"",
"```{r echo=FALSE}",
"# saveCap('')",
"```")
cat(z, sep='\n', file=out)
invisible()
}