# The function: bibtex_2academic bibtex_2academic <- function(bibfile, outfold, abstract = TRUE, overwrite = FALSE) { if (!require("pacman")) install.packages("pacman") pacman::p_load(RefManageR, dplyr, stringr, anytime, tidyr, stringi) # Import the bibtex file and convert to data.frame mypubs <- ReadBib(bibfile, check = "warn", .Encoding = "UTF-8") %>% as.data.frame() # This section is for creating columns if not existing (as in the case of pure presentations collections) fncols <- function(data, cname) { add <-cname[!cname%in%names(data)] if(length(add)!=0) data[add] <- NA data } mypubs <-fncols(mypubs, c("journal", "abstract", "annotation", "editor", "booktitle", "volume", "number", "pages", "address", "institution", "publisher", "doi", "isbn", "url", "year", "month", "school")) mypubs <- mypubs %>% mutate(mainref = journal) mypubs$mainref <- as.character(mypubs$mainref) mypubs$mainref <- mypubs$mainref %>% replace_na(' ') #otherwise it appears "NA" in post mypubs$bibtype <-str_replace_all(mypubs$bibtype, c("Misc" = "Presentation")) # str_replace_all(mypubs$bibtype, c("PhdThesis" = "Thesis")) mypubs$abstract <- mypubs$abstract %>% replace_na('(Abstract not available)') #otherwise it appears "NA" in post # relplace NAs in month mypubs$month <- as.character(mypubs$month) mypubs$month <- mypubs$month %>% replace_na("jan") #otherwise it appears "NA" in post # mypubs$annotation <- mypubs$annotation %>% replace_na('image_preview = ""') #otherwise # Customize Zotero extra field (here "annotation") in order to leave there the additional information for the md file #clean backslashes generated by conversion of underline characters in links (_) & other special characters # mypubs$annotation<- gsub( # pattern = ('(.+)---'), # replacement = '\n---\n', # x = mypubs$annotation # ) mypubs$annotation<- gsub( pattern = ('--- '), replacement = '---\n', x = mypubs$annotation ) mypubs$annotation<- gsub( pattern = (' ---'), replacement = '\n---\n', x = mypubs$annotation ) mypubs$annotation<- gsub( pattern = ('\\\\'), replacement = '', x = mypubs$annotation ) mypubs$abstract<- gsub( pattern = ('\\\\'), replacement = '', x = mypubs$abstract ) mypubs$mainref<- gsub( pattern = ('\\\\'), replacement = '', x = mypubs$mainref ) mypubs$annotation<- gsub( pattern = ('\\{'), replacement = '', x = mypubs$annotation ) mypubs$annotation<- gsub( pattern = ('\\}'), replacement = '', x = mypubs$annotation ) mypubs$annotation<- gsub( pattern = ('\\$'), replacement = '', x = mypubs$annotation ) # Further customization of annotation for including yaml information in Zotero Extra field # mypubs$annotation <-gsub("url_pdf", "\nurl_pdf",mypubs$annotation ) mypubs$annotation <-gsub("url_preprint", "\nurl_preprint",mypubs$annotation ) mypubs$annotation <-gsub("url_dataset", "\nurl_dataset",mypubs$annotation ) mypubs$annotation <-gsub("url_project", "\nurl_project",mypubs$annotation ) mypubs$annotation <-gsub("url_slides", "\nurl_slides",mypubs$annotation ) mypubs$annotation <-gsub("url_video", "\nurl_video",mypubs$annotation ) mypubs$annotation <-gsub("url_poster", "\nurl_poster",mypubs$annotation ) mypubs$annotation <-gsub("links", "\nlinks",mypubs$annotation ) mypubs$annotation <-gsub("- icon", "\n- icon",mypubs$annotation ) mypubs$annotation <-gsub(" icon_pack", "\n icon_pack",mypubs$annotation ) mypubs$annotation <-gsub(" name:", "\n name:",mypubs$annotation ) mypubs$annotation <-gsub(" web:", "\n url:",mypubs$annotation ) # something does not work when trying to replace "url:" from Zotero, that's why in the extra field use web for this function, which then is replaced with the url field and it works. # Customize for more than one editor mypubs$editor<- gsub( pattern = (' and '), replacement = ', ', x = mypubs$editor ) mypubs$editor<- stri_replace_last_fixed(mypubs$editor, ',', ' &') # add characters between tags for properly render mypubs$keywords<- gsub( pattern = (','), replacement = '","', x = mypubs$keywords ) #add line breaks for the different entries # mypubs$annotation<-cat(stri_wrap(mypubs$annotation, whitespace_only = TRUE)) # assign "categories" to the different types of publications mypubs <- mypubs %>% dplyr::mutate( pubtype = dplyr::case_when(bibtype == "Article" ~ "2", bibtype == "Article in Press" ~ "2", bibtype == "InProceedings" ~ "1", bibtype == "Proceedings" ~ "1", bibtype == "Conference" ~ "1", bibtype == "Conference Paper" ~ "1", bibtype == "Thesis" ~ "3", bibtype == "MastersThesis" ~ "3", bibtype == "PhdThesis" ~ "3", bibtype == "Manual" ~ "4", bibtype == "TechReport" ~ "4", bibtype == "Book" ~ "5", bibtype == "InCollection" ~ "6", bibtype == "InBook" ~ "6", bibtype == "Presentation" ~ "8", TRUE ~ "0")) # create a function which populates the md template based on the info # about a publication create_md <- function(x) { # define a date and create filename by appending date and start of title if (!is.na(x[["year"]])) { x[["date"]] <- paste0(x[["year"]]) } else { x[["date"]] <- "2999-01-01" } # define a date for the bibtex record (with month) x[["date2"]] <- paste0(x[["year"]], "-", x[["month"]] , "-", "01" ) filename <- paste(x[["date"]], x[["title"]] %>% str_replace_all(fixed(" "), "_") %>% str_remove_all(fixed(":")) %>% str_sub(1, 20) %>% paste0(".md"), sep = "_") # start writing if (!file.exists(file.path(outfold, filename)) | overwrite) { fileConn <- file.path(outfold, filename) write("---", fileConn) # Title and date write(paste0("title : \"", x[["title"]], "\""), fileConn, append = T) write(paste0("date : \"", anydate(x[["date2"]]), "\""), fileConn, append = T) # write(paste0("date : \"", x[["year"]], "-", x[["month"]] , "-", "01" , "\""), fileConn, append = T) # Authors. Comma separated list, e.g. `["Bob Smith", "David Jones"]`. auth_hugo <- str_replace_all(x["author"], " and ", "\", \"") auth_hugo <- stringi::stri_trans_general(auth_hugo, "latin-ascii") write(paste0("authors : [\"", auth_hugo,"\"]"), fileConn, append = T) # Publication type. Legend: # 0 = Uncategorized, 1 = Conference paper, 2 = Journal article # 3 = Manuscript, 4 = Report, 5 = Book, 6 = Book section write(paste0("publication_types : [\"", x[["pubtype"]],"\"]"), fileConn, append = T) # Publication details publication <- x[["mainref"]] if (!is.na(x[["editor"]])) publication <- paste0(publication, " In ", x[["editor"]], ": ") if (!is.na(x[["booktitle"]])) publication <- paste0(publication, x[["booktitle"]], "") if (!is.na(x[["volume"]])) publication <- paste0(publication, ", ", x[["volume"]], "") if (!is.na(x[["type"]])) publication <- paste0(publication, x[["type"]], " ") if (!is.na(x[["number"]])) publication <- paste0(publication, "(", x[["number"]], ")") if (!is.na(x[["pages"]])) publication <- paste0(publication, " ", x[["pages"]], " ") if (!is.na(x[["school"]])) publication <- paste0(publication, "- ", x[["school"]]) if (!is.na(x[["address"]])) publication <- paste0(publication, ". ", x[["address"]], "") if (!is.na(x[["institution"]])) publication <- paste0(publication, " ", x[["institution"]]) if (!is.na(x[["publisher"]])) publication <- paste0(publication, ": ", x[["publisher"]]) if (!is.na(x[["doi"]])) publication <- paste0(publication, " ", paste0("https://doi.org/", x[["doi"]])) if (!is.na(x[["isbn"]])) publication <- paste0(publication, ". ISBN: ", paste0(x[["isbn"]])) write(paste0("publication : \"", publication,"\""), fileConn, append = T) write(paste0("publication_short : \"", publication,"\""),fileConn, append = T) # Abstract and optional shortened version. write(paste0("abstract : \"", x[["abstract"]], "\""), fileConn, append = T) # if (abstract) { # write(paste0("abstract = \"", x[["abstract"]],"\""), fileConn, append = T) # } else { # write("abstract = \"\"", fileConn, append = T) # } write(paste0("abstract_short : \"","\""), fileConn, append = T) # Source document from Zotero URL field if (!is.na(x[["url"]])) write(paste0("url_source : \"", x[["url"]], "\""), fileConn, append = T) if (!is.na(x[["keywords"]])) write(paste0("tags : [\"", x[["keywords"]], "\"]"), fileConn, append = T) # other possible fields are kept empty. They can be customized later by # editing the created md # write("url_code = \"\"", fileConn, append = T) # write("image_preview = \"\"", fileConn, append = T) # write("selected = false", fileConn, append = T) # write("projects = []", fileConn, append = T) #links # write("url_pdf = \"\"", fileConn, append = T) # write("url_preprint = \"\"", fileConn, append = T) # write("url_dataset = \"\"", fileConn, append = T) # write("url_project = \"\"", fileConn, append = T) # write("url_slides = \"\"", fileConn, append = T) # write("url_video = \"\"", fileConn, append = T) # write("url_poster = \"\"", fileConn, append = T) #other stuff # write("math = true", fileConn, append = T) # write("highlight = true", fileConn, append = T) # Featured image # write("[header]", fileConn, append = T) # write("image = \"\"", fileConn, append = T) # write("caption = \"\"", fileConn, append = T) # write("---", fileConn, append = T) # Any other relevant information from Zotero: write in the "Extra" field if (!is.na(x[["annotation"]])) write(paste0(x[["annotation"]]), fileConn, append = T) } } # apply the "create_md" function over the publications list to generate # the different "md" files. apply(mypubs, FUN = function(x) create_md(x), MARGIN = 1) } # Running the function for publications my_bibfile <- "content/publication/academic-publications.bib" out_fold <- "content/publication" bibtex_2academic(bibfile = my_bibfile, outfold = out_fold, abstract = TRUE, overwrite = TRUE) #this is for including my presentations collection together with my publications in the same page for the sake of simplicity my_bibfile <- "content/publication/academic-presentations.bib" out_fold <- "content/publication" bibtex_2academic(bibfile = my_bibfile, outfold = out_fold, abstract = TRUE, overwrite = TRUE) # and this is for including my theses, which actually are in another page (tesis), but I let the script here in order to run it just once for the sake of simplicity my_bibfile <- "content/tesis/tesis.bib" out_fold <- "content/tesis" bibtex_2academic(bibfile = my_bibfile, outfold = out_fold, abstract = TRUE, overwrite = TRUE)