R/pkg-tools.R

Defines functions find_pkg_references

#' scans LaTeX document for any package references
#'
#' Usually this was handled by rjtools package, since the generated Rmd lacks
#' these commands (they are transformed to links during pandoc conversion), we
#' need to add one more step to pre-process.
#' @param input_file name of the input file
#'
#' @return pkg_meta.yaml file containing meta data, FALSE otherwise
#' @noRd
find_pkg_references <- function(input_file){
    input_file <- xfun::normalize_path(input_file)
    if(file.exists(input_file)){
        input <- readLines(input_file)
    }
    else {
        message("LaTeX file not found !")
        return(FALSE)
    }
    pkgs <- gregexpr("\\\\(CRAN|BIO)pkg\\{.+?\\}", input)
    pkgs <- mapply(
        function(pos, line) {
            if (pos[1] == -1) return(NULL)
            substr(rep_len(line, length(pos)),
                   pos, pos + pos%@%"match.length" - 1)
        },
        pkgs, input,
        SIMPLIFY = FALSE
    )
    pkgs <- unique(do.call(c, pkgs))
    pkg_is_cran <- grepl("^\\\\CRAN", pkgs)
    pkgs <- sub("\\\\(CRAN|BIO)pkg\\{(.+?)\\}$", "\\2", pkgs)
    message(paste0(
        "Detected the following packages from article:\n  ",
        "CRAN: ", paste0(pkgs[pkg_is_cran], collapse = ", "), "\n  ",
        "Bioconductor: ", paste0(pkgs[!pkg_is_cran], collapse = ", ")
    ))
    packages <- list(
        cran = pkgs[pkg_is_cran],
        bioc = pkgs[!pkg_is_cran]
    )
    pkg_yaml_path <- paste(dirname(input_file), "pkg_meta.yaml", sep = "/" )
    write_external_file(pkg_yaml_path, "w", yaml::as.yaml(packages))
}

Try the texor package in your browser

Any scripts or data that you put into this service are public.

texor documentation built on Oct. 15, 2024, 5:08 p.m.