R/extract.tex.R

Defines functions extract.tex

Documented in extract.tex

#' extract.tex
#'
#' Extracts comments of the form \code{\\begin{comment}[option]{filename}...\\end{comment}} from \code{tex} files.
#'
#' @param contents string: \code{tex} file contents
#' @param pattern regular expression for [stringr::str_match_all] (default: \code{NULL})
#'
#' @return a data frame with components \code{option}, \code{file} and \code{contents}.
#' @importFrom stringr regex str_match_all
#' @export
#' @md 
#'
#' @examples
#' file  <- system.file("example.tex", package="extpro")
#' fcont <- paste0(readLines(file), collapse="\n")
#' extract.tex(fcont)
extract.tex <- function(contents, pattern=NULL) {
  # check environments
  match <- str_match_all(contents, regex("\\\\begin\\{lstlisting\\}(\\[.*?\\]){0,1}(.*?)\\\\end\\{lstlisting\\}", dotall=TRUE))[[1]]
  if (nrow(match)) {
    cat(paste0("  lstlisting: ", sapply(strsplit(match[,3], "\n", fixed=TRUE), '[', 2), "\n"))
  }
  match <- str_match_all(contents, regex("\\\\begin\\{minted\\}(\\[.*?\\]){0,1}(.*?)\\\\end\\{minted\\}", dotall=TRUE))[[1]]
  if (nrow(match)) {
    cat(paste0("  minted: ", sapply(strsplit(match[,4], "\n", fixed=TRUE), '[', 2), "\n"))
  }
  #
  if (is.null(pattern)) pattern <- regex("\\\\begin\\{comment\\}(\\[.*?\\]){0,1}\\{(.*?)\\}(.*?)\\\\end\\{comment\\}", dotall=TRUE)
  
  match <- str_match_all(contents, pattern)[[1]]
  browser()  

  data.frame(option=substr(match[,2], 2, nchar(match[,2])-1), file=match[,3], contents=match[,4], stringsAsFactors = FALSE)
}
sigbertklinke/extpro documentation built on Dec. 31, 2020, 7:26 a.m.