Skip to content

Instantly share code, notes, and snippets.

@bcdavasconcelos
Last active May 4, 2022 14:35
Show Gist options
  • Save bcdavasconcelos/7931523780dde34faf1954db1a601eee to your computer and use it in GitHub Desktop.
Save bcdavasconcelos/7931523780dde34faf1954db1a601eee to your computer and use it in GitHub Desktop.
I use this script to find bibliographical @references cited in my markdown files (using the Pandoc syntax) that happen to be absent from the bibliography file.
#!/usr/bin/env ruby
# frozen_string_literal: false
# I use this script to find bibliographical @references (using the Pandoc syntax) cited in my markdown files
# that happen to be absent in the bibliography file.
# Pandoc syntax: [@Ref] → (Author + Year) / [-@Ref] → (Year)
Encoding.default_external = Encoding::UTF_8
path = '/path/to/markdown/file.md'
bib_file = '/path/to/bib/file.bib'
md_input = File.open(path, 'r').read.split(/\n/)
bib = File.open(bib_file, 'r').read
found_references = []
missing_refs = []
md_input.each do |line|
found_references.push(line.scan(/(?<!\\|\w|\+|\*|{|-|!)-*@(?!})(?!tbl)(?!fig)(?!sec)(?!eq)(\b.+?\b)/)) unless line.empty? || line.nil?
end
references = found_references.flatten.sort.uniq
references.each do |ref|
found_item = bib.scan(ref).flatten[0]
if found_item.nil?
missing_refs.push(ref)
end
end
puts "Missing references: " + missing_refs.join(', ')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment