Debian Codegraph @

tests/testthat/test-lua-filters.R

raw
1 .generate_md_and_convert <- function(content, output_format) {
2 input_file <- local_rmd_file(c("---\ntitle: Test\n---\n", content))
3 res <- .render_and_read(input_file, output_format = output_format)
4 # print nicely for snapshot test
5 xfun::raw_string(res)
6 }
7
8 # rmarkdown requires pandoc >= 2.1 to support Lua filters
9 skip_if_not_pandoc("2.1")
10
11 # TODO: At some point, change this file to test only the filter in a simple pandoc conversion without all the rendering ( ~ unit test for Lua filters)
12
13 test_that("pagebreak Lua filters works", {
14 rmd <- "# HEADER 1\n\\newpage\n# HEADER 2\n\\pagebreak\n# HEADER 3"
15 res <- .generate_md_and_convert(rmd, "html_document")
16 expect_match(res[grep("HEADER 1", res)+1], "<div style=\"page-break-after: always;\"></div>")
17 expect_match(res[grep("HEADER 2", res)+1], "<div style=\"page-break-after: always;\"></div>")
18 # add a class instead of inline style
19 rmd2 <- paste0("---\nnewpage_html_class: page-break\n---\n", rmd)
20 res <- .generate_md_and_convert(rmd2, "html_document")
21 expect_match(res[grep("HEADER 1", res)+1], "<div class=\"page-break\"></div>")
22 expect_match(res[grep("HEADER 2", res)+1], "<div class=\"page-break\"></div>")
23 # For tex document this is unchanged
24 res <- .generate_md_and_convert(rmd, "latex_document")
25 expect_match(res[grep("HEADER 1", res)+2], "\\newpage", fixed = TRUE)
26 expect_match(res[grep("HEADER 2", res)+2], "\\pagebreak", fixed = TRUE)
27 })
28
29 test_that("number_sections Lua filter works", {
30 numbers <- c("1", "1.1", "2", "2.1")
31 headers <- c("# A", "## B", "# C", "## D")
32 rmd <- c(paste0(headers, "\n\n"), "See [A]")
33 # Variant for snapshot: pandoc 2.11.2 default to atx headers
34 pandoc_versions <- if (pandoc_available("3")) {
35 "pandoc-3"
36 } else if (pandoc_available("2.18.0.1")) {
37 "after-pandoc-2.18"
38 } else if (pandoc_available("2.18")) {
39 "pandoc-2.18"
40 } else if (pandoc_available("2.11.2")) {
41 "after-pandoc-2.11.2"
42 } else {
43 "before-pandoc-2.11.2"
44 }
45 # -gfm_auto_identifiers
46 result <- .generate_md_and_convert(rmd, md_document(number_sections = TRUE))
47 expect_snapshot_output(result, variant = pandoc_versions)
48
49 # +gfm_auto_identifiers
50 skip_if_not_pandoc("2.5") # gfm_auto_identifiers is not working the same before
51 result <- .generate_md_and_convert(
52 rmd,
53 md_document(number_sections = TRUE, md_extensions = "+gfm_auto_identifiers")
54 )
55 expect_snapshot_output(result, variant = pandoc_versions)
56
57 # Github document
58 skip_if_not_pandoc("2.10.1") # changes in gfm writer break this test for earlier versions
59 result <- .generate_md_and_convert(rmd, github_document(number_sections = TRUE, toc = TRUE))
60 expect_snapshot_output(result, variant = pandoc_versions)
61 })
62
63 test_that("latex-divs.lua works with HTML doc", {
64 rmd <- "::: custom\ncontent\n:::\n\n some other content"
65 res <- .generate_md_and_convert(rmd, "html_document")
66 expect_match(grep('<div.*class="custom"', res, value = TRUE), '<div class="custom"')
67 rmd <- "::: {.custom #id}\ncontent\n:::\n\n some other content"
68 res <- .generate_md_and_convert(rmd, "html_document")
69 expect_match(grep('<div.*class="custom"', res, value = TRUE), '<div id="id" class="custom">')
70 rmd <- "::: {.custom #id data-latex=''}\ncontent\n:::\n\n some other content"
71 res <- .generate_md_and_convert(rmd, "html_document")
72 expect_match(grep('<div.*class="custom"', res, value = TRUE), '<div id="id" class="custom">')
73 rmd <- "::: {.custom #id latex=true}\ncontent\n:::\n\n some other content"
74 res <- .generate_md_and_convert(rmd, "html_document")
75 expect_match(grep('<div.*class="custom"', res, value = TRUE), '<div id="id" class="custom">')
76 rmd <- "::: {.custom #id latex=1}\ncontent\n:::\n\n some other content"
77 res <- .generate_md_and_convert(rmd, "html_document")
78 expect_match(grep('<div.*class="custom"', res, value = TRUE), '<div id="id" class="custom">')
79 })
80
81 test_that("latex-divs.lua works with LaTeX (PDF)", {
82 rmd <- "::: {.custom #id}\ncontent\n:::\n\n some other content"
83 res <- .generate_md_and_convert(rmd, "latex_document")
84 expect_false(any(grepl('custom', res)))
85 rmd <- "::: {.custom #id data-latex=''}\ncontent\n:::\n\n some other content"
86 res <- .generate_md_and_convert(rmd, "latex_document")
87 lines <- local({ i <- grep("custom", res); seq.int(i[1], i[2])})
88 expect_snapshot_output(cat(res[lines]))
89 rmd <- "::: {.custom #id latex=true}\ncontent\n:::\n\n some other content"
90 res <- .generate_md_and_convert(rmd, "latex_document")
91 lines <- local({ i <- grep("custom", res); seq.int(i[1], i[2])})
92 expect_snapshot_output(cat(res[lines]))
93 rmd <- "::: {.custom #id latex=1}\ncontent\n:::\n\n some other content"
94 res <- .generate_md_and_convert(rmd, "latex_document")
95 lines <- local({ i <- grep("custom", res); seq.int(i[1], i[2])})
96 expect_snapshot_output(cat(res[lines]))
97 })
98
99 test_that("formats have the expected Lua filter", {
100 expect_filters <- function(format_fun, expected_filters) {
101 filters <- basename(format_fun$pandoc$lua_filters)
102 if ("rmarkdown_output_format" %in% class(expected_filters))
103 expected_filters <- basename(expected_filters$pandoc$lua_filters)
104 expect_identical(
105 filters,
106 xfun::with_ext(expected_filters, "lua")
107 )
108 }
109 # different lua filter
110 pgb <- "pagebreak"; lxd <- "latex-div"
111 tbl <- if (pandoc_available("3.2.1")) "table-classes"
112 nbs <- "number-sections"; acs <- "anchor-sections"
113 expect_filters(beamer_presentation(), c(pgb, lxd))
114 expect_filters(github_document(number_sections = TRUE),
115 md_document(number_sections = TRUE))
116 expect_filters(html_document(), c(pgb, lxd, tbl))
117 expect_filters(html_document(anchor_sections = TRUE), c(pgb, lxd, tbl, acs))
118 expect_filters(html_document(anchor_sections = list(depth = 3)), c(pgb, lxd, tbl, acs))
119 expect_filters(html_document_base(), c(pgb, lxd, tbl))
120 expect_filters(latex_document(), c(pgb, lxd))
121 expect_filters(context_document(ext = ".tex"), c(pgb))
122 expect_filters(md_document(number_sections = TRUE), c(nbs))
123 expect_filters(pdf_document(), c(pgb, lxd))
124 expect_filters(powerpoint_presentation(number_sections = TRUE), c(nbs))
125 expect_filters(odt_document(number_sections = TRUE), c(pgb, nbs))
126 expect_filters(rtf_document(number_sections = TRUE), c(nbs))
127 expect_filters(slidy_presentation(number_sections = TRUE), c(pgb, lxd, tbl, nbs))
128 expect_filters(
129 word_document(number_sections = TRUE),
130 c(pgb, if (!pandoc_available("2.10.1")) nbs)
131 )
132 })
133
134 test_that("lua file are correctly found", {
135 expect_match(basename(pkg_file_lua()), ".*[.]lua$")
136 expect_match(basename(pkg_file_lua("number-sections.lua")), "^number-sections.lua$")
137 })