-
Notifications
You must be signed in to change notification settings - Fork 316
/
Rd2ex.R
181 lines (172 loc) · 6.44 KB
/
Rd2ex.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
# File src/library/tools/R/Rd2ex.R
# Part of the R package, https://www.R-project.org
#
# Copyright (C) 1995-2014 The R Core Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# A copy of the GNU General Public License is available at
# https://www.R-project.org/Licenses/
## This warns on multiple \examples sections, never fails.
Rd2ex <-
function(Rd, out="", defines=.Platform$OS.type, stages="render",
outputEncoding="UTF-8", commentDontrun = TRUE, commentDonttest = FALSE, ...)
{
WriteLines <- function(x, con, outputEncoding, ...) {
if (outputEncoding != "UTF-8") {
x <- iconv(x, "UTF-8", outputEncoding, mark=FALSE)
if (anyNA(x))
x <- iconv(x, "UTF-8", outputEncoding, sub="byte", mark=FALSE)
}
writeLines(x, con, useBytes = TRUE, ...)
}
dropNewline <- FALSE # drop next char if newline
of0 <- function(...)
of1(paste0(...))
of1 <- function(text) {
if (dropNewline && length(text)) {
text[1L] <- psub("^\n", "", text[1L])
dropNewline <<- FALSE
}
WriteLines(text, con, outputEncoding, sep = "")
}
wr <- function(x)
paste0("###", strwrap(remap(x), 73L, indent=1L, exdent=3L), collapse="\n")
remap <- function(x) {
if(!length(x)) return(x)
## \link, \var are untouched in comments: e.g. is.R
x <- psub("\\\\(link|var)\\{([^}]+)\\}", "\\2", x)
## not valid in perl: use lookbehind instead.
## x <- gsub("(^|[^\\])\\\\([%{])", "\\1\\2", x)
x <- psub("(?<!\\\\)\\\\([%{])", "\\1", x)
x <- psub("\\\\(l|)dots", "...", x)
## FIXME: Previously said "Want to leave file bytes unchanged"
x
}
render <- function(x, prefix = "")
{
renderDont <- function(txt, comment, label=TRUE, xtra1=comment) {
if (label)
of0("## ", txt, ": ")
## Special case for one line.
if (xtra1 && length(x) == 1L) {
render(x[[1L]], prefix)
} else {
if (!grepl("^\n", x[[1L]][1L], perl = TRUE) &&
RdTags(x)[1L] != "COMMENT") {
writeLines("", con)
render(x[[1L]], paste0(if (comment) "##D ", prefix))
} else render(x[[1L]], prefix)
for(i in seq_along(x)[-1]) ## `` i in 2:length(x) ''
render(x[[i]], paste0(if (comment) "##D ", prefix))
last <- x[[length(x)]]
if (!grepl("\n$", last[length(last)], perl = TRUE))
writeLines("", con)
if (label)
of0("## End(",txt,")")
}
}
tag <- attr(x, "Rd_tag")
if(tag %in% c("\\dontshow", "\\testonly")) {
renderDont("Don't show", comment=FALSE)
} else if (tag == "\\dontrun") {
renderDont("Not run", commentDontrun, label=commentDontrun)
} else if (tag == "\\donttest") {
renderDont("No test", commentDonttest, xtra1=FALSE)
} else if (tag == "COMMENT") {
## % can escape a whole line (e.g. beavers.Rd) or
## be trailing when we want a NL
## This is not right (leading spaces?) but it may do
if(attr(x, "srcref")[2L] == 1L) dropNewline <<- TRUE
} else if (tag %in% c("\\dots", "\\ldots")) {
of1("...")
} else if (tag == "\\if" || tag == "\\ifelse") {
if (testRdConditional("example", x, Rdfile))
for(i in seq_along(x[[2L]])) render(x[[2L]][[i]], prefix)
else if (tag == "\\ifelse")
for(i in seq_along(x[[3L]])) render(x[[3L]][[i]], prefix)
} else if (tag == "\\out") {
for (i in seq_along(x))
of1(x[[i]])
} else if (tag %in% c("USERMACRO", "\\newcommand", "\\renewcommand")) {
# do nothing
} else {
txt <- unlist(x)
of0(prefix, remap(txt))
}
}
Rd <- prepare_Rd(Rd, defines=defines, stages=stages, ...)
Rdfile <- attr(Rd, "Rdfile")
sections <- RdTags(Rd)
## FIXME should we skip empty \examples sections?
where <- which(sections == "\\examples")
if(length(where)) {
if (is.character(out)) {
if(out == "") {
con <- stdout()
} else {
con <- file(out, "wt")
on.exit(close(con))
}
} else {
con <- out
out <- summary(con)$description
}
if(length(where) > 1L)
warning("more than one \\examples section, using the first")
ex <- Rd[[ where[1L] ]]
exl <- unlist(ex)
## Do we need to output an encoding?
if(length(exl) && any(Encoding(exl) != "unknown")) {
if(any(f <- sections == "\\encoding")) {
encoding <- unlist(Rd[which(f)])[1L]
## FIXME: which should win here?
if(nzchar(outputEncoding))
encoding <- outputEncoding
else
outputEncoding <- encoding
of0("### Encoding: ", encoding, "\n\n") #
}
}
nameblk <- sections == "\\name"
if (any(nameblk)) {
## perl wrapped here, but it seems unnecessary
name <- as.character(Rd[[ which.max(nameblk) ]])
of0("### Name: ", name, "\n")
}
title <- .Rd_format_title(.Rd_get_title(Rd))
if (!length(title))
title <- "No title found"
of0(wr(paste0("Title: ", title)), "\n")
aliasblks <- sections == "\\alias"
if (any(aliasblks)) {
aliases <- unlist(Rd[aliasblks])
sp <- grep(" ", aliases, fixed = TRUE)
aliases[sp] <- paste0("'", aliases[sp], "'")
of0(wr(paste0("Aliases: ", paste(aliases, collapse=" "))),
"\n")
}
keyblks <- sections == "\\keyword"
if (any(keyblks)) {
## some people have only empty keyword blocks.
keys <- unlist(Rd[keyblks])
if(length(keys)) {
keys <- psub("^\\s+", "", keys)
of0(wr(paste("Keywords: ",
paste0(keys, collapse=" "))), "\n")
}
}
writeLines(c("", "### ** Examples"), con)
for (i in seq_along(ex)) render(ex[[i]])
of1("\n\n\n")
}
invisible(out)
}