File: whisker.R

package info (click to toggle)
r-cran-reprex 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,424 kB
  • sloc: sh: 13; makefile: 2
file content (93 lines) | stat: -rw-r--r-- 2,333 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
apply_template <- function(x, reprex_data = NULL) {
  data <- with(reprex_data, list(
    yaml = yaml_md("gfm"),
    tidyverse_quiet = as.character(tidyverse_quiet),
    comment = comment,
    upload_fun = "knitr::imgur_upload",
    ad = "Created on `r Sys.Date()` by the [reprex package](https://reprex.tidyverse.org) (v`r utils::packageVersion(\"reprex\")`)"
  ))

  if (!is.null(reprex_data$std_file)) {
    data$std_file_stub <- prose(newline(backtick(reprex_data$std_file)))
  }

  if (isTRUE(reprex_data$si)) {
    data$si <- collapse(si(details = reprex_data$venue == "gh"))
  }

  if (reprex_data$venue %in% c("gh", "so")) {
    data$ad <- paste0("<sup>", data$ad, "</sup>")
  }

  if (reprex_data$venue == "so") {
    data$yaml <- yaml_md("md")
    data$so_syntax_highlighting <- prose("<!-- language-all: lang-r -->")
    ## empty line between html comment re: syntax highlighting and reprex code
    x <- c("", x)
  }

  if (reprex_data$venue == "r") {
    data$upload_fun <- "identity"
  }

  data$ad <- if (reprex_data$advertise) prose(data$ad) else NULL
  data$yaml <- collapse(data$yaml)
  data$body <- collapse(x)
  whisker::whisker.render(read_template("REPREX"), data = data)
}

read_template <- function(slug) {
  path <- system.file(
    "templates",
    path_ext_set(slug, "R"),
    package = "reprex",
    mustWork = TRUE
  )
  readLines(path)
}

yaml_md <- function(flavor = c("gfm", "md"),
                    pandoc_version = rmarkdown::pandoc_version()) {
  flavor <- match.arg(flavor)
  yaml <- c(
    "---",
    "output:",
    "  md_document:",
    "    pandoc_args:",
    if (flavor == "gfm") {
      c(
    "      - '--from=markdown-implicit_figures'",
    "      - '--to=commonmark'"
      )
    },
    if (!is.null(pandoc_version)) {
      if (pandoc_version < "1.16") {
    "      - '--no-wrap'"
      } else {
    "      - '--wrap=preserve'"
      }
    },
    "---"
  )
  ## prepend with `#' ` in a separate step because
  ## https://github.com/klutometis/roxygen/issues/668
  prose(yaml)
}

si <- function(details = FALSE) {
  txt <- if (requireNamespace("devtools", quietly = TRUE)) {
    "devtools::session_info()"
  } else {
    "sessionInfo()"
  }

  if (details) {
    txt <- c(
      prose("<details><summary>Session info</summary>"),
      txt,
      prose("</details>")
    )
  }

  txt
}