|
4 | 4 | #' without inserting linebreaks into spans of inline R code.
|
5 | 5 | #'
|
6 | 6 | #' @name WrapRmd
|
7 |
| -#' @docType package |
8 | 7 | #' @import stringr
|
9 |
| -NULL |
| 8 | +"_PACKAGE" |
10 | 9 |
|
11 | 10 |
|
12 | 11 | #' Wrap text but don't insert lines breaks into inline R code
|
@@ -191,9 +190,10 @@ md_wrap <- function(
|
191 | 190 | str_replace("\\n$", "")
|
192 | 191 |
|
193 | 192 | wrapped %>%
|
194 |
| - unescape(raw_string, "[") %>% |
195 |
| - unescape(raw_string, "]") %>% |
196 |
| - unescape(raw_string, "!") %>% |
| 193 | + unescape(raw_string, stringr::fixed("[")) %>% |
| 194 | + unescape(raw_string, stringr::fixed("]")) %>% |
| 195 | + unescape(raw_string, stringr::fixed("!")) %>% |
| 196 | + unescape_latex_words(raw_string) %>% |
197 | 197 | restore_escape(raw_string, "\\@ref")
|
198 | 198 | }
|
199 | 199 |
|
@@ -232,37 +232,62 @@ restore_escape <- function(string, raw_string, target) {
|
232 | 232 | string
|
233 | 233 | }
|
234 | 234 |
|
| 235 | +unescape_latex_words <- function(string, raw_string) { |
| 236 | + latex_words <- raw_string %>% |
| 237 | + stringr::str_extract_all("\\\\[A-Za-z]+\\{") %>% |
| 238 | + unlist() %>% |
| 239 | + unique() |
| 240 | + |
| 241 | + for (word in latex_words) { |
| 242 | + string <- unescape(string, raw_string, fixed(word)) |
| 243 | + } |
| 244 | + string |
| 245 | + |
| 246 | +} |
| 247 | + |
235 | 248 | unescape <- function(string, raw_string, target) {
|
| 249 | + esc_target <- if (inherits(target, "stringr_fixed")) { |
| 250 | + target |
| 251 | + } else { |
| 252 | + esc(target) |
| 253 | + } |
| 254 | + |
236 | 255 | # Find the target in the original string
|
237 |
| - location_in_raw <- str_locate_all(raw_string, esc(target))[[1]] %>% |
| 256 | + location_in_raw <- raw_string %>% |
| 257 | + str_locate_all(esc_target) %>% |
| 258 | + getElement(1) %>% |
238 | 259 | as.data.frame()
|
239 | 260 |
|
240 |
| - # Check each use of target to see if it was exepcted in the raw string |
| 261 | + # Check each use of target to see if it was escaped in the raw string |
241 | 262 | for (i in seq_along(location_in_raw$start)) {
|
242 | 263 | char_loc <- location_in_raw$start[[i]]
|
243 |
| - escaped <- substr(raw_string, char_loc - 1, char_loc) == esc(target) |
| 264 | + char_loc_end <- location_in_raw$end[[i]] |
| 265 | + escaped <- substr(raw_string, char_loc - 1, char_loc_end) == esc(esc_target) |
244 | 266 | location_in_raw$escaped[[i]] <- escaped
|
245 | 267 | }
|
246 | 268 |
|
247 | 269 | # Find the target in the wrapped string
|
248 |
| - location_in_wrapped <- str_locate_all(string, esc(target))[[1]] |
| 270 | + location_in_wrapped <- str_locate_all(string, esc_target)[[1]] |
249 | 271 |
|
250 | 272 | # Check for escapes in the wrapped text
|
251 | 273 | for (i in seq_along(location_in_wrapped[, 1])) {
|
252 | 274 | char_loc <- location_in_wrapped[i, 1]
|
253 |
| - escaped_in_wrapped <- substr(string, char_loc - 1, char_loc) == esc(target) |
| 275 | + char_loc_end <- location_in_wrapped[i, 2] |
| 276 | + |
| 277 | + escaped_in_wrapped <- substr(string, char_loc - 1, char_loc_end) == esc(esc_target) |
254 | 278 | escaped_in_raw <- location_in_raw$escaped[[i]]
|
255 | 279 |
|
256 | 280 | # If an escape was added, remove it and update target locations
|
257 | 281 | if (escaped_in_wrapped & !escaped_in_raw) {
|
258 |
| - str_sub(string, char_loc - 1, char_loc) <- target |
259 |
| - location_in_wrapped <- str_locate_all(string, esc(target))[[1]] |
| 282 | + str_sub(string, char_loc - 1, char_loc_end) <- target |
| 283 | + location_in_wrapped <- str_locate_all(string, esc_target)[[1]] |
260 | 284 | }
|
261 | 285 | }
|
262 | 286 |
|
263 | 287 | string
|
264 | 288 | }
|
265 | 289 |
|
| 290 | + |
266 | 291 | esc <- function(x) {
|
267 | 292 | paste0("\\", x)
|
268 | 293 | }
|
|
0 commit comments