generated from slds-lmu/seminar_website_skeleton
-
Notifications
You must be signed in to change notification settings - Fork 27
/
fix-cits.R
95 lines (77 loc) · 2.49 KB
/
fix-cits.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
citeprog_file = here::here("citeprog-warnings.txt")
cplines = readLines(citeprog_file)
cplines_year = cplines[grepl("Warning--empty year", cplines)]
refs = sapply(cplines_year, function(cp) strsplit(cp, " in ")[[1]][2], USE.NAMES = FALSE)
addEntry = function(cit, bib_file = "book.bib", bib_out = "book.bib.new",
entry = "YEAR", value = "XXXX", line_end = ",") {
blines = readLines(bib_file)
idx_ref = grep(paste0("[{]", cit, ","), blines)
slines = blines[idx_ref]
idx_ref = idx_ref[grep("@", slines)]
if (length(idx_ref) == 0) {
warning("Cannot find ref ", cit)
} else {
blines_top = blines[seq_len(idx_ref)]
blines_bottom = blines[seq(idx_ref + 1, length(blines))]
bline_add = paste0(" ", entry, " = {", value, "}", line_end)
blines_new = c(blines_top, bline_add, blines_bottom)
writeLines(blines_new, bib_out)
}
return(NULL)
}
removeEntry = function(cit, bib_file = "book.bib", bib.out = "book.bib.new", entry) {
blines = readLines(bib_file)
idx_ref = grep(cit, blines)
blines_new = blines[-idx_ref]
writeLines(blines_new, bib.out)
return(NULL)
}
invisible(lapply(refs, addEntry, bib_file = "book.bib.new", bib_out = "book.bib.new"))
### Merge bibfiles:
library(bib2df)
df <- bib2df("book.bib")
df = bibtex::read.bib("book.bib")
getRefs = function(file = "book.bib") {
lines = readLines(file)
idx_ref = grep("@", lines)
sapply(lines[idx_ref], function(l) {
a = strsplit(l, "[{]")[[1]][2]
return(strsplit(a, ",")[[1]][1])
}, USE.NAMES = FALSE)
}
getRefLines = function(ref, file = "book.bib") {
lines = readLines(file)
lstart = grep(ref, lines, perl = TRUE)
lend = lstart + 1
rtype = strsplit(lines[lstart], "[{]")[[1]][1]
rtype = gsub("@", "", rtype)
found_end = FALSE
while(! found_end) {
if (grepl("@", lines[lend]) || (lend == length(lines))) {
found_end = TRUE
} else {
lend = lend + 1
}
}
return(lines[seq(lstart, lend - 1)])
}
#getRefLines(ref)
#rfs = getRefs("book.bib")
mergeBibs = function(bprio, badd, bnew) {
refs_prio = getRefs(bprio)
refs_add = getRefs(badd)
refs_add = refs_add[! refs_add %in% refs_prio]
ls = readLines(bprio)
for (ref in refs_add) {
message(ref)
ls = c(ls, getRefLines(ref, badd))
}
writeLines(ls, bnew)
}
#mergeBibs("book.bib", "bibfiles/03-01img2text_luyang.bib", "tmp.bib")
bfiles = list.files("bibfiles", full.names = TRUE)
mergeBibs("book.bib", bfiles[1], "tmp.bib")
for (bf in bfiles[-1]) {
message("Reading:", bf)
mergeBibs("tmp.bib", bf, "tmp.bib")
}