|
|
#!/usr/bin/env node |
|
|
|
|
|
import { readFileSync, writeFileSync, existsSync } from 'fs'; |
|
|
import { join, dirname, basename } from 'path'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function cleanBibliography(inputBibFile, outputBibFile) { |
|
|
if (!existsSync(inputBibFile)) { |
|
|
console.log(' β οΈ No bibliography file found:', inputBibFile); |
|
|
return false; |
|
|
} |
|
|
|
|
|
console.log('π Cleaning bibliography...'); |
|
|
let bibContent = readFileSync(inputBibFile, 'utf8'); |
|
|
|
|
|
|
|
|
bibContent = bibContent.replace(/file = \{[^}]+\}/g, ''); |
|
|
|
|
|
|
|
|
bibContent = bibContent.replace(/,\s*\n\s*\n/g, '\n\n'); |
|
|
bibContent = bibContent.replace(/,\s*\}/g, '\n}'); |
|
|
|
|
|
|
|
|
bibContent = bibContent.replace(/,,/g, ','); |
|
|
|
|
|
|
|
|
bibContent = bibContent.replace(/,(\s*\n\s*)\}/g, '$1}'); |
|
|
|
|
|
writeFileSync(outputBibFile, bibContent); |
|
|
console.log(` π Clean bibliography saved: ${outputBibFile}`); |
|
|
|
|
|
return true; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function main() { |
|
|
const args = process.argv.slice(2); |
|
|
|
|
|
if (args.includes('--help') || args.includes('-h')) { |
|
|
console.log(` |
|
|
π BibTeX Bibliography Cleaner |
|
|
|
|
|
Usage: |
|
|
node bib-cleaner.mjs [input.bib] [output.bib] |
|
|
node bib-cleaner.mjs --input=input.bib --output=output.bib |
|
|
|
|
|
Options: |
|
|
--input=FILE Input .bib file |
|
|
--output=FILE Output cleaned .bib file |
|
|
--help, -h Show this help |
|
|
|
|
|
Examples: |
|
|
# Clean main.bib to clean.bib |
|
|
node bib-cleaner.mjs main.bib clean.bib |
|
|
|
|
|
# Using flags |
|
|
node bib-cleaner.mjs --input=references.bib --output=clean-refs.bib |
|
|
`); |
|
|
process.exit(0); |
|
|
} |
|
|
|
|
|
let inputFile, outputFile; |
|
|
|
|
|
|
|
|
if (args.length >= 2 && !args[0].startsWith('--')) { |
|
|
|
|
|
inputFile = args[0]; |
|
|
outputFile = args[1]; |
|
|
} else { |
|
|
|
|
|
for (const arg of args) { |
|
|
if (arg.startsWith('--input=')) { |
|
|
inputFile = arg.split('=')[1]; |
|
|
} else if (arg.startsWith('--output=')) { |
|
|
outputFile = arg.split('=')[1]; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if (!inputFile || !outputFile) { |
|
|
console.error('β Both input and output files are required'); |
|
|
console.log('Use --help for usage information'); |
|
|
process.exit(1); |
|
|
} |
|
|
|
|
|
const success = cleanBibliography(inputFile, outputFile); |
|
|
if (success) { |
|
|
console.log('π Bibliography cleaning completed!'); |
|
|
} else { |
|
|
process.exit(1); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (import.meta.url === `file://${process.argv[1]}`) { |
|
|
main(); |
|
|
} |
|
|
|