File: test-html.R

package info (click to toggle)
r-cran-bookdown 0.42%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,752 kB
  • sloc: javascript: 11,343; makefile: 21; sh: 20
file content (60 lines) | stat: -rw-r--r-- 2,359 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
library(testit)

assert("parse figure reference correctly", {
  res = parse_fig_labels("<caption>(#tab:foo) caption </caption>")
  (names(res$ref_table) %==% "tab:foo")
  (grepl("Table 1", res$content, fixed = TRUE))
  (!grepl("(#tab:foo)", res$content, fixed = TRUE))
  # works with indented too
  # https://github.com/rstudio/gt/issues/719
  res = parse_fig_labels("  <caption>(#tab:foo) caption </caption>")
  (names(res$ref_table) %==% "tab:foo")
  (grepl("Table 1", res$content, fixed = TRUE))
  (!grepl("(#tab:foo)", res$content, fixed = TRUE))
})

assert("biblio references section is correctly found", {
  # with a csl like https://www.zotero.org/styles/nature
  # reference div have more attributes
  html = c('<div id="references" class="section level1 unnumbered">',
           '<h1>References</h1>',
           '',
           '<div id="refs" class="references csl-bib-body" line-spacing="2">',
           '<div id="ref-item1" class="csl-entry">',
           '<div class="csl-left-margin">1. </div><div class="csl-right-inline">Doe, J. <em>First book</em>. (Cambridge University Press, 2005).</div>',
           '</div>')
  (parse_references(html)$div %==% html[[4]])
  (length(parse_references(html)$refs) == 1)
  html[4] = '<div id="refs" class="references csl-bib-body">'
  (parse_references(html)$div %==% html[[4]])
  (length(parse_references(html)$refs) == 1)
  html[4] = '<div id="refs" class="references">'
  (parse_references(html)$div %==% html[[4]])
  (length(parse_references(html)$refs) == 1)
})

assert("i18n config can be retrieved ", {
  opts$set(config = list())
  # default
  (i18n("label", "tab", label_names) %==% "Table ")
  (i18n("ui", "chapter_name", ui_names) %==% "")
  (i18n("ui", "dummy", ui_names) %==% NULL)
  # config set
  opts$set(config = list(language = list(
    label = list(tab = "TABLE "),
    ui = list(chapter_name = "CHAPTER "))
  ))
  (i18n("label", "tab") %==% "TABLE ")
  (i18n("ui", "chapter_name") %==% "CHAPTER ")
  opts$set(config = list())
})

assert("label_prefix retrieves correct config", {
  fun = function(i) paste0("TAB-", i)
  opts$set(config = list(language = list(label = list(tab = fun))))
  (label_prefix("tab") %==% fun)
  (is.function(label_prefix("fig")))
  (label_prefix("fig")(1) %==% "Figure 1")
  (label_prefix("fig", sep = ":")(1) %==% "Figure 1:")
  opts$set(config = list())
})