6
6
# ' @name WrapRmd
7
7
# ' @docType package
8
8
# ' @import stringr
9
- # ' @importFrom utils head tail
10
9
NULL
11
10
12
11
13
-
14
12
# ' Wrap text but don't insert lines breaks into inline R code
15
13
# '
16
14
# ' Call this addin to wrap paragraphs in an R Markdown document.
@@ -41,7 +39,9 @@ knit_selection_addin <- function() {
41
39
text <- unlist(selection )[" text" ]
42
40
cat(
43
41
commonmark :: markdown_commonmark(
44
- knitr :: knit(text = text , quiet = TRUE )))
42
+ knitr :: knit(text = text , quiet = TRUE )
43
+ )
44
+ )
45
45
}
46
46
47
47
@@ -69,7 +69,8 @@ str_rmd_wrap <- function(string, width = getOption("WrapRmd.width", 80)) {
69
69
re_blanks_at_start ,
70
70
re_paragraph_sep ,
71
71
re_blanks_at_close ,
72
- sep = " |" )
72
+ sep = " |"
73
+ )
73
74
74
75
# Find paragraph separations
75
76
paragraph_seps <- string %> %
@@ -79,12 +80,12 @@ str_rmd_wrap <- function(string, width = getOption("WrapRmd.width", 80)) {
79
80
# Split at those points to get paragraphs.
80
81
paragraphs <- string %> %
81
82
str_split(re_start_or_sep_or_close ) %> %
82
- unlist %> %
83
+ unlist() %> %
83
84
unname()
84
85
85
86
# Wrap each paragraph.
86
87
paragraphs <- Map(function (... ) str_rmd_wrap_one(... , width ), paragraphs ) %> %
87
- unlist %> %
88
+ unlist() %> %
88
89
unname()
89
90
90
91
str_interleave(paragraphs , paragraph_seps )
@@ -97,8 +98,8 @@ str_interleave <- function(strings, interleaves) {
97
98
stopifnot(length(strings ) - length(interleaves ) == 1 )
98
99
99
100
# Pop the first string off. Concatenate pairs of interleaves and strings.
100
- start <- head(strings , 1 )
101
- left <- tail(strings , - 1 )
101
+ start <- utils :: head(strings , 1 )
102
+ left <- utils :: tail(strings , - 1 )
102
103
body <- paste0(interleaves , left , collapse = " " )
103
104
104
105
# Reattach head
@@ -109,42 +110,54 @@ str_interleave <- function(strings, interleaves) {
109
110
str_rmd_wrap_one <- function (string , width ) {
110
111
output <- string
111
112
113
+ # Patterns to protect from line breaks
112
114
re_inline_code <- " (`r)( )([^`]+`)"
115
+ re_inline_math <- " ((?!=[$])[$][^$]+[$])"
113
116
re_cross_references <- " (\\ w+\\\\ \\\\ @ref\\ (.*?\\ ))"
114
- re_nonword <- " \\ W|_"
115
117
118
+ # Locate the patterns
116
119
inline_code <- string %> %
117
120
str_extract_all(re_inline_code ) %> %
118
121
unlist()
119
122
123
+ inline_math <- string %> %
124
+ str_extract_all(re_inline_math ) %> %
125
+ unlist()
126
+
120
127
cross_references <- string %> %
121
128
str_extract_all(re_cross_references ) %> %
122
129
unlist()
123
130
131
+ # Substrings that match the patterns
124
132
wrap_ignore <- c(
125
133
inline_code ,
134
+ inline_math ,
126
135
cross_references
127
136
)
128
137
129
- # Just wrap if no code or cross-references
138
+ # Just wrap if no substrings need to be protected
130
139
if (length(wrap_ignore ) == 0 ) {
131
140
return (md_wrap(string , width ))
132
141
}
133
142
134
- # Make R code spans into long words
143
+ # Make protected strings spans into long words
135
144
136
- # I used to replace with "_" but md_wrap() escapes them as "\\_" which messes
137
- # up the line width
145
+ # Replace all nonwords and _'s with Qs
146
+ re_nonword <- " \\ W|_ "
138
147
spaceless_wrap_ignore <- str_replace_all(wrap_ignore , re_nonword , " Q" )
139
148
140
149
for (i in seq_along(wrap_ignore )) {
141
- output <- str_replace(output , coll(wrap_ignore [i ]), spaceless_wrap_ignore [i ])
150
+ output <- str_replace(
151
+ string = output ,
152
+ pattern = coll(wrap_ignore [i ]),
153
+ replacement = spaceless_wrap_ignore [i ]
154
+ )
142
155
}
143
156
144
- # Wrap
157
+ # Wrap the text now that the strings are protected
145
158
output <- md_wrap(output , width )
146
159
147
- # Put original code spans back
160
+ # Put original versions of protected strings back in
148
161
for (i in seq_along(wrap_ignore )) {
149
162
output <- stringi :: stri_replace_first_coll(
150
163
str = output ,
@@ -157,12 +170,14 @@ str_rmd_wrap_one <- function(string, width) {
157
170
}
158
171
159
172
160
- md_wrap <- function (string ,
161
- width ,
162
- hardbreaks = FALSE ,
163
- smart = getOption(" WrapRmd.smart" , FALSE ),
164
- normalize = FALSE ,
165
- extensions = getOption(" WrapRmd.extensions" , TRUE )) {
173
+ md_wrap <- function (
174
+ string ,
175
+ width ,
176
+ hardbreaks = FALSE ,
177
+ smart = getOption(" WrapRmd.smart" , FALSE ),
178
+ normalize = FALSE ,
179
+ extensions = getOption(" WrapRmd.extensions" , TRUE )
180
+ ) {
166
181
raw_string <- string
167
182
168
183
wrapped <- string %> %
@@ -171,7 +186,8 @@ md_wrap <- function(string,
171
186
extensions = extensions ,
172
187
normalize = normalize ,
173
188
smart = smart ,
174
- width = width ) %> %
189
+ width = width
190
+ ) %> %
175
191
str_replace(" \\ n$" , " " )
176
192
177
193
wrapped %> %
0 commit comments