~hrbrmstr/cloc

cloc/R/strip-rmd.R -rw-r--r-- 749 bytes
0350958fhrbrmstr Working on #10 2 years ago
                                                                                
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
#' @keywords internal
strip_rmd_addin <- function() {

  ctx <- rstudioapi::getActiveDocumentContext()

  if (!is.null(ctx)) {

    if (is_rmd_file(ctx$path)) {

      x <- basename(ctx$path)
      x <- tools::file_path_sans_ext(x)

      tf1 <- tempfile(pattern = x, fileext = ".Rmd")
      on.exit(unlink(tf1), add = TRUE)

      tf2 <- tempfile(pattern = x, fileext = ".R")

      cat(ctx$contents, file = tf1, sep = "\n")

      nc <- cloc_remove_comments(tf1)

      cat(nc, file = tf2)

      navigateToFile(tf2, line = -1L, column = -1L)

      message(
        sprintf(
          "Temporary file generated is in [%s]", tf2
        )
      )

    } else {
      stop("Can only perform operations on .Rmd files.", call. = FALSE)
    }

  }

}