Skip to content

Instantly share code, notes, and snippets.

@inukshuk
Last active September 21, 2018 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inukshuk/1c2431c02a800c534386571c4d4a9d59 to your computer and use it in GitHub Desktop.
Save inukshuk/1c2431c02a800c534386571c4d4a9d59 to your computer and use it in GitHub Desktop.
require 'anystyle'
require 'citeproc/ruby'
require 'csl/styles'
# Load all dependent CSL styles and create a Renderer
# Those should be cached!
styles = CSL::Style.list.map { |id| CSL::Style.load id }.reject { |s| s.bibliography.nil? }
cp = CiteProc::Ruby::Renderer.new
# Parse a reference to CSL/JSON
input = 'Knuth, Donald. (1968). The art of computer programming (Vol. 1). Boston: Addison-Wesley.'
items = AnyStyle.parse(input, format: 'csl')
# Generate a output hash for every style
refs = Hash[styles.map { |s|
# Convert the item for use with CiteProc
item = CiteProc::CitationItem.new { |c| c.data = CiteProc::Item.new(items[0]) }
# Render the item
ref = cp.render item, s.bibliography
# TODO compute Levenshtein distance to input (or similar)
score = 0
# Return style id and reference string and score
[s.id, ref, score]
}]
@dshorthouse
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment