-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreflow.R
356 lines (308 loc) · 9.7 KB
/
reflow.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
{
library(tidyverse)
library(officer)
library(dplyr)
library(stringr)
library(dbplyr)
library(RSQLite)
library(jsonlite)
}
proj_path <- getwd()
input_path <- 'INPUT'
output_path <- 'OUTPUT'
data_path <- 'DATA'
search_title <- 'Big Data'
find_date_lang_format <- function(sample) {
langs <- c('pl', 'eng')
score_pl <- unlist(gregexpr("\\d{1,2}\\s\\p{L}+\\s\\d{4}", sample, perl = TRUE))
score_eng <- unlist(gregexpr("[A-Za-z]+ \\d{1,2}, \\d{4}", sample, perl = TRUE))
datelang <- langs[c(score_pl > 0, score_eng > 0)]
return(datelang)
}
remove_dates <- function(text, datelang) {
pattern <- switch(datelang,
'pl' = "\\d{1,2}\\s\\p{L}+\\s\\d{4}",
'eng' = "[A-Za-z]+ \\d{1,2}, \\d{4}",
stop("specify 'pl' or 'eng'"))
text_mod <- str_replace(text, pattern, " Page ")
text_mod <- str_trim(text_mod)
return(text_mod)
}
remove_links <- function(text) {
match <- unlist(gregexpr('HYPERLINK[^"]+"', text, perl = TRUE))
if (match > 0) {
text_clean <- gsub('\\s(HYPERLINK)\\s\"\\w.*\"\\s', '', text)
return(text_clean)
} else {
return(text)
}
}
quote_transform <- function(text, admonitions) {
if (substr(text, 1, 2) == "##") {
return(text)
}
match <- gregexpr("Page\\s\\d+\\s*(.*?)($|\n(?=Page))", text, perl = TRUE)
page_nums <- regmatches(text, match)[[1]]
text <- regmatches(text, match, invert = TRUE)[[1]]
quote_format <- if (admonitions) "> [!quote] " else "> "
quotes <- paste0(quote_format, page_nums, "\n> ", text[1])
return(paste(quotes, collapse = "\n\n"))
}
getGooglePlay <- function(title_docx, admonitions = TRUE) {
doc <- read_docx(file.path(proj_path, input_path, title_docx))
text <- docx_summary(doc)
text <- replace(text,is.na(text),"")
text <- filter(text, text != '')
text <- text[2:4]
text$text[text$style_name == "heading 2"] <- paste("###", text$text[text$style_name == "heading 2"], " \n", sep = " ")
row_num <- which(startsWith(text$text, "##"))[1]
text <- text[row_num:length(text$text),]
format_d <- find_date_lang_format(text$text[2])
text$text <- unlist(lapply(text$text, remove_dates, datelang = format_d))
text$text <- unlist(lapply(text$text, remove_links))
text$text <- unlist(lapply(text$text, quote_transform, admonitions = admonitions))
md_text <- paste(text$text, collapse = "\n\n")
title_md <- gsub('\\.docx', '.md', title_docx)
writeLines(md_text, file.path(proj_path, output_path, title_md))
print("DONE!")
}
# Function to append the front-matter at the beginning of the file
meta_appender <- function(filename, new_text) {
# Read the contents of the file into a character vector
lines <- readLines(filename)
# Concatenate the new text and the existing lines into character vector
lines <- c(new_text, lines)
# Write the modified lines back to the file
writeLines(lines, filename)
}
# Function to construct the front-matter
fmatter_constructor <- function(meta) {
# Conditionality on translator
if (meta$original_language == '') {
citation <-
paste0(
"'",
meta$author,
", ",
meta$title,
", ",
meta$publisher,
", ",
meta$pub_place,
" ",
meta$pub_year,
"'"
)
} else {
citation <-
paste0(
"'",
meta$author,
", ",
meta$title,
", tłum. ",
meta$translator,
", ",
meta$publisher,
", ",
meta$pub_place,
" ",
meta$pub_year,
"'"
)
}
note_meta <- paste0(
"---\n# NOTE-META\ncreated: ", as.character(today()), "\n",
"type: 'reference'\n",
"subtype: 'book'\n",
"status: 'DRAFT'\n",
"tags: [Wiedza/Streszczenia, ]\n",
"alias: \n",
"#\n"
)
object_meta <- paste0(
"# OBJECT-META\n",
"title: '", meta$title, "'\n",
"shorttitle: ", meta$short_title, "\n",
"original_title: ", meta$original_title, "\n",
"author: '", meta$author, "'\n",
"coauthors: ", meta$coauthors, "\n",
"translator: ", meta$translator, "\n",
"subject: \n",
"language: ", meta$language, "\n",
"original_language: ", meta$original_language, "\n",
"pages: ", meta$pages, "\n",
"pub_date: ", meta$pub_date, "\n",
"pub_place: ", meta$pub_place, "\n",
"publisher: ", meta$publisher, "\n",
"ISBN: ", meta$isbn, "\n",
"OCLC: ", meta$oclc, "\n",
"#\n"
)
sub_meta <- paste0("# SUB-META\n",
"read: TRUE\n",
"read_year: \n",
"citation: ", citation, "\n---\n")
fmatter <- paste0(note_meta, object_meta, sub_meta)
return(fmatter)
}
create_manifest <- function(){
meta <- as.data.frame(matrix(NA, ncol = 15, nrow = 1))
names(meta) <-
c(
"title",
"original_title",
"short_title",
"publisher",
"author",
"coauthors",
"translator",
"isbn",
"oclc",
"language",
"original_language",
"pages",
"pub_date",
"pub_year",
"pub_place"
)
return(meta)
}
fill_frontmatter <- function(search_title, filename) {
# ... (The rest of the code is unchanged)
# Exec
fmatter <- fmatter_constructor(meta)
meta_appender(filename, fmatter)
}
get_callibre <- function(search_title){
con <- dbConnect(SQLite(), dbname = file.path(proj_path,data_path,"metadata.db"))
query <-
paste0(
"SELECT books.title, authors.name AS author, books.pubdate, publishers.name AS publisher, languages.lang_code AS lang, custom_column_11.value AS pages, identifiers.val, identifiers.type
FROM books
LEFT JOIN books_authors_link ON books.id = books_authors_link.book
LEFT JOIN authors ON books_authors_link.author = authors.id
LEFT JOIN books_publishers_link ON books.id = books_publishers_link.book
LEFT JOIN publishers ON books_publishers_link.publisher = publishers.id
LEFT JOIN books_languages_link ON books.id = books_languages_link.book
LEFT JOIN languages ON books_languages_link.lang_code = languages.id
LEFT JOIN custom_column_11 ON books.id = custom_column_11.book
LEFT JOIN identifiers ON books.id = identifiers.book
WHERE title LIKE '",
search_title,
#"' AND identifiers.type = 'isbn';"
"';"
)
meta_calibre <- dbGetQuery(con, query)
meta_calibre<- meta_calibre %>% filter(type == 'isbn')
# Close the connection to the database
dbDisconnect(con)
rm(con)
return(meta_calibre)
}
get_bn <- function(meta){
#if ISBN missing then use different condition
# if (isbn_exist) {
bn_url <-
paste0("https://data.bn.org.pl/api/institutions/bibs.json?isbnIssn=",
meta$isbn)
#} else {
# bn_url <-
# URLencode(
# paste0(
# "https://data.bn.org.pl/api/institutions/bibs.json?title=",
# meta$title,
# "&limit=1"
#)
# )
#}
json_object_bn <- fromJSON(bn_url)
pos <-
which(!is.na(json_object_bn[["bibs"]][["marc"]][["fields"]][[1]][["041"]][["ind1"]]))
if (length(pos) >= 1) {
meta$original_language <-
json_object_bn[["bibs"]][["marc"]][["fields"]][[1]][["041"]][["subfields"]][[pos]][["h"]][2]
} else {
rm(pos)
}
if (!isbn_exist) {
meta$isbn <- json_object_bn[["bibs"]][["isbnIssn"]]
}
oc <- json_object_bn[["bibs"]][["marc"]][["fields"]][[1]][["035"]][3]
uoc <- unlist(oc, recursive = TRUE, use.names = FALSE)
ocl <- grep("^\\(OCoLC\\)", uoc, value = TRUE)
if (length(ocl)!=0) {
meta$oclc <- substr(ocl, start = 8, stop = 100)
}
plc <-
json_object_bn[["bibs"]][["marc"]][["fields"]][[1]][["260"]][["subfields"]]
plb <- unlist(plc, recursive = TRUE, use.names = FALSE)
pub_place <- plb[1]
meta$pub_place <- substring(pub_place, 1, nchar(pub_place) - 2)
if (!is.na(meta$original_language)) {
tl <-
json_object_bn[["bibs"]][["marc"]][["fields"]][[1]][["700"]][["subfields"]]
tl <- unlist(tl, recursive = TRUE, use.names = FALSE)
translator <- tl[1]
translator <- strsplit(translator, " ")[[1]]
translator <- rev(translator)
translator <- paste(translator, collapse = " ")
meta$translator <- substring(translator, 1, nchar(translator) - 1)
} else {
meta$translator <- ''
}
if (!is.na(meta$original_language)) {
pos <-
which(!is.na(json_object_bn[["bibs"]][["marc"]][["fields"]][[1]][["246"]][["ind1"]]))
or <-
json_object_bn[["bibs"]][["marc"]][["fields"]][[1]][["246"]][["subfields"]][pos]
or <- unlist(or, recursive = TRUE, use.names = FALSE)
or <- or[!is.na(or)]
#original_title<-paste0(or[2],or[3])
char <- ","
og <- or[2]
if (substr(og, nchar(og), nchar(og)) == char) {
meta$original_title <- og
} else {
meta$original_title <- paste0(or[2], or[3])
}
} else {
meta$original_title <- ''
meta$original_language <- ''
}
}
get_callibre(search_title)
#-----META------
meta<-create_manifest()
meta_calibre<-get_callibre(search_title)
meta$title <- as.character(meta_calibre$title[1])
meta$short_title <- str_extract(meta$title, "[^.:]+")
meta$publisher <- as.character(meta_calibre$publisher[1])
meta$author <- as.character(meta_calibre$author[1])
#coauthors
if (length(meta_calibre$author) > 1) {
meta$coauthors <-
paste0('[', paste(meta_calibre$author[2:length(meta_calibre$author)], collapse =
", "), ']')
} else {
meta$coauthors <- ''
}
meta$isbn <- as.character(meta_calibre$val)
isbn_exist <- !is.na(meta$isbn)
meta$language <- as.character(meta_calibre$lang[1])
meta$pages <- as.numeric(meta_calibre$pages[1])
pub_date_raw <- as.POSIXct(
as.character.Date(meta_calibre$pubdate[1]),
tz = "",
tryFormats = c(
"%Y-%m-%d %H:%M:%OS",
"%Y/%m/%d %H:%M:%OS",
"%Y-%m-%d %H:%M",
"%Y/%m/%d %H:%M",
"%Y-%m-%d",
"%Y/%m/%d"
)
)
meta$pub_date <- format(pub_date_raw, "%Y-%m-%d")
meta$pub_year <- format(pub_date_raw, "%Y")
get_bn(meta)