### * checkVignettes
###
### Run a tangle+source and a weave on all vignettes of a package.
checkVignettes <-
function(package, dir, lib.loc = NULL,
tangle = TRUE, weave = TRUE, latex = FALSE,
workdir = c("tmp", "src", "cur"),
keepfiles = FALSE)
{
vigns <- pkgVignettes(package=package, dir=dir, lib.loc=lib.loc)
if(is.null(vigns)) return(NULL)
workdir <- match.arg(workdir)
wd <- getwd()
if(workdir == "tmp") {
tmpd <- tempfile("Sweave")
if(!dir.create(tmpd)) stop("unable to create temp directory ", tmpd)
setwd(tmpd)
}
else {
keepfiles <- TRUE
if(workdir == "src") setwd(vigns$dir)
}
outConn <- file(open = "w+") # anonymous tempfile
sink(outConn, type = "output")
sink(outConn, type = "message")
on.exit({
sink(type = "output")
sink(type = "message")
setwd(wd)
if(!keepfiles) unlink(tmpd, recursive = TRUE)
})
result <- list(tangle = list(), weave = list(), source = list())
for(f in vigns$docs) {
if(tangle) {
yy <- try(utils::Stangle(f, quiet=TRUE))
if(inherits(yy, "try-error"))
result$tangle[[f]] <- yy
}
if(weave) {
yy <- try(utils::Sweave(f, quiet=TRUE))
if(inherits(yy, "try-error"))
result$weave[[f]] <- yy
}
}
if(tangle) {
rfiles <- list_files_with_exts(getwd(), c("r", "s", "R", "S"))
for(f in rfiles) {
yy <- try(source(f))
if(inherits(yy, "try-error"))
result$source[[f]] <- yy
}
}
if(tangle && weave && latex) {
have.makefile <- "makefile" %in% tolower(list.files(vigns$dir))
if(!have.makefile) {
on.exit()
sink(type = "output")
sink(type = "message")
on.exit({
setwd(wd)
if(!keepfiles) unlink(tmpd, recursive = TRUE)
})
message("--- running texi2dvi on vignettes")
for(f in vigns$docs) {
f <- basename(f)
bf <- sub("\\..[^\\.]*$", "", f)
bft <- paste(bf, ".tex", sep="")
texi2dvi(file = bft, pdf = TRUE, clean = FALSE, quiet = TRUE)
}
}
}
class(result) <- "checkVignettes"
result
}
print.checkVignettes <-
function(x, ...)
{
mycat <- function(y, title) {
if(length(y)>0){
cat("\n", title, "\n\n", sep="")
for(k in 1:length(y)){
cat("File", names(y)[k], ":\n")
cat(as.character(y[[k]]), "\n")
}
}
}
mycat(x$weave, "*** Weave Errors ***")
mycat(x$tangle, "*** Tangle Errors ***")
mycat(x$source, "*** Source Errors ***")
invisible(x)
}
### * pkgVignettes
###
### Get an object of class pkgVignettes which contains a list of Sweave
### files and the name of the directory which contains them.
pkgVignettes <- function(package, dir, lib.loc = NULL)
{
## Argument handling.
if(!missing(package)) {
if(length(package) != 1)
stop("argument 'package' must be of length 1")
docdir <- file.path(.find.package(package, lib.loc), "doc")
## Using package installed in @code{dir} ...
}
else {
if(missing(dir))
stop("you must specify 'package' or 'dir'")
## Using sources from directory @code{dir} ...
if(!file_test("-d", dir))
stop(gettextf("directory '%s' does not exist", dir),
domain = NA)
else
## maybe perform tilde expansion on @code{dir}
docdir <- file.path(dirname(dir), basename(dir), "inst", "doc")
}
if(!file_test("-d", docdir)) return(NULL)
docs <- list_files_with_type(docdir, "vignette")
z <- list(docs=docs, dir=docdir)
class(z) <- "pkgVignettes"
z
}
### * buildVignettes
###
### Run a weave and pdflatex on all vignettes of a package and try to
### remove all temporary files that were created.
buildVignettes <-function(package, dir, lib.loc = NULL, quiet=TRUE)
{
vigns <- pkgVignettes(package = package, dir = dir, lib.loc = lib.loc)
if(is.null(vigns)) return(NULL)
wd <- getwd()
on.exit(setwd(wd))
setwd(vigns$dir)
origfiles <- list.files()
have.makefile <- "makefile" %in% tolower(origfiles)
pdfs <- character(0)
for(f in vigns$docs) {
f <- basename(f)
bf <- sub("\\..[^\\.]*$", "", f)
bft <- paste(bf, ".tex", sep="")
pdfs <- c(pdfs, paste(bf, ".pdf", sep=""))
tryCatch(utils::Sweave(f, quiet = quiet),
error = function(e) {
stop(gettextf("processing vignette '%s' failed with diagnostics:\n%s",
f, conditionMessage(e)),
domain = NA, call. = FALSE)
})
if(!have.makefile)
texi2dvi(file = bft, pdf = TRUE, clean = FALSE, quiet = quiet)
}
if(have.makefile) {
make <- Sys.getenv("MAKE")
yy <- system(make)
if(make == "" || yy > 0) stop("running 'make' failed")
} else {
f <- list.files()
f <- f %w/o% c(pdfs, origfiles)
file.remove(f)
}
invisible(NULL)
}
### * .build_vignette_index
vignetteMetaRE <- function(tag)
paste("[[:space:]]*%+[[:space:]]*\\\\Vignette", tag,
"\\{([^}]*)\\}", sep = "")
vignetteInfo <- function(file) {
lines <- readLines(file, warn = FALSE)
##