-
Notifications
You must be signed in to change notification settings - Fork 1
/
_hilang_setup.Rmd
41 lines (38 loc) · 1.64 KB
/
_hilang_setup.Rmd
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
```{r eval=TRUE, echo=FALSE, results="asis"}
# take a character vector of parameters and inject
# the appropriate script tag for code mirror
# ensures that the script tags are only inserted once
for(i in seq_along(params$hilang)) {
js_mode <- paste0("\n<script src=\"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.31.0/mode/", tolower(params$hilang[i]), "/", tolower(params$hilang[i]), ".min.js\"></script>\n")
cat(htmltools::htmlPreserve(js_mode))
}
```
```{r eval=TRUE, echo=FALSE}
knitr::knit_hooks$set(source = function(x, options) {
if (!is.null(options$hilang)) {
textarea_id <- paste(sample(LETTERS, 5), collapse = "")
code_open <- paste0("\n\n<textarea id=\"", textarea_id, "\">\n")
code_close <- "\n</textarea>"
jscript_editor <- paste0("\n<script> var codeElement = document.getElementById(\"", textarea_id, "\"); var editor = null; if (null != codeElement) { editor = CodeMirror.fromTextArea(codeElement, { lineNumbers: true, readOnly: true, viewportMargin: Infinity, mode: 'text/x-", tolower(options$hilang), "' }); } </script>\n")
# if the option from_file is set to true then assume that
# whatever is in the code chunk is a file path
if (!is.null(options$from_file) && options$from_file) {
code_body <- readLines(file.path(x))
} else {
code_body <- x
}
knitr::asis_output(
htmltools::htmlPreserve(
stringr::str_c(
code_open,
paste(code_body, collapse = "\n"),
code_close,
jscript_editor
)
)
)
} else {
stringr::str_c("\n\n```", tolower(options$engine), "\n", paste0(x, collapse = "\n"), "\n```\n\n")
}
})
```