Last active
June 16, 2021 13:51
-
-
Save jmclawson/882c7ff79e5e9fdf37973a38ca52571a to your computer and use it in GitHub Desktop.
Pre-process a bib file for clean use in documentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# To prepare it for use in documentation, import a .bib file, strip Bibdesk's extra fields and additions, and enclose each entry with code compatible with Latex's {listings} package. | |
library(dplyr) | |
library(stringr) | |
library(readr) | |
# 0. Set relative file path for the bibfile | |
# setwd() | |
# 1. read the bib file as a vector of lines | |
bibdata <- scan("handbook9.bib", what="character", sep = "\n") %>% .[-c(1:4)] | |
# 2. Drop the smart list from the end | |
bibdata <- bibdata[-c(grep("@comment\\{BibDesk Smart Groups",bibdata):length(bibdata))] | |
# 3. Find all the lines that include undesirable fields created by Bibdesk and add these line numbers to a vector. Make sure to exclude any line numbers that are at the end of a source entry. | |
badlines <- grep("\tBdsk-Url-1|\tdate-added|\tdate-modified",bibdata) %>% | |
.[!c(. %in% grep("}}",bibdata))] | |
# 4. Drop these bad lines | |
bibdata <- bibdata[-badlines] | |
# 5. Index the start and end of each entry | |
startlines <- grep("^@",bibdata) | |
endlines <- | |
(startlines[2:length(startlines)]-1) %>% | |
c(length(bibdata)) | |
# 6. Tidy up any remaining undesirable fields that were left behind (because they were the last line in a source entry) by reducing them to just the closing bracket. | |
bibdata[grep("\tBdsk-Url-1|\tdate-added|\tdate-modified",bibdata)] <- "}" | |
# 7. Get a list of cite keys in order. | |
citekeys <- bibdata[startlines] %>% | |
str_remove("^@[a-z]*\\{") %>% | |
str_remove(",") | |
# 8. Start each entry with a {listings}-compatible opening that references its cite key. | |
bibdata[startlines] <- | |
paste("%[[", citekeys, "]]")%>% | |
paste(bibdata[startlines], sep="\n") | |
# 9. End each entry with a {listings}-compatible close. | |
bibdata[endlines] <- | |
paste("%[[", "end", "]]")%>% | |
paste(bibdata[endlines], ., sep="\n") | |
# 10. Convert the vector to a single block of text. | |
bibtext <- bibdata %>% | |
paste(collapse = "\n") | |
# 11. Clean up any dangling brackets. | |
bibtext <- gsub("\n}","}", bibtext) %>% | |
gsub(",}","}",.) | |
# 12. Save it to a new file. | |
write_file(bibtext, "handbook9_clean.bib") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment