Created
April 8, 2021 14:09
-
-
Save josepaiva94/c91834935923e8394aa19ed766d8fa51 to your computer and use it in GitHub Desktop.
Bibtex to SQL (SoKMan)
This file contains hidden or 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
const fs = require('fs'); | |
const bibtexParse = require('bibtex-parse-js'); | |
const _escapeString = function (val) { | |
val = val.replace(/[\0\n\r\b\t\\'"\x1a]/g, function (s) { | |
switch (s) { | |
case "\0": | |
return "\\0"; | |
case "\n": | |
return "\\n"; | |
case "\r": | |
return "\\r"; | |
case "\b": | |
return "\\b"; | |
case "\t": | |
return "\\t"; | |
case "\x1a": | |
return "\\Z"; | |
case "--": | |
return "-"; | |
case "'": | |
return "''"; | |
case '"': | |
return '""'; | |
default: | |
return "\\" + s; | |
} | |
}); | |
return val; | |
}; | |
const data = fs.readFileSync('primary-sources.bib', 'utf8'); | |
const samples = bibtexParse.toJSON(data); | |
let sqlData = 'INSERT INTO sok_publication (cite_key, title, year, doi, classified)\nVALUES\n'; | |
for (const sample of samples) { | |
sqlData += `\t("${sample.citationKey}", "${_escapeString(sample.entryTags.title)}", ${sample.entryTags.year}, "${sample.entryTags.doi}", 1),\n` | |
} | |
sqlData = sqlData.slice(0, -2) + ';'; | |
fs.writeFileSync('primary-sources.sql', sqlData); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment