File: get_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 (36 lines) | stat: -rw-r--r-- 891 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

get_block <- function(block) {
  if (block$t != "CodeBlock" && block$t != "Code") return(NULL)
  id <- block$c[[1]][[1]]
  language <- if (length(block$c[[1]][[2]]))
    block$c[[1]][[2]][[1]] else ""
  #if (language != "R") return(NULL)
  args1 <- if (length(block$c[[1]][[2]]) > 1) {
    utils::tail(unlist(block$c[[1]][[2]]), -1)
  } else character(0L)
  arguments <- get_block_arguments(block$c[[1]][[3]])
  code <- block$c[[2]]
  list(
    id = id,
    language = language,
    arguments_single = args1,
    arguments = arguments,
    code = code
  )
}

get_block_arguments <- function(arguments) {
  sapply(arguments, function(a) {
    val <- a[[2]]
    if (val == "TRUE") { 
      val <- TRUE 
    } else if (val == "FALSE") {
      val <- FALSE
    } else if (grepl("^[0-9]+$", val)) {
      val <- as.numeric(val)
    }
    res <- list(val)
    names(res) <- a[[1]]
    res
  })
}