Skip to content

Commit b36c24c

Browse files
committed
try to handle latex words
1 parent 987affc commit b36c24c

File tree

7 files changed

+72
-18
lines changed

7 files changed

+72
-18
lines changed

DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: WrapRmd
22
Title: RStudio Addin for Wrapping RMarkdown Paragraphs
3-
Version: 0.0.0.9005
3+
Version: 0.0.0.9006
44
Authors@R: c(
55
person("Tristan", "Mahr", , "[email protected]", role = c("aut", "cre")),
66
person("Shir", "Dekel", email = "[email protected]", role = "ctb", comment = c(ORCID = "0000-0003-1773-2446"))
@@ -20,6 +20,6 @@ Imports:
2020
Suggests:
2121
testthat
2222
LazyData: true
23-
RoxygenNote: 7.2.3
23+
RoxygenNote: 7.3.1
2424
Encoding: UTF-8
2525
Roxygen: list(markdown = TRUE)

NEWS.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# WrapRmd 0.0.0.9006
2+
3+
- LaTeX-looking words (matching `\[A-Za-z]{`) are unescaped if
4+
commonmark escaped the leading backslash.
5+
16
# WrapRmd 0.0.0.9005
27

38
- Inline math (that starts and ends with a `$` character) is treated like R

R/package.R

+37-12
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
#' without inserting linebreaks into spans of inline R code.
55
#'
66
#' @name WrapRmd
7-
#' @docType package
87
#' @import stringr
9-
NULL
8+
"_PACKAGE"
109

1110

1211
#' Wrap text but don't insert lines breaks into inline R code
@@ -191,9 +190,10 @@ md_wrap <- function(
191190
str_replace("\\n$", "")
192191

193192
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) %>%
197197
restore_escape(raw_string, "\\@ref")
198198
}
199199

@@ -232,37 +232,62 @@ restore_escape <- function(string, raw_string, target) {
232232
string
233233
}
234234

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+
235248
unescape <- function(string, raw_string, target) {
249+
esc_target <- if (inherits(target, "stringr_fixed")) {
250+
target
251+
} else {
252+
esc(target)
253+
}
254+
236255
# 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) %>%
238259
as.data.frame()
239260

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
241262
for (i in seq_along(location_in_raw$start)) {
242263
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)
244266
location_in_raw$escaped[[i]] <- escaped
245267
}
246268

247269
# 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]]
249271

250272
# Check for escapes in the wrapped text
251273
for (i in seq_along(location_in_wrapped[, 1])) {
252274
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)
254278
escaped_in_raw <- location_in_raw$escaped[[i]]
255279

256280
# If an escape was added, remove it and update target locations
257281
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]]
260284
}
261285
}
262286

263287
string
264288
}
265289

290+
266291
esc <- function(x) {
267292
paste0("\\", x)
268293
}

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# WrapRmd
22

3-
[![Travis-CI Build Status](https://travis-ci.org/tjmahr/WrapRmd.svg?branch=master)](https://travis-ci.org/tjmahr/WrapRmd)
4-
53
An [RStudio Addin](https://rstudio.github.io/rstudioaddins/) to wrap paragraphs
64
of RMarkdown text without inserting line breaks into inline R code.
75

@@ -14,7 +12,11 @@ You can install the plain version WrapRmd from GitHub with:
1412
devtools::install_github("tjmahr/WrapRmd")
1513
```
1614

17-
This package used the [commonmark](https://cran.r-project.org/web/packages/commonmark/index.html) package to wrap and reformat to markdown text. Using commonmark means that it can wrap links and markdown lists. The package does some additional work to handle inline R Markdown.
15+
This package used the
16+
[commonmark](https://cran.r-project.org/web/packages/commonmark/index.html)
17+
package to wrap and reformat to markdown text. Using commonmark means that it
18+
can wrap links and markdown lists. The package does some additional work to
19+
handle inline R Markdown.
1820

1921
## Overview
2022

man/WrapRmd.Rd

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-escaping.R

+9
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,12 @@ test_that("escape \ in math mode", {
2020
})
2121

2222

23+
test_that("no escape LaTeX", {
24+
string <-
25+
"Someone said something [@bibref-to-article], and I am doing something in
26+
\\experiment{1} to test it."
27+
28+
expect_equal(str_rmd_wrap(string, 80), string)
29+
})
30+
31+

tests/testthat/test-wrap.R

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
test_that("basic wrapping", {
22
no_code <- "regular words on a line"
3-
expect_equal(str_rmd_wrap(no_code), no_code)
3+
expect_equal(
4+
str_rmd_wrap(no_code),
5+
no_code
6+
)
47

58
long_paragraph <- "`r hello` and `r 1 + 1` and `r 1 + b + b + c` and drop a line right here `r maybe_here` `r goodbye`"
69

0 commit comments

Comments
 (0)