1 |
|
- |
# backport of simplified trimws() (introduced in R-3.3.0) |
2 |
|
- |
trimws <- function(x) { |
3 |
|
- |
sub("^[[:space:]]+", "", sub("[[:space:]]$", "", x)) |
4 |
|
- |
} |
5 |
|
- |
|
6 |
1 |
|
UnlistSplitClean <- function(s) { |
7 |
2 |
|
unlist(strsplit(gsub("[{}]", "", trimws(s)), " ")) |
8 |
3 |
|
} |
9 |
4 |
|
|
10 |
5 |
|
#' @importFrom tools deparseLatex latexToUtf8 parseLatex |
11 |
|
- |
cleanupLatex <- function (x){ |
12 |
|
- |
if (!length(x)) |
|
6 |
+ |
cleanupLatex <- function(x) { |
|
7 |
+ |
if (!length(x)) { |
13 |
8 |
|
return(x) |
|
9 |
+ |
} |
14 |
10 |
|
|
15 |
|
- |
if (any(grepl('mkbib', x))){ |
16 |
|
- |
x <- gsub('mkbibquote', 'dQuote', x) |
17 |
|
- |
x <- gsub('mkbibemph', 'emph', x) |
18 |
|
- |
x <- gsub('mkbibbold', 'bold', x) |
|
11 |
+ |
if (any(grepl("mkbib", x))) { |
|
12 |
+ |
x <- gsub("mkbibquote", "dQuote", x) |
|
13 |
+ |
x <- gsub("mkbibemph", "emph", x) |
|
14 |
+ |
x <- gsub("mkbibbold", "bold", x) |
19 |
15 |
|
} |
20 |
|
- |
x <- gsub('\\\\hyphen', '-', x) |
|
16 |
+ |
x <- gsub("\\\\hyphen", "-", x) |
21 |
17 |
|
|
22 |
18 |
|
latex <- try(parseLatex(x), silent = TRUE) |
23 |
19 |
|
if (inherits(latex, "try-error")) { |
24 |
20 |
|
x |
25 |
|
- |
}else { |
|
21 |
+ |
} else { |
26 |
22 |
|
x <- deparseLatex(latexToUtf8(latex), dropBraces = TRUE) |
27 |
|
- |
if (grepl("\\\\[[:punct:]]", x)){ |
28 |
|
- |
x <- gsub("\\\\'I", '\u00cd', x) |
29 |
|
- |
x <- gsub("\\\\'i", '\u00ed', x) |
30 |
|
- |
x <- gsub('\\\\"I', '\u00cf', x) |
31 |
|
- |
x <- gsub('\\\\"i', '\u00ef', x) |
32 |
|
- |
x <- gsub("\\\\\\^I", '\u00ce', x) |
33 |
|
- |
x <- gsub("\\\\\\^i", '\u00ee', x) |
34 |
|
- |
x <- gsub("\\\\`I", '\u00cc', x) |
35 |
|
- |
x <- gsub("\\\\`i", '\u00ec', x) |
36 |
|
- |
Encoding(x) <- 'UTF-8' |
|
23 |
+ |
if (grepl("\\\\[[:punct:]]", x)) { |
|
24 |
+ |
x <- gsub("\\\\'I", "\u00cd", x) |
|
25 |
+ |
x <- gsub("\\\\'i", "\u00ed", x) |
|
26 |
+ |
x <- gsub('\\\\"I', "\u00cf", x) |
|
27 |
+ |
x <- gsub('\\\\"i', "\u00ef", x) |
|
28 |
+ |
x <- gsub("\\\\\\^I", "\u00ce", x) |
|
29 |
+ |
x <- gsub("\\\\\\^i", "\u00ee", x) |
|
30 |
+ |
x <- gsub("\\\\`I", "\u00cc", x) |
|
31 |
+ |
x <- gsub("\\\\`i", "\u00ec", x) |
|
32 |
+ |
Encoding(x) <- "UTF-8" |
37 |
33 |
|
} |
38 |
34 |
|
x |
39 |
35 |
|
} |
40 |
36 |
|
} |
41 |
37 |
|
|
42 |
38 |
|
#' @importFrom utils as.personList |
43 |
|
- |
ArrangeAuthors <- function (x){ |
|
39 |
+ |
ArrangeAuthors <- function(x) { |
44 |
40 |
|
rx <- "(?i)[[:space:]]+and[[:space:]]+" |
45 |
|
- |
x <- gsub('[[:space:]]{2,}', ' ', x, useBytes = TRUE) |
|
41 |
+ |
x <- gsub("[[:space:]]{2,}", " ", x, useBytes = TRUE) |
46 |
42 |
|
authors <- lapply(strsplit(x, rx, perl = TRUE)[[1]], ArrangeSingleAuthor) |
47 |
43 |
|
as.personList(authors) |
48 |
44 |
|
} |
49 |
45 |
|
|
50 |
|
- |
ArrangeSingleAuthor <- function(y){ |
51 |
|
- |
if (grepl('[\\]', y)){ |
|
46 |
+ |
ArrangeSingleAuthor <- function(y) { |
|
47 |
+ |
if (grepl("[\\]", y)) { |
52 |
48 |
|
tmp <- try(parseLatex(y), TRUE) |
53 |
|
- |
if (!inherits(tmp, 'try-error')) |
|
49 |
+ |
if (!inherits(tmp, "try-error")) { |
54 |
50 |
|
y <- deparseLatex(latexToUtf8(tmp)) |
|
51 |
+ |
} |
55 |
52 |
|
} |
56 |
53 |
|
parts <- unlist(strsplit(y, ", ?(?![^{}]*})", perl = TRUE)) |
57 |
54 |
|
len.parts <- length(parts) |
58 |
|
- |
if (len.parts == 1L){ |
|
55 |
+ |
if (len.parts == 1L) { |
59 |
56 |
|
# parts <- "{Barnes} {and} {Noble,} {Inc.}" |
60 |
|
- |
if (grepl("[^{][[:print:]][}]$", parts)){ |
61 |
|
- |
s <- unlist(strsplit(parts, '')) |
|
57 |
+ |
if (grepl("[^{][[:print:]][}]$", parts)) { |
|
58 |
+ |
s <- unlist(strsplit(parts, "")) |
62 |
59 |
|
i <- length(s) - 1L |
63 |
60 |
|
paren <- 1 |
64 |
|
- |
while (paren > 0 && i > 0){ |
65 |
|
- |
if (s[i] == '{'){ |
|
61 |
+ |
while (paren > 0 && i > 0) { |
|
62 |
+ |
if (s[i] == "{") { |
66 |
63 |
|
paren <- paren - 1L |
67 |
|
- |
}else if (s[i] == '}'){ |
|
64 |
+ |
} else if (s[i] == "}") { |
68 |
65 |
|
paren <- paren + 1L |
69 |
66 |
|
} |
70 |
67 |
|
i <- i - 1L |
71 |
68 |
|
} |
72 |
|
- |
last <- paste0(s[(i+2):(length(s)-1)], collapse = '') |
|
69 |
+ |
last <- paste0(s[(i + 2):(length(s) - 1)], collapse = "") |
73 |
70 |
|
first <- NULL |
74 |
|
- |
if (i > 0) |
75 |
|
- |
first <- paste0(s[seq_len(i-1)], collapse = '') |
76 |
|
- |
person(UnlistSplitClean(first), cleanupLatex(last)) # Mathew {McLean IX} |
77 |
|
- |
}else{ |
|
71 |
+ |
if (i > 0) { |
|
72 |
+ |
first <- paste0(s[seq_len(i - 1)], collapse = "") |
|
73 |
+ |
} |
|
74 |
+ |
person(UnlistSplitClean(first), cleanupLatex(last)) # Mathew {McLean IX} |
|
75 |
+ |
} else { |
78 |
76 |
|
vonrx <- "(^|[[:space:]])([[:lower:]+[:space:]?]+)[[:space:]]" |
79 |
77 |
|
m <- regexec(vonrx, parts) |
80 |
78 |
|
von <- unlist(regmatches(parts, m))[3L] |
81 |
|
- |
if (!is.na(von)){ |
|
79 |
+ |
if (!is.na(von)) { |
82 |
80 |
|
name <- unlist(strsplit(parts, vonrx)) |
83 |
|
- |
if (length(name) == 1L){ # von Bommel |
84 |
|
- |
person(family=c(cleanupLatex(von), cleanupLatex(name))) |
85 |
|
- |
}else{ # Mark von Bommel |
86 |
|
- |
person(given = UnlistSplitClean(name[1L]), family=c(cleanupLatex(von), cleanupLatex(name[2L]))) |
|
81 |
+ |
if (length(name) == 1L) { # von Bommel |
|
82 |
+ |
person(family = c(cleanupLatex(von), cleanupLatex(name))) |
|
83 |
+ |
} else { # Mark von Bommel |
|
84 |
+ |
person(given = UnlistSplitClean(name[1L]), family = c(cleanupLatex(von), cleanupLatex(name[2L]))) |
87 |
85 |
|
} |
88 |
|
- |
}else{ # George Bernard Shaw |
|
86 |
+ |
} else { # George Bernard Shaw |
89 |
87 |
|
name <- UnlistSplitClean(parts) |
90 |
88 |
|
len.name <- length(name) |
91 |
|
- |
if (len.name <= 1L){ |
|
89 |
+ |
if (len.name <= 1L) { |
92 |
90 |
|
person(family = name) |
93 |
|
- |
}else{ |
|
91 |
+ |
} else { |
94 |
92 |
|
person(given = name[seq_len(len.name - 1L)], family = name[len.name]) |
95 |
93 |
|
} |
96 |
94 |
|
} |
97 |
95 |
|
} |
98 |
|
- |
}else if (len.parts == 2L){ |
99 |
|
- |
if (grepl('^[{]', parts[1L])){ # e.g. {de Gama}, Vasco |
|
96 |
+ |
} else if (len.parts == 2L) { |
|
97 |
+ |
if (grepl("^[{]", parts[1L])) { # e.g. {de Gama}, Vasco |
100 |
98 |
|
person(UnlistSplitClean(parts[2L]), UnlistSplitClean(parts[1L])) |
101 |
|
- |
}else{ |
|
99 |
+ |
} else { |
102 |
100 |
|
vonrx <- "^([[:lower:]+[:space:]?]+)[[:space:]]" |
103 |
101 |
|
m <- regexec(vonrx, parts[1L]) |
104 |
102 |
|
von <- unlist(regmatches(parts[1L], m))[2] |
105 |
|
- |
if (is.na(von)){ # e.g. Smith, John Paul |
|
103 |
+ |
if (is.na(von)) { # e.g. Smith, John Paul |
106 |
104 |
|
person(UnlistSplitClean(parts[2L]), cleanupLatex(parts[1L])) |
107 |
|
- |
}else{ # e.g. de la Soul, John |
108 |
|
- |
person(UnlistSplitClean(parts[2L]), c(cleanupLatex(von), cleanupLatex(sub(vonrx, '', parts[1L])))) |
|
105 |
+ |
} else { # e.g. de la Soul, John |
|
106 |
+ |
person(UnlistSplitClean(parts[2L]), c(cleanupLatex(von), cleanupLatex(sub(vonrx, "", parts[1L])))) |
109 |
107 |
|
} |
110 |
108 |
|
} |
111 |
|
- |
}else if (len.parts == 3L){ |
|
109 |
+ |
} else if (len.parts == 3L) { |
112 |
110 |
|
vonrx <- "^([[:lower:]+[:space:]?]+)[[:space:]]" |
113 |
111 |
|
m <- regexec(vonrx, parts[1L]) |
114 |
112 |
|
von <- unlist(regmatches(parts[1L], m))[2] |
115 |
|
- |
if (is.na(von)){ # e.g. White, Jr., Walter |
|
113 |
+ |
if (is.na(von)) { # e.g. White, Jr., Walter |
116 |
114 |
|
person(UnlistSplitClean(parts[3L]), c(cleanupLatex(parts[1L]), cleanupLatex(parts[2L]))) |
117 |
|
- |
}else{ # e.g. des White, Jr., Walter |
118 |
|
- |
person(UnlistSplitClean(parts[3L]), |
119 |
|
- |
c(cleanupLatex(von), cleanupLatex(sub(vonrx, '', parts[1L])), cleanupLatex(parts[2L]))) |
|
115 |
+ |
} else { # e.g. des White, Jr., Walter |
|
116 |
+ |
person( |
|
117 |
+ |
UnlistSplitClean(parts[3L]), |
|
118 |
+ |
c(cleanupLatex(von), cleanupLatex(sub(vonrx, "", parts[1L])), cleanupLatex(parts[2L])) |
|
119 |
+ |
) |
120 |
120 |
|
} |
121 |
|
- |
}else{ |
122 |
|
- |
stop('Invalid author/editor format.') |
|
121 |
+ |
} else { |
|
122 |
+ |
stop("Invalid author/editor format.") |
123 |
123 |
|
} |
124 |
124 |
|
} |
125 |
125 |
|
|
126 |
126 |
|
#' @importFrom utils bibentry person citation installed.packages toBibtex |
127 |
|
- |
make.bib.entry <- function( x ){ |
128 |
|
- |
type <- attr( x, "entry" ) |
129 |
|
- |
key <- attr( x, "key" ) |
130 |
|
- |
|
131 |
|
- |
y <- as.list( x ) |
132 |
|
- |
names(y) <- tolower( names(y) ) |
133 |
|
- |
|
134 |
|
- |
err.fun <- function(e){ |
135 |
|
- |
message( sprintf( "ignoring entry '%s' (line %d) because :\n\t%s\n", |
136 |
|
- |
key, |
137 |
|
- |
attr(x, "srcref")[1], |
138 |
|
- |
conditionMessage( e ) ) ) |
139 |
|
- |
NULL |
140 |
|
- |
} |
|
127 |
+ |
make.bib.entry <- function(x) { |
|
128 |
+ |
type <- attr(x, "entry") |
|
129 |
+ |
key <- attr(x, "key") |
141 |
130 |
|
|
142 |
|
- |
if( "author" %in% names(y) ){ |
143 |
|
- |
y[["author"]] <- tryCatch(ArrangeAuthors( y[["author"]] ), error = err.fun) |
144 |
|
- |
if (is.null(y[["author"]])) |
145 |
|
- |
return() |
|
131 |
+ |
y <- as.list(x) |
|
132 |
+ |
names(y) <- tolower(names(y)) |
|
133 |
+ |
|
|
134 |
+ |
err.fun <- function(e) { |
|
135 |
+ |
message(sprintf( |
|
136 |
+ |
"ignoring entry '%s' (line %d) because :\n\t%s\n", |
|
137 |
+ |
key, |
|
138 |
+ |
attr(x, "srcref")[1], |
|
139 |
+ |
conditionMessage(e) |
|
140 |
+ |
)) |
|
141 |
+ |
NULL |
|
142 |
+ |
} |
|
143 |
+ |
|
|
144 |
+ |
if ("author" %in% names(y)) { |
|
145 |
+ |
y[["author"]] <- tryCatch(ArrangeAuthors(y[["author"]]), error = err.fun) |
|
146 |
+ |
if (is.null(y[["author"]])) { |
|
147 |
+ |
return() |
146 |
148 |
|
} |
147 |
|
- |
if( "editor" %in% names(y) ){ |
148 |
|
- |
y[["editor"]] <- tryCatch(ArrangeAuthors( y[["editor"]] ), error = err.fun) |
149 |
|
- |
if (is.null(y[["editor"]])) |
150 |
|
- |
return() |
|
149 |
+ |
} |
|
150 |
+ |
if ("editor" %in% names(y)) { |
|
151 |
+ |
y[["editor"]] <- tryCatch(ArrangeAuthors(y[["editor"]]), error = err.fun) |
|
152 |
+ |
if (is.null(y[["editor"]])) { |
|
153 |
+ |
return() |
151 |
154 |
|
} |
|
155 |
+ |
} |
152 |
156 |
|
|
153 |
|
- |
# if there is a date entryn try to extract the year (#15) |
154 |
|
- |
fields <- names(y) |
155 |
|
- |
if( "date" %in% fields && !"year" %in% fields ){ |
156 |
|
- |
y$year <- format( as.Date( y$date), "%Y" ) |
157 |
|
- |
} |
|
157 |
+ |
# if there is a date entryn try to extract the year (#15) |
|
158 |
+ |
fields <- names(y) |
|
159 |
+ |
if ("date" %in% fields && !"year" %in% fields) { |
|
160 |
+ |
y$year <- format(as.Date(y$date), "%Y") |
|
161 |
+ |
} |
158 |
162 |
|
|
159 |
|
- |
tryCatch(bibentry( bibtype = type, key = key, other = y ), error = err.fun) |
|
163 |
+ |
tryCatch(bibentry(bibtype = type, key = key, other = y), error = err.fun) |
160 |
164 |
|
} |
161 |
165 |
|
|
162 |
|
- |
make.citation.list <- function( x, header, footer){ |
163 |
|
- |
rval <- list() |
164 |
|
- |
for( i in seq_along(x) ){ |
165 |
|
- |
if( !is.null(x[[i]] ) ) |
166 |
|
- |
rval <- c( rval, x[[i]] ) |
|
166 |
+ |
make.citation.list <- function(x) { |
|
167 |
+ |
rval <- list() |
|
168 |
+ |
for (i in seq_along(x)) { |
|
169 |
+ |
if (!is.null(x[[i]])) { |
|
170 |
+ |
rval <- c(rval, x[[i]]) |
167 |
171 |
|
} |
168 |
|
- |
class(rval) <- c( "bibentry" ) |
169 |
|
- |
rval |
|
172 |
+ |
} |
|
173 |
+ |
class(rval) <- c("bibentry") |
|
174 |
+ |
rval |
170 |
175 |
|
} |
171 |
176 |
|
|
172 |
177 |
|
findBibFile <- function(package) { |
173 |
|
- |
if( package %in% c("base", "datasets", "graphics", "grDevices", |
174 |
|
- |
"methods", "stats", "stats4", "tools", "utils" ) |
175 |
|
- |
) { |
176 |
|
- |
system.file( "bib", sprintf( "%s.bib", package ), package = "bibtex" ) |
177 |
|
- |
} else { |
178 |
|
- |
reference_locations <- c("REFERENCES.bib", "inst/REFERENCES.bib") |
179 |
|
- |
for(file_path in reference_locations) { |
180 |
|
- |
attempt <- system.file( file_path, package = package ) |
181 |
|
- |
if( nzchar(attempt) ) return( attempt ) |
|
178 |
+ |
if (package %in% c( |
|
179 |
+ |
"base", "datasets", "graphics", "grDevices", |
|
180 |
+ |
"methods", "stats", "stats4", "tools", "utils" |
|
181 |
+ |
) |
|
182 |
+ |
) { |
|
183 |
+ |
system.file("bib", sprintf("%s.bib", package), package = "bibtex") |
|
184 |
+ |
} else { |
|
185 |
+ |
reference_locations <- c("REFERENCES.bib", "inst/REFERENCES.bib") |
|
186 |
+ |
for (file_path in reference_locations) { |
|
187 |
+ |
attempt <- system.file(file_path, package = package) |
|
188 |
+ |
if (nzchar(attempt)) { |
|
189 |
+ |
return(attempt) |
182 |
190 |
|
} |
183 |
|
- |
stop( sprintf( "no bibtex database for package '%s'", package ) ) |
184 |
191 |
|
} |
|
192 |
+ |
stop(sprintf("no bibtex database for package '%s'", package)) |
|
193 |
+ |
} |
185 |
194 |
|
} |
186 |
195 |
|
|
187 |
196 |
|
#' convenience wrapper around .External call |