-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgetZoteroNotes.Rmd
More file actions
133 lines (126 loc) · 5.27 KB
/
getZoteroNotes.Rmd
File metadata and controls
133 lines (126 loc) · 5.27 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
Get all zotero entries with notes:
```{r}
u <- paste0 ("https://api.zotero.org/groups/2416765/items")
.params <- list (limit = 100)
x <- httr::GET (u, query = .params)
res <- httr::content (x, as = "text", encoding = "UTF-8")
f <- "bibnotes.txt"
con <- file (f)
writeLines (res, con = con)
close (con)
```
```{r}
dat <- jsonlite::read_json (f)
names (dat) <- vapply (dat, function (i) i$data$key, character (1))
# index of items with "parentItem"
index <- which (vapply (dat, function (i)
"parentItem" %in% names (i$data),
logical (1)))
# function to convert zotero list items delineated with <ui><li>-type tags to
# markdown format
convert_list_items <- function (x) {
# convert any list items
x <- gsub ("<ul>\n<li>", "- ", x)
x <- gsub ("<li>", " - ", x)
x <- gsub ("</li>|</ul>", "", x)
x <- strsplit (gsub ("<.*?>", "\n", x), "\\n") [[1]]
# remove any blank lines between list items.
i1 <- grep ("^[[:space:]]+\\-", x) # list items
if (length (i1) > 0) {
# index of empty lines
i2 <- which (nchar (x) == 0)
i2 <- i2 [i2 >= min (i1) & i2 <= max (i1)]
# get sequences of all i1 between list items
i1 <- split (seq (x), findInterval (seq (x), i1))
i1 <- unlist (unname (lapply (i1, function (i) i [-1])))
# empty lines which lie between list items
i2 <- i2 [which (i2 %in% i1)]
x <- x [!seq (x) %in% i2]
}
return (x)
}
# The notes are formatted with lots of html tags. These must all be removed,
# and then the strings all split at line breaks.
notes <- lapply (index, function (i) {
res <- convert_list_items (dat [[i]]$data$note)
res <- lapply (res, function (j) strsplit (j, "\\n") [[1]])
nc <- unlist (lapply (res, length))
res [which (nc == 0)] <- ""
unlist (res) })
keys <- vapply (index, function (i) dat [[i]]$data$parentItem, character (1))
names (notes) <- keys
index <- match (names (notes), names (dat))
itemTypes <- vapply (index, function (i) dat [[i]]$data$itemType, character (1))
titles <- vapply (index, function (i) dat [[i]]$data$title, character (1))
pubTitles <- vapply (index, function (i) {
pt <- dat [[i]]$data$publicationTitle
ifelse (length (pt) == 0, "", pt) }, character (1))
volumes <- vapply (index, function (i) {
vol <- dat [[i]]$data$volume
ifelse (length (vol) == 0, "", vol)}, character (1))
issues <- vapply (index, function (i) {
issue <- dat [[i]]$data$issue
ifelse (length (issue) == 0, "", issue) }, character (1))
pages <- vapply (index, function (i) {
p <- dat [[i]]$data$pages
ifelse (length (p) == 0, "", p) }, character (1))
creators <- vapply (index, function (i) {
cr <- dat [[i]]$data$creators
first <- vapply (cr, function (i) i$firstName,
character (1))
last <- vapply (cr, function (i) i$lastName,
character (1))
if (length (first) == 1 & first [1] == "")
cr <- last
else if (length (last) == 1 & last [1] == "")
cr <- first
else
cr <- paste0 (last, ", ", first)
if (length (cr) > 1)
cr <- paste0 (paste0 (cr [-length (cr)],
collapse = "; "),
"; and ", cr [length (cr)])
return (cr)
}, character (1))
abstracts <- vapply (index, function (i) dat [[i]]$data$abstractNote, character (1))
dates <- vapply (index, function (i) dat [[i]]$data$date, character (1))
urls <- vapply (index, function (i) dat [[i]]$data$url, character (1))
```
Then dump those notes to a `.md` file:
```{r}
out <- c ("---",
"title: 'Annotated Bibliography for Statistical Software'",
"output:",
" html_document:",
" toc: yes",
" toc_float: yes",
" number_sections: yes",
" theme: flatly",
"---",
"",
"# Bibliography",
"", "")
for (i in seq (notes)) {
linkline <- paste (creators [i], " (", dates [i], ") ",
pubTitles [i])
if (volumes [i] != "") {
lineline <- paste0 (linkline, " **", volumes [i])
if (issues [i] != "")
linkline <- paste0 (linkline, " (", issues [i], ")")
linkline <- paste0 (linkline, ": ", pages [i])
}
out <- c (out, paste0 ("## ", paste0 (titles [i], collapse = " ")),
"",
paste0 ("[", linkline, "](", urls [i], ")"),
"",
paste0 ("**Abstract** ", abstracts [i]),
"")
if (!is.null (notes [[i]]))
out <- c (out, "**NOTES**", notes [[i]], "")
}
con <- file ("out.md", "w")
writeLines (out, con = con)
close (con)
rmarkdown::render ("out.md", output_file = "out.html")
#system2 ("xdg-open", c ("out.html", "&"))
```