paragraph? If NA, we're not, but we're not allowed to be
inAsIs <- FALSE # Should we show characters "as is"?
### These correspond to HTML wrappers
HTMLTags <- c("\\bold"="b",
"\\cite"="cite",
"\\code"="code",
"\\command"="code",
"\\dfn"="dfn",
"\\emph"="em",
"\\kbd"="kbd",
"\\preformatted"="pre",
# "\\special"="pre",
"\\strong"="strong",
"\\var"="var")
# These have simple substitutions
HTMLEscapes <- c("\\R"='R',
"\\cr"="
",
"\\dots"="...",
"\\ldots"="...")
## These correspond to idiosyncratic wrappers
HTMLLeft <- c("\\acronym"='',
"\\donttest"="",
"\\env"='',
"\\file"='‘',
"\\option"='',
"\\pkg"='',
"\\samp"='',
"\\sQuote"="‘",
"\\dQuote"="“",
"\\verb"='')
HTMLRight <- c("\\acronym"='
',
"\\donttest"="",
"\\env"="",
"\\file"='’',
"\\option"="",
"\\pkg"="",
"\\samp"="",
"\\sQuote"="’",
"\\dQuote"="”",
"\\verb"="")
addParaBreaks <- function(x) {
if (isBlankLineRd(x) && isTRUE(inPara)) {
inPara <<- FALSE
return("
", x) inPara <<- TRUE } x } enterPara <- function(enter = TRUE) { if (enter && isTRUE(!inPara)) { of0("
") inPara <<- TRUE } } leavePara <- function(newval) { if (isTRUE(inPara)) of0("
\n") inPara <<- newval } writeWrapped <- function(tag, block, doParas) { if (!doParas || HTMLTags[tag] == "pre") leavePara(NA) else enterPara() saveAsIs <- inAsIs asis <- !is.na(match(tag, "\\command")) if(asis) inAsIs <<- TRUE if (!isBlankRd(block)) { of0("<", HTMLTags[tag], ">") writeContent(block, tag) of0("", HTMLTags[tag], ">") } if(HTMLTags[tag] == "pre") inPara <<- FALSE if(asis) inAsIs <<- saveAsIs } writeLink <- function(tag, block, doParas) { parts <- get_link(block, tag, Rdfile) writeHref <- function() { enterPara(doParas) savePara <- inPara inPara <<- NA if (!no_links) of0('') writeContent(block, tag) if (!no_links) of1('') inPara <<- savePara } if (is.null(parts$targetfile)) { ## ---------------- \link{topic} and \link[=topic]{foo} topic <- parts$dest if (dynamic) { # never called with package="" htmlfile <- paste0("../../", urlify(package), "/help/", urlify(topic)) writeHref() return() } else { htmlfile <- NA_character_ if (!is.null(Links)) { tmp <- Links[topic] if (!is.na(tmp)) htmlfile <- tmp else { tmp <- Links2[topic] if (!is.na(tmp)) htmlfile <- tmp } } } if (is.na(htmlfile)) { ## Used to use the search engine, but we no longer have one, ## and we don't get here for dynamic help. if (!no_links) warnRd(block, Rdfile, "missing link ", sQuote(topic)) writeContent(block, tag) } else { ## treat links in the same package specially -- was needed for CHM pkg_regexp <- paste0("^../../", urlify(package), "/html/") if (grepl(pkg_regexp, htmlfile)) { htmlfile <- sub(pkg_regexp, "", htmlfile) } writeHref() } } else { ## ----------------- \link[pkg]{file} and \link[pkg:file]{bar} htmlfile <- paste0(urlify(parts$targetfile), ".html") if (!dynamic && !no_links && nzchar(pkgpath <- system.file(package = parts$pkg))) { ## check the link, only if the package is found OK <- FALSE if (!file.exists(file.path(pkgpath, "html", htmlfile))) { ## does not exist as static HTML, so look harder f <- file.path(pkgpath, "help", "paths.rds") if (file.exists(f)) { paths <- sub("\\.[Rr]d$", "", basename(readRDS(f))) OK <- parts$targetfile %in% paths } } else OK <- TRUE if (!OK) { ## so how about as a topic? file <- utils:::index.search(parts$targetfile, pkgpath) if (!length(file)) { warnRd(block, Rdfile, "file link ", sQuote(parts$targetfile), " in package ", sQuote(parts$pkg), " does not exist and so has been treated as a topic") parts$targetfile <- basename(file) } else { warnRd(block, Rdfile, "missing file link ", sQuote(parts$targetfile)) } } } if (parts$pkg == package) { ## use href = "file.html" writeHref() } else { ## href = "../../pkg/html/file.html" htmlfile <- paste0("../../", urlify(parts$pkg), "/html/", htmlfile) writeHref() } } } writeLR <- function(block, tag, doParas) { enterPara(doParas) of1(HTMLLeft[tag]) writeContent(block, tag) of1(HTMLRight[tag]) } writeDR <- function(block, tag) { if (length(block) > 1L) { of1('## Not run: ') writeContent(block, tag) of1('\n## End(Not run)') } else { of1('## Not run: ') writeContent(block, tag) } } writeBlock <- function(block, tag, blocktag) { doParas <- !(blocktag %in% c("\\tabular")) switch(tag, UNKNOWN =, VERB = of1(vhtmlify(block, inEqn)), RCODE = of1(vhtmlify(block)), TEXT = of1(if(doParas && !inAsIs) addParaBreaks(htmlify(block)) else vhtmlify(block)), USERMACRO =, "\\newcommand" =, "\\renewcommand" =, COMMENT = {}, LIST = writeContent(block, tag), "\\describe"=, "\\enumerate"=, "\\itemize" = { leavePara(FALSE) writeContent(block, tag) }, "\\bold" =, "\\cite" =, "\\code" =, "\\command" =, "\\dfn" =, "\\emph" =, "\\kbd" =, "\\preformatted" =, "\\strong" =, "\\var" = writeWrapped(tag, block, doParas), "\\special" = writeContent(block, tag), ## FIXME, verbatim? "\\linkS4class" =, "\\link" = writeLink(tag, block, doParas), ## cwhmisc has an empty \\email "\\email" = if (length(block)) { url <- paste(as.character(block), collapse="") url <- gsub("\n", "", url) enterPara(doParas) of0('', htmlify(url), '')}, ## watch out for empty URLs (TeachingDemos had one) "\\url" = if(length(block)) { url <- paste(as.character(block), collapse = "") url <- trimws(gsub("\n", "", url, fixed = TRUE, useBytes = TRUE)) enterPara(doParas) of0('', htmlify(url), '') }, "\\href" = { if(length(block[[1L]])) { url <- paste(as.character(block[[1L]]), collapse = "") url <- trimws(gsub("\n", "", url, fixed = TRUE, useBytes = TRUE)) enterPara(doParas) of0('') closing <- "" } else closing <- "" savePara <- inPara inPara <<- NA writeContent(block[[2L]], tag) of0(closing) inPara <<- savePara }, "\\Sexpr"= of0(as.character.Rd(block, deparse=TRUE)), "\\cr" =, "\\dots" =, "\\ldots" =, "\\R" = { enterPara(doParas) of1(HTMLEscapes[tag]) }, "\\acronym" =, "\\donttest" =, "\\env" =, "\\file" =, "\\option" =, "\\pkg" =, "\\samp" =, "\\sQuote" =, "\\dQuote" =, "\\verb" = writeLR(block, tag, doParas), "\\dontrun"= writeDR(block, tag), "\\enc" = writeContent(block[[1L]], tag), "\\eqn" = { enterPara(doParas) inEqn <<- TRUE of1("") block <- block[[length(block)]]; ## FIXME: space stripping needed: see Special.html writeContent(block, tag) of1("") inEqn <<- FALSE }, "\\deqn" = { inEqn <<- TRUE leavePara(TRUE) of1('')
block <- block[[length(block)]];
writeContent(block, tag)
of0('')
leavePara(FALSE)
inEqn <<- FALSE
},
"\\figure" = {
enterPara(doParas)
## This is what is needed for static html pages
if(dynamic) of1(' 1L
&& length(imgoptions <- .Rd_get_latex(block[[2]]))
&& startsWith(imgoptions, "options: ")) {
# There may be escaped percent signs within
imgoptions <- gsub("\\%", "%", imgoptions, fixed=TRUE)
of1(sub("^options: ", "", imgoptions))
} else {
of1('alt="')
writeContent(block[[length(block)]], tag)
of1('"')
}
of1(' />')
},
"\\dontshow" =,
"\\testonly" = {}, # do nothing
"\\method" =,
"\\S3method" =,
"\\S4method" = {
# Should not get here
},
"\\tabular" = writeTabular(block),
"\\subsection" = writeSection(block, tag),
"\\if" =,
"\\ifelse" =
if (testRdConditional("html", block, Rdfile))
writeContent(block[[2L]], tag)
else if (tag == "\\ifelse")
writeContent(block[[3L]], tag),
"\\out" = for (i in seq_along(block))
of1(block[[i]]),
stopRd(block, Rdfile, "Tag ", tag, " not recognized")
)
}
writeTabular <- function(table) {
format <- table[[1L]]
content <- table[[2L]]
if (length(format) != 1 || RdTags(format) != "TEXT")
stopRd(table, Rdfile, "\\tabular format must be simple text")
format <- strsplit(format[[1L]], "", fixed = TRUE)[[1L]]
if (!all(format %in% c("l", "c", "r")))
stopRd(table, Rdfile,
"Unrecognized \\tabular format: ", table[[1L]][[1L]])
format <- c(l="left", c="center", r="right")[format]
tags <- RdTags(content)
leavePara(NA)
of1('\n
') newcol <- FALSE } switch(tags[i], "\\tab" = { of1(' | ') newcol <- TRUE }, "\\cr" = { if (!newcol) of1('') of1('\n
')
inPara <<- NA
writeContent(block[[1L]], tag)
of1(' | \n\n') inPara <<- FALSE writeContent(block[[2L]], tag) leavePara(FALSE) of1(' |
") inPara <<- NA pre <- TRUE } else { inPara <<- FALSE pre <- FALSE } if (length(section)) { ## There may be an initial \n, so remove that s1 <- section[[1L]][1L] if (RdTags(section)[1] == "TEXT" && s1 == "\n") section <- section[-1L] writeContent(section, tag) } leavePara(FALSE) if (pre) of0("\n") sectionLevel <<- save } if (is.character(out)) { if (out == "") { con <- stdout() } else { con <- file(out, "wt") on.exit(close(con)) } } else { con <- out out <- summary(con)$description } Rd <- prepare_Rd(Rd, defines = defines, stages = stages, fragment = fragment, ...) Rdfile <- attr(Rd, "Rdfile") sections <- RdTags(Rd) if (fragment) { if (sections[1L] %in% names(sectionOrder)) for (i in seq_along(sections)) writeSection(Rd[[i]], sections[i]) else for (i in seq_along(sections)) writeBlock(Rd[[i]], sections[i], "") } else { name <- htmlify(Rd[[2L]][[1L]]) of0('', '', '
',name,' {', package,'}') else of0('"> | |
',name) of0(' | R Documentation |