Skip to content

Commit 8d5f411

Browse files
committed
fix rstudio#825: appendix_name can be a function, too
1 parent f827441 commit 8d5f411

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: bookdown
22
Type: Package
33
Title: Authoring Books and Technical Documents with R Markdown
4-
Version: 0.15.2
4+
Version: 0.15.3
55
Authors@R: c(
66
person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
77
person("JJ", "Allaire", role = "ctb"),

R/html.R

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,10 +970,22 @@ number_appendix = function(x, i1, i2, type = c('toc', 'header'), prefix, counter
970970
stop('appendix_counters in _bookdown.yml must be either a character vector or a function')
971971
}
972972

973+
prefix_fun = if (is.character(prefix)) {
974+
function(i) switch(
975+
length(prefix), paste0(prefix, i),
976+
paste0(prefix[1], i, prefix[2]),
977+
stop('appendix_name must be of length 1 or 2')
978+
)
979+
} else if (is.function(prefix)) prefix else {
980+
stop('appendix_name in _bookdown.yml must be a character string or function')
981+
}
982+
counters = vapply(s, counter_fun, character(1))
983+
prefixes = character(length(s))
973984
# only add the prefix to top-level appendix titles
974-
if (length(prefix) == 1 && prefix != '') prefix = ifelse(top, prefix, '')
985+
prefixes[top] = vapply(counters[top], prefix_fun, character(1))
986+
counters[top] = ''
975987

976-
x[i] = paste0(s1, prefix, vapply(s, counter_fun, character(1)), s3)
988+
x[i] = paste0(s1, prefixes, counters, s3)
977989
x
978990
}
979991

tests/testit/test-utils.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ assert('number_appendix inserts the prefix and counters', {
6868
'<li><span class=\"toc-section-number\">APP b</span> Two',
6969
'</ul>'
7070
))
71+
(number_appendix(html, 2, 5, 'toc', prefix = c('APP ', ':'), counters = letters) %==%
72+
c(
73+
'<ul>',
74+
'<li><span class=\"toc-section-number\">APP a:</span> One',
75+
'<li><span class=\"toc-section-number\">a.1</span> One subsection',
76+
'<li><span class=\"toc-section-number\">APP b:</span> Two',
77+
'</ul>'
78+
))
7179
eng_ints <- function(n) {
7280
eng = c("one", "two", "three")[n[1]]
7381
rest = paste0(n[-1], collapse = ".")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy