Skip to content

Commit d5c5424

Browse files
committed
refactor to reduce cyclomatic complexity
1 parent 8413012 commit d5c5424

File tree

1 file changed

+104
-69
lines changed

1 file changed

+104
-69
lines changed

R/rmdCite.R

Lines changed: 104 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -86,38 +86,15 @@ Cite <- function(bib, ..., textual = FALSE, before = NULL, after = NULL,
8686
if (identical(class(bib), "bibentry"))
8787
bib <- as.BibEntry(bib)
8888

89-
result <- with(BibOptions(), {
89+
with(BibOptions(), {
9090
style <- .BibEntry_match_format_style(style)
9191
papers <- suppressMessages(do.call(`[.BibEntry`, list(x = bib, ...)))
9292
keys <- unlist(papers$key)
9393
if (!length(papers))
9494
return("")
9595
if (cite.style == "pandoc"){
96-
result <- paste0(paste0("@", names(papers)),
97-
collapse = paste0(bibpunct[5L], " "))
98-
result <- paste0(before, result, after)
99-
if (textual)
100-
result <- paste0(bibpunct[3L], result,
101-
bibpunct[4L])
102-
AddCite(keys, FALSE)
103-
result
96+
MakePandocCitation(papers, keys, textual, bibpunct, before, after)
10497
}else{
105-
shortName <- function(person){
106-
if (length(person$family))
107-
paste(cleanupLatex(person$family), collapse = " ")
108-
else paste(cleanupLatex(person$given), collapse = " ")
109-
}
110-
authorList <- function(paper){
111-
names <- vapply(paper$author, shortName, "")
112-
if (!length(names))
113-
names <- vapply(paper$editor, shortName, "")
114-
if (!length(names))
115-
names <- paper$label
116-
if (!length(names))
117-
names <- vapply(paper$translator, shortName, "")
118-
names
119-
}
120-
12198
numeric <- "numeric" %in% cite.style
12299
alphabetic <- "alphabetic" %in% cite.style
123100
if (cite.style != .cites$sty)
@@ -219,53 +196,111 @@ Cite <- function(bib, ..., textual = FALSE, before = NULL, after = NULL,
219196
}else {
220197
result <- paste0(auth, bibpunct[6L], " ", year)
221198
}
222-
if (make.hyper){
223-
url <- switch(hyperlink, to.bib = paste0("#bib-",
224-
gsub("[^_a-zA-Z0-9-]", "",
225-
keys,
226-
useBytes = TRUE)),
227-
to.doc = vapply(papers, GetURL, "",
228-
flds = c("url", "eprint", "doi"),
229-
to.bib = TRUE),
230-
hyperlink)
231-
if (style == "html"){
232-
new.links <- if (any(first))
233-
paste(paste("<a id='cite-", gsub("[^_a-zA-Z0-9-]",
234-
"", keys[first],
235-
useBytes = TRUE),
236-
"'></a>", sep = ""), collapse = "")
237-
else ""
238-
result <- if (numeric && super && textual)
239-
paste0(auth, "<sup><a href='", url, "'>", result,
240-
"</a></sup>")
241-
else paste0("<a href='", url, "'>", result, "</a>")
242-
}else if(style == "markdown"){
243-
new.links <- if(any(first))
244-
paste(paste("<a name=cite-",
245-
gsub("[^_a-zA-Z0-9-]", "",
246-
keys[first], useBytes = TRUE),
247-
"></a>", sep = ""), collapse = "")
248-
else ""
249-
result <- if (numeric && super && textual)
250-
paste0(auth, "^[", result, "](", url, ")")
251-
else paste0("[", result, "](", url, ")")
252-
}
253-
}
254-
result <- paste(result, collapse = paste0(bibpunct[5L], " "))
255-
if (!textual && (numeric || alphabetic)) {
256-
result <- paste0(bibpunct[3L], before, result, after,
257-
bibpunct[4L])
258-
if (super && numeric)
259-
result <- paste0("^{", result, "}")
260-
}else if (!textual){
261-
result <- paste0(bibpunct[1L], before, result, after,
262-
bibpunct[2L])
263-
}
264-
if (make.hyper && style %in% c("html", "markdown"))
265-
result <- paste0(new.links, result)
199+
result <- if (make.hyper)
200+
MakeCiteHyperlink(result, papers, hyperlink, keys,
201+
auth, style, first, numeric,
202+
alphabetic, super, textual,
203+
bibpunct, before, after)
204+
else
205+
AddCitationPunct(result, bibpunct, before, after,
206+
textual, numeric, alphabetic, super)
266207
result
267208
} # end else for if cite.style == "pandoc"
268209
}) # end with for BibOptions
210+
}
211+
212+
#' Convert one element of person object (i.e. a single person)
213+
#' to character for printing citation
214+
#' @noRd
215+
shortName <- function(person){
216+
if (length(person$family))
217+
paste(cleanupLatex(person$family), collapse = " ")
218+
else paste(cleanupLatex(person$given), collapse = " ")
219+
}
220+
221+
#' Convert person object with multiple persons to character
222+
#' @noRd
223+
authorList <- function(paper){
224+
names <- vapply(paper$author, shortName, "")
225+
if (!length(names))
226+
names <- vapply(paper$editor, shortName, "")
227+
if (!length(names))
228+
names <- paper$label
229+
if (!length(names))
230+
names <- vapply(paper$translator, shortName, "")
231+
names
232+
}
233+
234+
#' Print citation in pandoc format
235+
#' @noRd
236+
MakePandocCitation <- function(papers, keys, textual, bibpunct, before, after){
237+
result <- paste0(paste0("@", names(papers)),
238+
collapse = paste0(bibpunct[5L], " "))
239+
result <- paste0(before, result, after)
240+
if (textual)
241+
result <- paste0(bibpunct[3L], result,
242+
bibpunct[4L])
243+
AddCite(keys, FALSE)
244+
result
245+
}
246+
247+
#' Add hyperlink, punctuation, before and after text to citation
248+
#' @noRd
249+
MakeCiteHyperlink <- function(result, papers, hyperlink, keys, auth,
250+
style, first, numeric, alphabetic, super,
251+
textual, bibpunct, before, after){
252+
url <- switch(hyperlink, to.bib = paste0("#bib-",
253+
gsub("[^_a-zA-Z0-9-]", "",
254+
keys,
255+
useBytes = TRUE)),
256+
to.doc = vapply(papers, GetURL, "",
257+
flds = c("url", "eprint", "doi"),
258+
to.bib = TRUE),
259+
hyperlink)
260+
if (style == "html"){
261+
new.links <- if (any(first))
262+
paste(paste("<a id='cite-", gsub("[^_a-zA-Z0-9-]",
263+
"", keys[first],
264+
useBytes = TRUE),
265+
"'></a>", sep = ""), collapse = "")
266+
else ""
267+
result <- if (numeric && super && textual)
268+
paste0(auth, "<sup><a href='", url, "'>", result,
269+
"</a></sup>")
270+
else paste0("<a href='", url, "'>", result, "</a>")
271+
}else if(style == "markdown"){
272+
new.links <- if(any(first))
273+
paste(paste("<a name=cite-",
274+
gsub("[^_a-zA-Z0-9-]", "",
275+
keys[first], useBytes = TRUE),
276+
"></a>", sep = ""), collapse = "")
277+
else ""
278+
result <- if (numeric && super && textual)
279+
paste0(auth, "^[", result, "](", url, ")")
280+
else paste0("[", result, "](", url, ")")
281+
}
282+
result <- AddCitationPunct(result, bibpunct, before, after, textual,
283+
numeric, alphabetic, super)
284+
285+
if (style %in% c("html", "markdown"))
286+
result <- paste0(new.links, result)
287+
result
288+
}
289+
290+
#' Add punctions and before and after text to a citation
291+
#' @noRd
292+
AddCitationPunct <- function(result, bibpunct, before, after, textual,
293+
numeric, alphabetic, super){
294+
result <- paste(result, collapse = paste0(bibpunct[5L], " "))
295+
if (!textual && (numeric || alphabetic)) {
296+
result <- paste0(bibpunct[3L], before, result, after,
297+
bibpunct[4L])
298+
if (super && numeric)
299+
result <- paste0("^{", result, "}")
300+
}else if (!textual){
301+
result <- paste0(bibpunct[1L], before, result, after,
302+
bibpunct[2L])
303+
}
269304
result
270305
}
271306

0 commit comments

Comments
 (0)