/
format-rd.R
318 lines (292 loc) · 7.89 KB
/
format-rd.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
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
311
312
313
314
315
316
317
318
# See https://roxygen2.r-lib.org/articles/formatting.html
# Character formatting ----------------------------------------------------
#' Format Italic
#'
#' @description
#' Surround selected text with Rd markup for italic.
#'
#' @details
#' * Selected text: `text`
#' * Result of function: `\emph{text}`
#' * Example output with no text selection: `\emph{}`
#'
#' @references
#' \href{https://cran.r-project.org/doc/manuals/R-exts.html#Writing-R-documentation-files}{The \emph{Writing R Extensions} manual.}
#'
#' @export
rd_italic <- function() {
.replace_text(pre_string = "\\emph{", post_string = "}")
}
#' Format Bold
#'
#' @description
#' Surround selected text with Rd markup for bold.
#'
#' @details
#' * Selected text: `text`
#' * Result of function: `\strong{text}`
#' * Example output with no text selection: `\strong{}`
#'
#' @references
#' \href{https://cran.r-project.org/doc/manuals/R-exts.html#Writing-R-documentation-files}{The \emph{Writing R Extensions} manual.}
#'
#' @export
rd_bold <- function() {
.replace_text(pre_string = "\\strong{", post_string = "}")
}
#' Format Code
#'
#' @description
#' Surround selected text with Rd markup for inline code.
#'
#' @details
#' * User-selected text: `text`
#' * Result of function: `\code{text}`
#' * Example output with no text selection: `\code{}`
#'
#' @references
#' \href{https://cran.r-project.org/doc/manuals/R-exts.html#Writing-R-documentation-files}{The \emph{Writing R Extensions} manual.}
#'
#' @export
rd_code <- function() {
.replace_text(pre_string = "\\code{", post_string = "}")
}
#' Format Package
#'
#' @description
#' Surround selected text with Rd markup for package names.
#'
#' @details
#' * User-selected text: `package`
#' * Result of function: `\pkg{package}`
#' * Example output with no text selection: `\pkg{}`
#'
#' @references
#' \href{https://cran.r-project.org/doc/manuals/R-exts.html#Writing-R-documentation-files}{The \emph{Writing R Extensions} manual.}
#'
#' @export
rd_package <- function() {
.replace_text(pre_string = "\\pkg{", post_string = "}")
}
# Links -------------------------------------------------------------------
# Other docs ----
#' Link to Function (This Package)
#'
#' @description
#' Surround selected text with Rd markup that links to a function in the
#' current package.
#'
#' @details
#' * User-selected text: `text`
#' * Result of function: `\code{\link{text}}`
#' * Example output with no text selection: `\code{\link{}}`
#'
#' @references
#' \href{https://cran.r-project.org/doc/manuals/R-exts.html#Writing-R-documentation-files}{The \emph{Writing R Extensions} manual.}
#'
#' @export
rd_fn_this_pkg <- function() {
.replace_text(pre_string = "\\code{\\link{", post_string = "}}")
}
#' Link To Function (Another Package)
#'
#' @description
#' Surround selected text (in the form 'package::function') with Rd markup that
#' links to a function in another package.
#'
#' @details
#' * User-selected text: `package::function`
#' * Result of function: `\code{\link[package]{function}}`
#' * Example output with no text selection: `\code{\link[]{}}`
#'
#' @references
#' \href{https://cran.r-project.org/doc/manuals/R-exts.html#Writing-R-documentation-files}{The \emph{Writing R Extensions} manual.}
#'
#' @export
rd_fn_other_pkg <- function() {
.replace_text(
pre_string = "\\code{\\link[",
mid_string = "]{",
post_string = "}}"
)
}
#' Link To Function (Another Package, Show Name)
#'
#' @description
#' Surround selected text (in the form 'package::function') with Rd markup that
#' links to a function in another package and shows that package's name.
#'
#' @details
#' * User-selected text: `package::function`
#' * Result of function: `\code{\link[package:function]}`
#' * Example output with no text selection: `\code{\link[:]}`
#'
#' @references
#' \href{https://cran.r-project.org/doc/manuals/R-exts.html#Writing-R-documentation-files}{The \emph{Writing R Extensions} manual.}
#'
#' @export
rd_link_fn_other_pkg_name <- function() {
.replace_text(
pre_string = "\\code{\\link[",
mid_string = ":",
post_string = "]}"
)
}
#' Link To S4 Class
#'
#' @description
#' Surround selected text with Rd markup to link to an S4 class.
#'
#' @details
#' * User-selected text: `class`
#' * Result of function: `\linkS4class{class}`
#' * Example output with no text selection: `\linkS4class{}`
#'
#' @references
#' \href{https://cran.r-project.org/doc/manuals/R-exts.html#Writing-R-documentation-files}{The \emph{Writing R Extensions} manual.}
#'
#' @export
rd_link_s4_class <- function() {
.replace_text(pre_string = "\\linkS4class{", post_string = "}")
}
# Web ----
#' Link To Web
#'
#' @description
#' Surround selected URL text with Rd markup for a link to the web.
#'
#' @details
#' * User-selected text: `url`
#' * Result of function: `\url{url}`
#' * Example output with no text selection: `\url{}`
#'
#' @references
#' \href{https://cran.r-project.org/doc/manuals/R-exts.html#Writing-R-documentation-files}{The \emph{Writing R Extensions} manual.}
#'
#' @export
rd_url <- function() {
.replace_text(pre_string = "\\url{", post_string = "}")
}
#' Link To Web (Named)
#'
#' @description
#' Surround selected URL text with Rd markup for a link to the web and show a
#' text label.
#'
#' @details
#' * User-selected text: `url`
#' * Result of function: `\href{url}{Link text}`
#' * Example output with no text selection: `\href{}{Link text}`
#'
#' @references
#' \href{https://cran.r-project.org/doc/manuals/R-exts.html#Writing-R-documentation-files}{The \emph{Writing R Extensions} manual.}
#'
#' @export
rd_url_named <- function() {
.replace_text(pre_string = "\\href{", post_string = "}{Link text}")
}
#' Link to Email
#'
#' @description
#' Surround selected text with Rd markup for an email link.
#'
#' @details
#' * User-selected text: `address`
#' * Result of function: `\email{address}`
#' * Example output with no text selection: `\email{}`
#'
#' @references
#' \href{https://cran.r-project.org/doc/manuals/R-exts.html#Writing-R-documentation-files}{The \emph{Writing R Extensions} manual.}
#'
#' @export
rd_email <- function() {
.replace_text(pre_string = "\\email{", post_string = "}")
}
# Lists -------------------------------------------------------------------
#' Insert List (Ordered)
#'
#' @description
#' Insert an Rd markup skeleton for an ordered (numbered) list.
#'
#' @details
#' Example output:
#' ```
#' \enumerate{
#' \item First item
#' \item Second item
#' }
#'
#' ````
#'
#' @references
#' \href{https://cran.r-project.org/doc/manuals/R-exts.html#Writing-R-documentation-files}{The \emph{Writing R Extensions} manual.}
#'
#' @export
rd_list_ordered <- function() {
rstudioapi::insertText(
paste0(
"#' \\enumerate{", "\n",
"#' \\item First item", "\n",
"#' \\item Second item", "\n",
"#' }", "\n"
)
)
}
#' Insert List (Bulleted)
#'
#' @description
#' Insert an Rd markup skeleton for an unordered (bulleted) list.
#'
#' @details
#' Example output:
#' ```
#' \itemize{
#' \item First item
#' \item Second item
#' }
#'
#' ````
#'
#' @references
#' \href{https://cran.r-project.org/doc/manuals/R-exts.html#Writing-R-documentation-files}{The \emph{Writing R Extensions} manual.}
#'
#' @export
rd_list_unordered <- function() {
rstudioapi::insertText(
paste0(
"#' \\itemize{", "\n",
"#' \\item First item", "\n",
"#' \\item Second item", "\n",
"#' }", "\n"
)
)
}
#' Insert List (Named)
#'
#' @description
#' Insert an Rd markup skeleton for a definition (named) list.
#'
#' @details
#' Example output:
#' ```
#' \describe{
#' \item{One}{First item}
#' \item{Two}{Second item}
#' }
#'
#' ````
#'
#' @references
#' \href{https://cran.r-project.org/doc/manuals/R-exts.html#Writing-R-documentation-files}{The \emph{Writing R Extensions} manual.}
#'
#' @export
rd_list_definition <- function() {
rstudioapi::insertText(
paste0(
"#' \\describe{", "\n",
"#' \\item{One}{First item}", "\n",
"#' \\item{Two}{Second item}", "\n",
"#' }", "\n"
)
)
}