File: buildNonSweaveVignette.R

package info (click to toggle)
r-cran-r.rsp 0.46.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,472 kB
  • sloc: javascript: 612; tcl: 304; sh: 18; makefile: 16
file content (422 lines) | stat: -rw-r--r-- 11,151 bytes parent folder | download
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
###########################################################################/**
# @RdocFunction parseVignette
#
# @title "Parses an Rnw file"
#
# \description{
#  @get "title".
# }
#
# @synopsis
#
# \arguments{
#   \item{pathname}{The Rnw file to be parsed.}
#   \item{commentPrefix}{A regular expression specifying the prefix
#     pattern of vignette comments.}
#   \item{final}{If @TRUE, the output PDF or HTML file is also located.}
#   \item{source}{If @TRUE, the output R source code file is also located.}
#   \item{maxLines}{The maximum number of lines to scan.}
#   \item{...}{Not used.}
# }
#
# \value{
#   Returns a named @list or NULL if a non-vignette.
# }
#
# @author
#
# @keyword file
# @keyword IO
# @keyword internal
#*/###########################################################################
parseVignette <- function(pathname, commentPrefix="^[ \t]*%[ \t]*", final=FALSE, source=FALSE, maxLines=-1L, ...) {
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Local functions
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  findOutput <- function(pathname, pattern) {
     path <- dirname(pathname)
     filename <- basename(pathname)
     ext <- gsub(".*[.]([^.]*)$", "\\1", filename)

     # All available output files
     filenames <- list.files(path=path, pattern=pattern)
     fullnames <- gsub("[.][^.]*$", "", filenames)
     patterns <- sprintf("^%s.*[.]%s$", fullnames, ext)
     keep <- (unlist(lapply(patterns, FUN=regexpr, filename), use.names=FALSE) != -1L)
     filenames <- filenames[keep]
     if (length(filenames) == 0L) return(NULL)

     # Order by decreasing filename lengths
     o <- order(nchar(filenames), decreasing=TRUE)
     filenames <- filenames[o]
     file.path(path, filenames)
  }


  if (!file.exists(pathname)) {
    stop("Cannot build vignette. File not found: ", pathname)
  }

  bfr <- readLines(pathname, warn=FALSE, n=maxLines)

  # Parse for "\Vignette" options
  pattern <- sprintf("%s\\\\Vignette(.*)\\{(.*)\\}", commentPrefix)
  rows <- which(regexpr(pattern, bfr) != -1L)
  bfr <- bfr[rows]

  # Nothing found?
  if (length(bfr) == 0L) {
    return(NULL)
  }

  # If the first entry is not among the first 20 rows, assume the ones
  # founds are part of the text document such entries rather than entries
  # used for the vignette itself.
  if (rows[1L] > 20L) {
    return(NULL)
  }

  opts <- grep(pattern, bfr, value=TRUE)
  keys <- gsub(pattern, "\\1", opts)
  values <- gsub(pattern, "\\2", opts)
  names(values) <- keys
  opts <- as.list(values)

  # Drop duplicated entries, assuming the first ones are the intended
  # ones.  The extra ones may happen when a vignette documents how to
  # use %\\VignetteNnn{} markup.
  keep <- !duplicated(names(opts))
  opts <- opts[keep]

  # No %\VignetteIndexEntry{}?
  if (!is.element("IndexEntry", names(values))) {
    return(NULL)
  }

  vign <- c(list(pathname=pathname), opts)


  # Look for a generated PDF or HTML file?
  if (final) {
     output <- findOutput(pathname, pattern="[.](pdf|PDF|html|HTML)$")
     if (length(output) == 0L) {
       stop("Failed to located PDF or HTML output file for vignette: ", pathname)
     } else if (length(output) > 1L) {
       stop("Located more than one PDF or HTML output file for vignette: ", pathname)
     }
     vign$final <- output
  }

  # Look for a generated R source code file?
  if (source) {
     output <- findOutput(pathname, pattern="[.][rRsS]$")
     if (length(output) > 1L) {
       output <- output[1L]
     }
     vign$source <- output
  }

  # Assert unique entries
  names <- names(vign)
  dups <- names[duplicated(names)]
  if (length(dups) > 0L) {
    throw("Duplicated entries detected: ", paste(dups, collapse=", "))
  }

  vign
} # parseVignette()



###########################################################################/**
# @RdocFunction parseVignettes
#
# @title "Locates and parses all vignettes"
#
# \description{
#  @get "title".
# }
#
# @synopsis
#
# \arguments{
#   \item{path}{The directory where to search for vignettes.}
#   \item{pattern}{Filename pattern to locate vignettes.}
#   \item{...}{Additional arguments passed to @see "parseVignette".}
#   \item{drop}{A @vector of filename patterns of vignette sources
#    to be ignored.}
# }
#
# \value{
#   Returns a @list where each element corresponds to an
#   identified vignette source file.  A file is considered to be
#   a vignette source file if it has \code{\\Vignette.*\{\}} markups
#   in the top 50 lines.
#   Each such vignette element consists of a named @list with
#   the parse \code{\\Vignette.*\{\}} information.
# }
#
# @author
#
# @keyword file
# @keyword IO
# @keyword internal
#*/###########################################################################
parseVignettes <- function(path=".", pattern="[.][^.~]*$", ..., drop="^dummy.tex$") {
  pathnames <- list.files(path=path, pattern=pattern, full.names=TRUE)

  # Ignore certain files, e.g. "^dummy.Rnw$"?
  if (length(drop) > 0L) {
    filenames <- basename(pathnames)
    excl <- rep(FALSE, times=length(filenames))
    for (pattern in drop) {
      excl <- excl | (regexpr(pattern, filenames) != -1L)
    }
    pathnames <- pathnames[!excl]
  }

  vigns <- list()
  for (kk in seq_along(pathnames)) {
    pathname <- pathnames[kk]
    vign <- parseVignette(pathname, ...)
    if (length(vign) == 0L)
       next
    vigns[[pathname]] <- vign
  }

  vigns
} # parseVignettes()



###########################################################################/**
# @RdocFunction buildNonSweaveVignette
#
# @title "Builds a non-Sweave Rnw vignette"
#
# \description{
#  @get "title".
# }
#
# @synopsis
#
# \arguments{
#   \item{...}{Not used.}
# }
#
# \value{
#   Returns (invisibly) a named @list.
# }
#
# @author
#
# @keyword file
# @keyword IO
# @keyword internal
#*/###########################################################################
buildNonSweaveVignette <- function(vign, envir=new.env(), ...) {
  # Local functions
  SweaveStangle <- function(file, ...) {
    pathnameR <- utils::Sweave(file, ...)
    utils::Stangle(file, ...)
    pathnameR
  }

  # A filename?
  if (is.character(vign)) {
    pathname <- vign
    vign <- parseVignette(pathname, ...)
  }

  pathname <- vign$pathname

  # Load required packages
  if (!is.null(vign$Depends)) {
    pkgNames <- vign$Depends
    pkgNames <- unlist(strsplit(pkgNames, split=",", fixed=TRUE), use.names=FALSE)
    pkgNames <- gsub("(^[ \t]*|[ \t]*$)", "", pkgNames)
    for (pkgName in pkgNames) {
      library(pkgName, character.only=TRUE)
    }
  }

  # Build vignette according to \VignetteBuild{} command
  if (!is.null(cmd <- vign$Engine) && nchar(cmd) > 0L) {
    # Retrieve the "engine" according to \VignetteEngine{} expression
    res <- get(cmd, envir=envir, mode="function")
  } else if (!is.null(cmd <- vign$Build) && nchar(cmd) > 0L) {
    # Parse \VignetteBuild{} expression
    tryCatch({
      expr <- parse(text=cmd)
    }, error = function(ex) {
      stop(sprintf("Syntax error in \\VignetteBuild{%s}: %s", cmd, ex$message))
    })

    # Evaluate \VignetteBuild{} expression
    res <- eval(expr, enclos = baseenv())
  } else {
     # If not specified, assume Sweave
     res <- SweaveStangle
  }

  # Was a function specified?
  if (is.function(res)) {
    fcn <- res
    res <- fcn(pathname)
  }

  invisible(res)
} # buildNonSweaveVignette()



###########################################################################/**
# @RdocFunction buildNonSweaveVignettes
#
# @title "Builds all non-Sweave Rnw vignette"
#
# \description{
#  @get "title".
# }
#
# @synopsis
#
# \arguments{
#   \item{...}{Additional arguments passed to @see "buildNonSweaveVignette".}
# }
#
# \value{
#   Returns (invisibly) a named @list with elements of what
#   the vignette builder returns.
# }
#
# @author
#
# \seealso{
#   To build vignette, see @see "buildNonSweaveVignette".
# }
#
# @keyword file
# @keyword IO
# @keyword internal
#*/###########################################################################
buildNonSweaveVignettes <- function(...) {
  vigns <- parseVignettes(...)
  if (length(vigns) > 0L) {
     envir <- new.env()
     path <- system.file("doc", "templates", package="R.rsp")
     path <- c(path, dirname(vigns[[1L]]$pathname))
     pathnames <- file.path(path, "enginesMap.R")
     pathnames <- pathnames[file_test("-f", pathnames)]
     for (pathname in pathnames) {
       expr <- parse(pathname)
       eval(expr, envir = envir, enclos = baseenv())
     }
  }
  for (kk in seq_along(vigns)) {
    vign <- vigns[[kk]]
    vign$result <- buildNonSweaveVignette(vign, envir=envir, ...)
    vigns[[kk]] <- vign
  }
  invisible(vigns)
} # buildNonSweaveVignettes()



###########################################################################/**
# @RdocFunction buildNonSweaveTexToPdf
#
# @title "Compiles all TeX files into PDFs"
#
# \description{
#  @get "title", unless already done.
# }
#
# @synopsis
#
# \arguments{
#   \item{path}{The directory where to search for TeX files.}
#   \item{pattern}{Filename pattern to locate TeX files.}
#   \item{...}{Additional arguments passed to @see "tools::texi2pdf".}
# }
#
# \value{
#   Returns (invisibly) a named @list of results.
# }
#
# @author
#
# @keyword file
# @keyword IO
# @keyword internal
#*/###########################################################################
buildNonSweaveTexToPdf <- function(path=".", pattern="[.](tex|ltx)$", ...) {
  pathnames <- list.files(path=path, pattern=pattern, full.names=TRUE)

  # Ignore dummy.tex (which is created by R from dummy.Rnw just before make)
  keep <- !is.element(basename(pathnames), c("dummy.tex"))
  pathnames <- pathnames[keep]

  res <- list()
  for (pathname in pathnames) {
    pathnamePDF <- sprintf("%s.pdf", gsub(pattern, "", pathname))
    if (!isFile(pathnamePDF)) {
       res[[pathname]] <- texi2pdf(file=pathname, ...)
    }
  }
  invisible(res)
} # buildNonSweaveTexToPdf()


###########################################################################/**
# @RdocFunction buildPkgIndexHtml
#
# @title "Builds a package index HTML file"
#
# \description{
#  @get "title", iff missing.
# }
#
# @synopsis
#
# \arguments{
#   \item{...}{Not used.}
# }
#
# \value{
#   Returns (invisibly) the absolute pathname to the built index.html file.
#   If an index.html file already exists, nothing is done and @NULL
#   is returned.
# }
#
# @author
#
# @keyword file
# @keyword IO
# @keyword internal
#*/###########################################################################
buildPkgIndexHtml <- function(...) {
  # Nothing to do?
  if (file.exists("index.html")) {
    return(NULL)
  }

  library("R.rsp")

  filename <- "index.html.rsp"
  if (!file.exists(filename)) {
    # If not custom index.html.rsp exists, use the one of the R.rsp package
    path <- system.file("doc/templates", package="R.rsp")
    pathname <- file.path(path, filename)
    file.copy(pathname, to=".")
    on.exit({
      file.remove(filename)
    })
  }

  # Sanity check
  stop_if_not(file.exists(filename))

  # Build index.html
  rfile(filename)
} # buildPkgIndexHtml()