Skip to content

Commit 3008de7

Browse files
committed
use Ricardos translation_links version
1 parent 1a2ace3 commit 3008de7

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

vignettes/_translation_links.R

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
# build a link list of alternative languages (may be character(0))
22
# idea is to look like 'Other languages: en | fr | de'
33
.write.translation.links <- function(fmt) {
4-
# Check if we're in a litedown context
5-
input_file = litedown::get_context("input")
6-
if (is.null(input_file) || !is.character(input_file) || length(input_file) == 0) {
7-
# Not in litedown context (e.g., being rendered by rmarkdown/pkgdown)
8-
return("")
9-
}
10-
11-
url = "https://rdatatable.gitlab.io/data.table/articles"
12-
path = dirname(input_file)
13-
if (basename(path) == "vignettes") {
14-
lang = "en"
15-
} else {
16-
lang = basename(path)
17-
path = dirname(path)
18-
}
19-
translation = dir(path,
20-
recursive = TRUE,
21-
pattern = glob2rx(input_file)
22-
)
23-
transl_lang = ifelse(dirname(translation) == ".", "en", dirname(translation))
24-
block = if (!all(transl_lang == lang)) {
25-
linked_transl = sprintf("[%s](%s)", transl_lang, file.path(url, sub("(?i)\\.Rmd$", ".html", translation)))
26-
linked_transl[transl_lang == lang] = lang
27-
sprintf(fmt, paste(linked_transl, collapse = " | "))
28-
} else ""
29-
litedown::raw_text(block)
4+
url = "https://rdatatable.gitlab.io/data.table/articles"
5+
# this guard is needed because pkgdown::build_article does not use litedown
6+
# https://github.com/yihui/knitr/issues/926#issuecomment-68503962
7+
knitr = isTRUE(getOption('knitr.in.progress'))
8+
path = if (knitr) knitr::current_input(TRUE) else normalizePath(litedown::get_context("input"))
9+
rmdfile = basename(path)
10+
rmddir = dirname(path)
11+
if (basename(rmddir) == "vignettes") {
12+
lang = "en"
13+
main_dir = rmddir
14+
} else {
15+
lang = basename(rmddir)
16+
main_dir = dirname(rmddir)
17+
}
18+
translation = dir(main_dir, recursive = TRUE, pattern = glob2rx(rmdfile))
19+
transl_lang = dirname(translation)
20+
transl_lang[transl_lang == "."] = "en"
21+
if (any(transl_lang != lang)) {
22+
link_path = file.path(url, sub("(?i)\\.Rmd$", ".html", translation))
23+
linked_transl = sprintf("[%s](%s)", transl_lang, link_path)
24+
linked_transl[transl_lang == lang] = lang
25+
block = sprintf(fmt, paste(linked_transl, collapse = " | "))
26+
} else {
27+
block = ""
28+
}
29+
litedown::raw_text(block)
3030
}

0 commit comments

Comments
 (0)