-
Notifications
You must be signed in to change notification settings - Fork 18
/
get_csl.R
50 lines (40 loc) · 1.69 KB
/
get_csl.R
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
#' Get a journal citation style from the official internet repository
#'
#' @param name Name of the journal,
#' exactly as found in <https://github.com/citation-style-language/styles>.
#' @param out.dir Directory to save the CSL file.
#'
#' @return The CSL file is saved in the selected directory, and the path is
#' returned invisibly.
#' @export
#'
#' @examplesIf interactive()
#' get_csl("peerj", out.dir = tempdir())
get_csl <- function(name = NULL, out.dir = NULL) {
if (is.null(name)) {
stop("Please specify the journal name")
}
gsub("\\.csl", "", name) # remove .csl from journal name
if (is.null(out.dir)) {
stop("Please specify where you would like to save the CSL file, e.g. out.dir = getwd()")
}
styles.repo <- "https://raw.githubusercontent.com/citation-style-language/styles/master/"
destfile <- file.path(out.dir, paste0(name, ".csl"))
# First try downloading CSL file from repo root
# this will fail for styles hosted in the "dependent" subfolder
out <- tryCatch(utils::download.file(paste0(styles.repo, name, ".csl"),
mode = "wb", destfile = destfile, quiet = TRUE),
warning = function(w){})
# if the style could not be downloaded, try the "dependent" folder
if (is.null(out)) {
out <- tryCatch(utils::download.file(paste0(styles.repo, "dependent/", name, ".csl"),
mode = "wb", destfile = destfile, quiet = TRUE),
warning = function(w){})
}
if (is.null(out)) {
if (file.exists(destfile)) file.remove(destfile)
stop("The citation style '", name, "' could not be downloaded. Please check the style name and your internet connection.")
} else {
destfile
}
}