Repository navigation
Expand file tree
/
Copy pathgithub_document.R
More file actions
198 lines (181 loc) · 7.64 KB
/
Copy pathgithub_document.R
File metadata and controls
198 lines (181 loc) · 7.64 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#' Convert to GitHub Flavored Markdown
#'
#' Format for converting from R Markdown to GitHub Flavored Markdown.
#'
#' See the [online
#' documentation](https://rmarkdown.rstudio.com/github_document_format.html) for additional details on using the `github_document()`
#' format.
#' @inheritParams output_format
#' @inheritParams html_document
#' @inheritParams md_document
#' @param math_method `"default"` means that [native Github
#' support](https://github.blog/changelog/2022-05-19-render-mathematical-expressions-in-markdown/)
#' for math notations using Mathjax syntax will be used. Other possible value
#' is `"webtex"` where equation will be rendered to an image in the resulting
#' Markdown. See [html_document()] for option to change webtex URL. Set `math_method`to
#' `NULL` to opt-out any math treatment.
#' @param hard_line_breaks `TRUE` to generate markdown that uses a simple
#' newline to represent a line break (as opposed to two-spaces and a newline).
#' @param html_preview `TRUE` to also generate an HTML file for the purpose of
#' locally previewing what the document will look like on GitHub.
#' @param keep_html `TRUE` to keep the preview HTML file in the working
#' directory. Default is `FALSE`.
#'
#' @details # About Math support
#'
#' Default behavior is to keep any inline equation using `$` and any block
#' equation using `$$` in the resulting markdown as Github will process those
#' using Mathjax. **This feature is only available with Pandoc 2.10.1 and above**
#'
#' When using `"webtex"`, PNG images with a white background are used by default so
#' that it shows correctly on Github on both light and dark theme. You can
#' choose to only output SVG for better quality by changing the URL used:
#'
#' ```yaml
#' output:
#' github_document:
#' math_method:
#' engine: webtex
#' url: https://latex.codecogs.com/svg.image?
#' ```
#'
#' Background or fonts color cannot be changed for now and your equation may not be visible on dark theme.
#'
#' Using `"webtex"` will be the default with Pandoc 2.0.4 until Pandoc 2.10. Before 2.0.4, Github document output does not support math.
#'
#' @return R Markdown output format to pass to [render()]
#' @export
#' @md
github_document <- function(toc = FALSE,
toc_depth = 3,
number_sections = FALSE,
math_method = "default",
preserve_yaml = FALSE,
fig_width = 7,
fig_height = 5,
dev = 'png',
df_print = "default",
includes = NULL,
md_extensions = NULL,
hard_line_breaks = TRUE,
pandoc_args = NULL,
html_preview = TRUE,
keep_html = FALSE) {
# add special markdown rendering template to ensure we include the title fields
# and add an optional feature to number sections
pandoc_args <- c(
pandoc_args, "--template",
pkg_file_arg("rmarkdown/templates/github_document/resources/default.md")
)
# use md_document as base
variant <- "gfm"
if (!hard_line_breaks) variant <- paste0(variant, "-hard_line_breaks")
# atx headers are the default in pandoc 2.11.2 and the flag has been deprecated
# to be replaced by `--markdown-headings=atx|setx`
if (!pandoc_available("2.11.2")) pandoc_args <- c(
"--atx-headers", pandoc_args
)
if ((toc || number_sections) &&
!isTRUE(grepl("gfm_auto_identifiers", md_extensions))) {
md_extensions <- c(md_extensions, "+gfm_auto_identifiers")
}
# math support
if (!is.null(math_method)) {
math <- check_math_argument(math_method)
preview_math <- NULL
if (!math$engine %in% c("default", "webtex")) {
stop("Markdown output format only support 'default' for native Github Math support or 'webtex' for using a Webtex online service to render math.")
}
if (math$engine == "default") {
if (pandoc_available("2.10.1")) {
# don't activate math in Pandoc and pass it as is
# https://github.blog/changelog/2022-05-19-render-mathematical-expressions-in-markdown/
math <- NULL
# Pandoc 3.4 uses +tex_math_gfm by default to write special gfm syntax. We choose to keep $$ and $ which are still supported by Github
variant <- if (pandoc_available("3.4")) paste0(variant, "-tex_math_gfm") else paste0(variant, "+tex_math_dollars")
preview_math <- check_math_argument("mathjax")
} else {
# fallback to webtex
math <- check_math_argument("webtex")
}
}
if (!is.null(math) && math$engine == "webtex") {
if (is.null(math$url)) {
# default to png and white background for webtex
math$url <- "https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D&space;%5Cbg_white&space;"
}
preview_math <- check_math_argument("webtex")
}
math <- add_math_support(math, NULL, NULL, NULL)
preview_math <- add_math_support(preview_math, NULL, NULL, NULL)
pandoc_args <- c(pandoc_args, math$args)
}
format <- md_document(
variant = variant, toc = toc, toc_depth = toc_depth,
number_sections = number_sections, preserve_yaml = preserve_yaml,
fig_width = fig_width, fig_height = fig_height,
dev = dev, df_print = df_print, includes = includes,
md_extensions = md_extensions, pandoc_args = pandoc_args
)
# add a post processor for generating a preview if requested
post <- format$post_processor
format$post_processor <- function(metadata, input_file, output_file, clean, verbose) {
if (is.function(post)) output_file <- post(metadata, input_file, output_file, clean, verbose)
if (html_preview) {
css <- pkg_file_arg(
"rmarkdown/templates/github_document/resources/github.css")
# provide a preview that looks like github
args <- c(
self_contained_args(), pandoc_highlight_args("pygments"),
"--template", pkg_file_arg(
"rmarkdown/templates/github_document/resources/preview.html"),
"--variable", paste0("github-markdown-css:", css),
"--metadata", "pagetitle=PREVIEW", # HTML5 requirement
if (!is.null(preview_math)) preview_math$args
)
# run pandoc
preview_file <- file_with_ext(output_file, "html")
pandoc_convert(
input = output_file, to = "html", from = variant, output = preview_file,
options = args, verbose = verbose
)
# move the preview to the preview_dir if specified
if (!keep_html) {
preview_dir <- Sys.getenv("RMARKDOWN_PREVIEW_DIR", unset = NA)
if (!is.na(preview_dir)) {
relocated_preview_file <- tempfile("preview-", preview_dir, ".html")
file.copy(preview_file, relocated_preview_file)
file.remove(preview_file)
preview_file <- relocated_preview_file
}
}
if (verbose) message("\nPreview created: ", preview_file)
}
output_file
}
format # return format
}
# explicit definition of gfm_format -- not currently used b/c it didn't
# yield different about that 'gfm' w/ pandoc 2.11. keeping in the source
# code for now in case we need to use it for another workaround.
gfm_format <- function() {
paste0("markdown_strict",
# commonmark
"+raw_html",
"+all_symbols_escapable",
"+backtick_code_blocks",
"+fenced_code_blocks",
"+space_in_atx_header",
"+intraword_underscores",
"+lists_without_preceding_blankline",
"+shortcut_reference_links",
# gfm extensions
"+auto_identifiers",
"+autolink_bare_uris",
"+emoji",
"+gfm_auto_identifiers",
"+pipe_tables",
"+strikeout",
"+task_lists"
)
}