forked from mitchelloharawild/vitae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsl_name.R
118 lines (102 loc) · 3.51 KB
/
csl_name.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#' A name variable conforming to the CSL schema
#'
#' This class provides helper utilities to display, sort, and select attributes
#' from a name in the CSL format.
#'
#' @param x For `csl_name()`, `x` should be a list of `csl_name()`. For
#' `list_of_csl_names()`, `x` should be a list of `csl_names()`.
#' @param family The family name
#' @param given The given name
#' @param dropping_particle,non_dropping_particle,suffix,comma_suffix,static_ordering,literal,parse_names Additional
#' name variable properties as described in the schema.
#'
#' @seealso
#' <https://citeproc-js.readthedocs.io/en/latest/csl-json/markup.html#name-fields>
#'
#' @keywords internal
#' @export
csl_name <- function(family = NULL, given = NULL, dropping_particle = NULL,
non_dropping_particle = NULL, suffix = NULL,
comma_suffix = NULL, static_ordering = NULL,
literal = NULL, parse_names = NULL) {
x <- list(
family = family, given = given, `dropping-particle` = dropping_particle,
`non-dropping-particle` = non_dropping_particle, suffix = suffix,
`comma-suffix` = comma_suffix, `static-ordering` = static_ordering,
literal = literal, `parse-names` = parse_names
)
new_csl_name(Filter(Negate(is.null), x), validate = FALSE)
}
csl_name_fields <- c("family", "given", "dropping-particle", "non-dropping-particle",
"suffix", "comma-suffix", "static-ordering", "literal", "parse-names")
new_csl_name <- function(x, validate = TRUE) {
if(!validate || all(names(x) %in% csl_name_fields)) {
structure(x, class = "csl_name")
} else {
abort(sprintf("Unknown CSL name properties: %s.",
paste(setdiff(names(x), csl_name_fields), collapse = ", ")))
}
}
#' @export
format.csl_name <- function(x, ...) {
fmt <- x[c("non-dropping-particle", "dropping-particle", "given", "family", "suffix", "literal")]
format(paste(Filter(Negate(is.null), fmt), collapse = " "), ...)
}
#' @export
print.csl_name <- function(x, ...) {
cat(format(x, ...))
}
#' @rdname csl_name
#' @export
csl_names <- function(x = list()) {
vctrs::new_vctr(lapply(x, new_csl_name), class = "csl_names")
}
#' @export
format.csl_names <- function(x, ...) {
vapply(x, format, character(1L), ...)
}
#' @rdname csl_name
#' @export
list_of_csl_names <- function(x = list()) {
new_list_of(x, csl_names(), class = "list_of_csl_names")
}
#' @export
format.list_of_csl_names <- function(x, ...) {
vapply(x, function(z) paste(format(z, ...), collapse = ", "), character(1L))
}
#' @export
obj_print_data.list_of_csl_names <- function(x, ...) {
print(format(x), quote = FALSE)
}
#' @export
vec_cast.list_of_csl_names.list <- function(x, to, ...) {
if(length(x) == 1 && !is.null(names(x[[1]]))) x <- list(x)
list_of_csl_names(lapply(x, csl_names))
}
#' @method vec_cast.character list_of_csl_names
#' @export
vec_cast.character.list_of_csl_names <- function(x, to, ...) {
format(x)
}
#' @export
xtfrm.list_of_csl_names <- function(x) {
xtfrm(format(x))
}
#' @export
vec_proxy_order.list_of_csl_names <- xtfrm.list_of_csl_names
#' @export
names.list_of_csl_names <- function(x) {
csl_name_fields
}
#' @export
`$.list_of_csl_names` <- function(x, name) {
vapply(x, function(authors) {
out <- vapply(authors, function(author) author[[name]], character(1L))
paste(out, collapse = ", ")
}, character(1L))
}
#' @importFrom pillar pillar_shaft
#' @export
pillar_shaft.list_of_csl_names <- function(x, ...) {
pillar::new_pillar_shaft_simple(format(x), align = "left", min_width = 10)
}