File: markdown_block.R

package info (click to toggle)
r-cran-simplermarkdown 0.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 392 kB
  • sloc: makefile: 2
file content (38 lines) | stat: -rw-r--r-- 1,067 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38


#' Return a code block object that can be included in the pandoc parse tree
#'
#' @param content a character vector containing the code
#' @param language language of the code in the code block
#' @param id optional id of the code block
#' @param ... additional arguments should be named. These are added to the
#'   markdown block as additional arguments.
#'
#' @return 
#' Returns a \code{list} with the correct structure for a code block in 
#' the pandoc parse tree.
#'
#' @export
markdown_block <- function(content, language, id = "", ...) {
  # parse extra arguments
  # This should be an unnamed list with each element a 2 element
  # vector; first element the name; second element the value
  extra <- list(...)
  if (!all(names(extra) != ""))
    stop("Additional arguments all need to be named")
  extra <- lapply(extra, \(x) as.character(x)[1])
  extra <- lapply(names(extra), \(x) list(x, extra[[x]]))
  # block
  list(
    t = "CodeBlock",
    c = list(
      list(
        id,
        list(language),
        extra
      ),
      content
    )
  )
}