forked from tidyverse/ggplot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaaa-rdoc.r
232 lines (192 loc) · 5.41 KB
/
aaa-rdoc.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
# Rebuild all rdoc documentation
# Builds rdoc pages for all ggplot2 objects
#
# @arguments path to save rd files
# @keyword internal
all_rdoc_pages_create <- function(path="web/") {
Geom$all_rdoc_pages_create()
Stat$all_rdoc_pages_create()
Scale$all_rdoc_pages_create()
Coord$all_rdoc_pages_create()
Position$all_rdoc_pages_create()
Facet$all_rdoc_pages_create()
}
# Name of physical file to create, doesn't include directory
TopLevel$rdoc_path <- function (.) {
ps(.$my_name(), ".rd")
}
TopLevel$all_rdoc_pages_create <- function(., path="man/") {
invisible(lapply(.$find_all(TRUE), function(x) x$rdoc_page_create(path)))
}
TopLevel$rdoc_page_create <- function(., path="man/") {
cat("Creating rdoc documentation for", .$my_name(), "\n")
target <- ps(path, .$rdoc_path())
cat(.$rdoc_page(), file=target)
}
TopLevel$rdoc_page <- function(.) {
ps(
.$rdoc_name(),
.$rdoc_aliases(),
.$rdoc_title(),
.$rdoc_description(),
.$rdoc_details(),
.$rdoc_aesthetics(),
.$rdoc_advice(),
.$rdoc_usage(),
.$rdoc_arguments(),
.$rdoc_seealso(),
.$rdoc_value(),
.$rdoc_examples(),
.$rdoc_author(),
.$rdoc_keyword(),
""
)
}
TopLevel$rdoc_name <- function(.) {
ps(
"\\name{", .$my_name(), "}\n"
)
}
TopLevel$aliases <- c()
TopLevel$rdoc_aliases <- function(.) {
aliases <- unique(c(
.$my_name(),
.$my_names(),
.$myName(),
.$aliases
))
ps(
"\\alias{", gsub("%", "\\%", aliases), "}\n"
)
}
TopLevel$rdoc_title <- function(.) {
ps(
"\\title{", gsub("_", "\\\\_", .$my_name()), "}\n"
)
}
TopLevel$rdoc_description <- function(.) {
ps(
"\\description{", .$desc, "}\n"
)
}
TopLevel$rdoc_details <- function(.) {
ps(
"\\details{\n",
rdoc_from_html(.$details, .$my_name()),
rdoc_from_html(ps("This page describes ", .$my_name(), ", see \\code{\\link{layer}} and \\code{\\link{qplot}} for how to create a complete plot from individual components.\n")),
"}\n"
)
}
TopLevel$rdoc_aesthetics <- function(.) {
if (!exists("default_aes", .)) return("")
aes <- c(.$required_aes, names(.$default_aes()))
if (length(aes) == 0) return("")
req <- ifelse(aes %in% .$required_aes, " (\\strong{required})", "")
desc <- paste(defaults(.$desc_params, .desc_aes)[aes], req, sep="")
ps(
"\\section{Aesthetics}{\n",
rdoc_from_html(ps("The following aesthetics can be used with ", .$my_name(), ". Aesthetics are mapped to variables in the data with the aes function: \\code{", .$my_name(), "(aes(x = var))}"), .$my_name()), "\n",
"\\itemize{\n",
ps(" \\item \\code{", aes, "}: ", desc, " \n"),
"}\n",
"}\n"
)
}
TopLevel$rdoc_advice <- function(.) {
if (.$advice == "") return()
ps(
"\\section{Advice}{\n",
rdoc_from_html(.$advice, .$my_name()),
"}\n"
)
}
TopLevel$rdoc_formals <- function(.) {
if (exists("common", .) && !is.null(.$common)) {
formals(get(ps(.$class(), "_", .$common[1], "_", .$objname)))
} else {
formals(get(.$my_name()))
}
}
TopLevel$call <- function(.) {
args <- .$rdoc_formals()
is.missing.arg <- function(arg) sapply(arg, typeof) == "symbol" & sapply(arg, deparse) == ""
equals <- ifelse(is.missing.arg(args), "", "=")
ps(
.$my_names(), ps("(",
ps(names(args), equals, sapply(args, deparse), collapse=", "),
")"), collapse=NULL
)
}
# FIXME: need to generate usage statements for all common scales
TopLevel$rdoc_usage <- function (.) {
# add line breaks
call <- deparse(parse(text = .$call())[[1]])
ps(
"\\usage{", ps(call, collapse="\n"), "}\n"
)
}
TopLevel$rdoc_arguments <- function(.) {
p <- names(.$rdoc_formals())
# p <- c("mapping", "data", "stat", "position", names(.$params()), "...")
ps(
"\\arguments{\n",
ps(" \\item{", p, "}{", defaults(.$desc_params, .desc_param)[p], "}\n"),
"}\n"
)
}
TopLevel$rdoc_seealso <- function(.) {
ps(
"\\seealso{\\itemize{\n",
if(length(.$seealso) > 0) {
ps(" \\item \\code{\\link{", names(.$seealso), "}}: ", .$seealso, "\n")
},
" \\item \\url{http://had.co.nz/ggplot2/", .$html_path(), "}\n",
"}}\n"
)
}
TopLevel$rdoc_value <- function(.) {
"\\value{A \\code{\\link{layer}}}\n"
}
TopLevel$rdoc_examples <- function(.) {
if (!.$doc) return()
ps(
"\\examples{\\dontrun{\n",
ps(.$examples_text(), collapse="\n"),
"\n}}\n"
)
}
TopLevel$rdoc_author <- function(.) {
"\\author{Hadley Wickham, \\url{http://had.co.nz/}}\n"
}
TopLevel$rdoc_keyword<- function(.) {
"\\keyword{hplot}\n"
}
# rdoc auto link
# Automatically link functions used in rdoc
#
# @arguments input rdoc string
# @arguments functions to omit
# @keyword internal
rdoc_auto_link <- function(input, skip="") {
if (!exists(".links")) html_autolink_index()
for (n in names(.links)[names(.links) != skip]) {
input <- gsub(ps("\\b", n, "\\b"), ps("\\\\code{\\\\link{", n, "}}"), input)
}
input
}
# Convert rdoc to html
# Crude regexp based conversion from html to rdoc.
#
# Assumes well-formed xhtml. Also autolinks any ggplot functions.
#
# @arguments input rdoc string
# @arguments pass to \code{\link{rdoc_auto_link}}
# @keyword internal
rdoc_from_html <- function(html, skip="") {
rd <- gsub("<p>", "", html)
rd <- gsub("</p>\n?", "\n\n", rd)
rd <- gsub("<em>(.*?)</em>", "\\\\emph{\\1}", rd)
rd <- gsub("<code>(.*?)</code>", "\\\\code{\\1}", rd)
rd <- gsub("_", "\\\\_", rd)
rdoc_auto_link(rd, skip)
}