HenrikBengtsson / R.rsp
1
###########################################################################/**
2
# @RdocDefault compileAsciiDoc
3
#
4
# @title "Compiles an AsciiDoc file"
5
#
6
# \description{
7
#  @get "title".
8
#
9
#  \emph{NOTE: This function requires the \bold{ascii} package, which was
10
#  "removed" (archived) from CRAN on 2019-01-26.}
11
# }
12
#
13
# @synopsis
14
#
15
# \arguments{
16
#   \item{filename, path}{The filename and (optional) path of the
17
#      document to be compiled.}
18
#   \item{...}{Additional arguments passed to executable \code{ascii}
19
#     (which must be on the system search path)
20
#     called via @see "base::system2".}
21
#   \item{outPath}{The output and working directory.}
22
#   \item{postprocess}{Should the generated document be post processed or not?}
23
#   \item{verbose}{See @see "R.utils::Verbose".}
24
# }
25
#
26
# \value{
27
#   Returns the pathname of the generated document.
28
# }
29
#
30
# @author
31
#
32
# @keyword file
33
# @keyword IO
34
# @keyword internal
35
#*/###########################################################################
36
setMethodS3("compileAsciiDoc", "default", function(filename, path=NULL, ..., outPath=".", postprocess=TRUE, verbose=FALSE) {
37 0
  use("ascii", quietly=TRUE)
38

39
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
40
  # Validate arguments
41
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
42
  # Arguments 'filename' & 'path':
43 0
  pathname <- if (is.null(path)) filename else file.path(path, filename)
44 0
  if (!isUrl(pathname)) {
45 0
    pathname <- Arguments$getReadablePathname(pathname)
46
  }
47

48
  # Arguments 'outPath':
49 0
  outPath <- Arguments$getWritablePath(outPath)
50 0
  if (is.null(outPath)) outPath <- "."
51

52
  # Argument 'verbose':
53 0
  verbose <- Arguments$getVerbose(verbose)
54 0
  if (verbose) {
55 0
    pushState(verbose)
56 0
    on.exit(popState(verbose))
57
  }
58

59

60 0
  verbose && enter(verbose, "Compiling AsciiDoc noweb document")
61
  # Download URL?
62 0
  if (isUrl(pathname)) {
63 0
    verbose && enter(verbose, "Downloading URL")
64 0
    url <- pathname
65 0
    verbose && cat(verbose, "URL: ", url)
66 0
    pathname <- downloadFile(url, verbose=less(verbose,50))
67 0
    verbose && cat(verbose, "Local file: ", pathname)
68 0
    verbose && exit(verbose)
69
  }
70

71 0
  pathname <- getAbsolutePath(pathname)
72 0
  verbose && cat(verbose, "Pathname (absolute): ", pathname)
73 0
  verbose && printf(verbose, "Input file size: %g bytes\n", file.info(pathname)$size)
74 0
  verbose && cat(verbose, "Output and working directory: ", outPath)
75

76 0
  bin <- findAsciiDoc(mustExist=TRUE, verbose=less(verbose, 10))
77

78 0
  opwd <- "."
79 0
  on.exit(setwd(opwd), add=TRUE)
80 0
  if (!is.null(outPath)) {
81 0
    opwd <- setwd(outPath)
82
  }
83

84 0
  args <- c("-v")
85
##  args <- c(args, "-a data-uri")
86

87
  # Output file
88 0
  fullnameR <- gsub("[.][^.]*$", "", basename(pathname))
89 0
  extR <- "html"
90 0
  filenameR <- sprintf("%s.%s", fullnameR, extR)
91 0
  pathnameR <- filenameR
92 0
  arg <- sprintf("-o %s", pathnameR)
93 0
  args <- c(args, arg)
94

95
  # Source file
96 0
  pathname <- normalizePath(pathname)
97 0
  args <- c(args, pathname)
98

99 0
  if (verbose && isVisible(less(verbose, 5))) {
100 0
     verbose && cat(verbose, "AsciiDoc executable:")
101 0
     verbose && print(verbose, bin)
102 0
     verbose && cat(verbose, "Command-line arguments:")
103 0
     verbose && print(verbose, args)
104
  }
105

106 0
  res <- system2(bin, args=args, stderr=TRUE)
107 0
  res <- trim(res)
108 0
  if (length(res) == 0L) {
109 0
    throw("Failed to run external 'asciidoc'. No output is available.")
110
  }
111

112 0
  if (verbose && isVisible(less(verbose, 10))) {
113 0
     verbose && cat(verbose, res, collapse="\n")
114
  }
115

116
  # Locate asciidoc warnings
117 0
  pattern <- "^asciidoc: WARNING: "
118 0
  warns <- grep(pattern, res, value=TRUE)
119 0
  if (length(warns) > 0L) {
120 0
    warns <- gsub(pattern, "", warns)
121 0
    warns <- trim(warns)
122 0
    verbose && enter(verbose, sprintf("Detected %d AsciiDoc warnings", length(warns)))
123 0
    verbose && cat(verbose, warns, collapse="\n")
124 0
    for (warn in warns) warning("AsciiDoc WARNING: ", warn)
125 0
    verbose && exit(verbose)
126
  }
127

128
  # Locate output filename
129 0
  pattern <- "^asciidoc: writing: "
130 0
  pathname2 <- grep(pattern, res, value=TRUE)
131 0
  pathname2 <- gsub(pattern, "", pathname2)
132 0
  pathname2 <- trim(pathname2)
133 0
  pathname2 <- getAbsolutePath(pathname2)
134 0
  setwd(opwd); opwd <- "."
135

136 0
  res <- RspFileProduct(pathname2)
137 0
  verbose && print(verbose, res)
138

139
  # Postprocess?
140 0
  if (postprocess) {
141 0
    res <- process(res, outPath=outPath, recursive=TRUE, verbose=verbose)
142
  }
143

144 0
  verbose && exit(verbose)
145

146 0
  res
147
}) # compileAsciiDoc()

Read our documentation on viewing source code .

Loading