HenrikBengtsson / R.rsp
1
###########################################################################/**
2
# @RdocDefault compileMarkdown
3
#
4
# @title "Compiles a Markdown file"
5
#
6
# \description{
7
#  @get "title" to HTML.
8
# }
9
#
10
# @synopsis
11
#
12
# \arguments{
13
#   \item{filename, path}{The filename and (optional) path of the
14
#      Markdown document to be compiled.}
15
#   \item{...}{Additional arguments passed to @see "markdown::markdownToHTML".}
16
#   \item{outPath}{The output and working directory.}
17
#   \item{header}{@character @vector of valid HTML strings that should be added to the HTML <head> section.}
18
#   \item{metadata}{A named @list with meta data that will add as <meta> tags in the HTML <head> section.}
19
#   \item{verbose}{See @see "R.utils::Verbose".}
20
# }
21
#
22
# \value{
23
#   Returns the pathname of the generated HTML document.
24
# }
25
#
26
# \details{
27
#   The input encoding is based on \code{getOption("encoding")} and
28
#   the output encoding is \code{"UTF-8"}
29
#   (forced by @see "markdown::markdownToHTML").
30
# }
31
#
32
# @author
33
#
34
# \seealso{
35
#   Internally, @see "markdown::markdownToHTML" is used.
36
# }
37
#
38
# @keyword file
39
# @keyword IO
40
# @keyword internal
41
#*/###########################################################################
42
setMethodS3("compileMarkdown", "default", function(filename, path=NULL, ..., outPath=".", header=NULL, metadata=getMetadata(filename), verbose=FALSE) {
43

44
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
45
  # Validate arguments
46
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
47
  # Arguments 'filename' & 'path':
48 1
  pathname <- if (is.null(path)) filename else file.path(path, filename)
49 1
  if (!isUrl(pathname)) {
50 1
    pathname <- Arguments$getReadablePathname(pathname)
51
  }
52

53
  # Arguments 'outPath':
54 1
  outPath <- Arguments$getWritablePath(outPath)
55 1
  if (is.null(outPath)) outPath <- "."
56

57
  # Argument 'metadata':
58 0
  if (is.null(metadata)) metadata <- list()
59 1
  stop_if_not(is.list(metadata))
60

61
  # Argument 'verbose':
62 1
  verbose <- Arguments$getVerbose(verbose)
63 1
  if (verbose) {
64 1
    pushState(verbose)
65 1
    on.exit(popState(verbose))
66
  }
67

68
  # Arguments '...':
69

70

71 1
  verbose && enter(verbose, "Compiling Markdown document")
72

73
  ## Incorporate meta data into HTML header?
74 1
  if (length(metadata) > 0L) {
75 1
    keywords <- metadata[["keywords"]]
76 1
    if (length(keywords) > 0L) {
77 0
      keywords <- paste(keywords, collapse=", ")
78 0
      headerT <- sprintf('<meta name="keywords" content="%s">', keywords)
79 0
      header <- c(header, headerT)
80
    }
81

82 1
    author <- metadata[["author"]]
83 1
    if (length(author) > 0L) {
84 0
      author <- paste(author, collapse=", ")
85 0
      headerT <- sprintf('<meta name="author" content="%s">', author)
86 0
      header <- c(header, headerT)
87
    }
88
  }
89 1
  header <- paste(header, collapse="\n")
90

91

92
  # Download URL?
93 1
  if (isUrl(pathname)) {
94 0
    verbose && enter(verbose, "Downloading URL")
95 0
    url <- pathname
96 0
    verbose && cat(verbose, "URL: ", url)
97 0
    pathname <- downloadFile(url, verbose=less(verbose,50))
98 0
    verbose && cat(verbose, "Local file: ", pathname)
99 0
    verbose && exit(verbose)
100
  }
101

102 1
  pathname <- getAbsolutePath(pathname)
103 1
  verbose && cat(verbose, "Markdown pathname (absolute): ", pathname)
104 1
  verbose && printf(verbose, "Input file size: %g bytes\n", file.info(pathname)$size)
105 1
  verbose && cat(verbose, "Output and working directory: ", getAbsolutePath(outPath))
106 1
  pattern <- "(.*)[.]([^.]+)$"
107 1
  replace <- "\\1.html"
108 1
  filenameOut <- gsub(pattern, replace, basename(pathname))
109 1
  pathnameOut <- filePath(outPath, filenameOut)
110 1
  pathnameOut <- getAbsolutePath(pathnameOut)
111 1
  verbose && cat(verbose, "Output pathname: ", pathnameOut)
112

113 1
  opwd <- "."
114 1
  on.exit(setwd(opwd), add=TRUE)
115 1
  if (!is.null(outPath)) {
116 1
    opwd <- setwd(outPath)
117
  }
118

119 1
  mdToHTML <- markdown::markdownToHTML
120 1
  fcnName <- "markdown::markdownToHTML()"
121

122 1
  verbose && enterf(verbose, "Calling %s", fcnName)
123 1
  pathnameR <- getRelativePath(pathname)
124 1
  pathnameOutR <- getRelativePath(pathnameOut)
125

126 1
  userArgs <- list(...)
127 1
  keep <- is.element(names(userArgs), names(formals(mdToHTML)))
128 1
  userArgs <- userArgs[keep]
129

130 1
  args <- c(list(pathnameR, output=pathnameOutR), userArgs, header=header)
131 1
  verbose && cat(verbose, "Arguments:")
132 1
  verbose && str(verbose, args)
133 1
  verbose && printf(verbose, "Input encoding: %s (from option 'encoding')\n", getOption("encoding"))
134 1
  verbose && printf(verbose, "Output encoding: UTF-8 (forced by %s)\n", fcnName)
135 1
  do.call(mdToHTML, args=args)
136

137 1
  verbose && exit(verbose)
138

139 1
  setwd(opwd); opwd <- "."
140 1
  verbose && printf(verbose, "Output file size: %g bytes\n", file.info(pathnameOut)$size)
141

142
  # Sanity check
143 1
  pathnameOut <- Arguments$getReadablePathname(pathnameOut)
144

145 1
  verbose && exit(verbose)
146

147 1
  pathnameOut
148
}) # compileMarkdown()

Read our documentation on viewing source code .

Loading