1
###########################################################################/**
2
# @RdocDefault findPandoc
3
#
4
# @title "Locates the pandoc executable"
5
#
6
# \description{
7
#  @get "title" on the current system.
8
# }
9
#
10
# @synopsis
11
#
12
# \arguments{
13
#   \item{mustExist}{If @TRUE, an exception is thrown if the executable
14
#      could not be located.}
15
#   \item{...}{Not used.}
16
#   \item{verbose}{See @see "R.utils::Verbose".}
17
# }
18
#
19
# \value{
20
#   Returns the pathname to the executable, or @NULL if not found.
21
# }
22
#
23
# \details{
24
#  The executable is searched for as follows:
25
#  \enumerate{
26
#   \item \code{Sys.which("pandoc")}
27
#  }
28
# }
29
#
30
# @author
31
#
32
# @keyword file
33
# @keyword IO
34
# @keyword internal
35
#*/###########################################################################
36
setMethodS3("findPandoc", "default", function(mustExist=TRUE, ..., verbose=FALSE) {
37
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
38
  # Validate arguments
39
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
40
  # Argument 'mustExist':
41 1
  mustExist <- Arguments$getLogical(mustExist)
42

43
  # Argument 'verbose':
44 1
  verbose <- Arguments$getVerbose(verbose)
45 1
  if (verbose) {
46 0
    pushState(verbose)
47 0
    on.exit(popState(verbose))
48
  }
49

50 1
  command <- "pandoc"
51

52 1
  verbose && enter(verbose, "Locating external software")
53 1
  verbose && cat(verbose, "Command: ", command)
54

55 1
  bin <- Sys.getenv("R_PANDOC")
56 1
  if (identical(bin, "")) bin <- Sys.getenv("RSTUDIO_PANDOC")
57 1
  if (identical(bin, "")) bin <- Sys.which(command)
58 0
  if (identical(bin, "")) bin <- NULL
59 0
  if (!isFile(bin)) bin <- NULL
60

61 1
  verbose && cat(verbose, "Located pathname: ", bin)
62

63 1
  if (mustExist && !isFile(bin)) {
64 0
    throw(sprintf("Failed to located external executable: '%s'", command))
65
  }
66

67
  # Validate by retrieving version information
68 1
  if (isFile(bin)) {
69 1
    output <- tryCatch({
70 1
      system2(bin, args="--version", stdout=TRUE)
71 1
    }, error = function(ex) {
72 0
      NULL
73
    })
74

75 1
    if (!is.null(output)) {
76 1
      name <- "pandoc"
77 1
      pattern <- "pandoc.* ([0-9.-]+).*"
78 1
      ver <- grep(pattern, output, value=TRUE)
79 1
      ver <- gsub(pattern, "\\1", ver)
80
      
81
      ## No matching output?
82 1
      if (length(ver) == 0) {
83 0
        stop(sprintf("Failed to infer version of %s based on captured output: ", sQuote(name), paste(dQuote(output), collapse=", ")))
84
      }
85

86
      ## Try to coerce to version objects
87 1
      ver <- numeric_version(ver, strict = FALSE)
88 1
      ver <- ver[!is.na(ver)]
89
      
90
      ## Failed to coerce?
91 1
      if (length(ver) == 0) {
92 0
        stop("Failed to parse version of %s based on captured output: ", sQuote(name), paste(dQuote(output), collapse=", "))
93
      }
94

95
      ## If more than one match, use the first one
96 1
      ver <- ver[[1]]
97

98 1
      attr(bin, "version") <- ver
99
    }
100
  }
101

102 1
  verbose && exit(verbose)
103

104 1
  bin
105
}) # findPandoc()

Read our documentation on viewing source code .

Loading