Path: blob/main/_plugins/gtn/scholar.rb
1677 views
# frozen_string_literal: true12require 'bibtex'3require 'citeproc/ruby'4require 'csl/styles'56module Gtn7# GTN implementation of Jekyll::Scholar except faster.8module Scholar9def self.load_bib(site)10return if site.config.key?('cached_global_bib')1112(global_bib, cp) = populate_cache13site.config['cached_global_bib'] = global_bib14site.config['cached_citeproc'] = cp15end1617def self.populate_cache18@@cache ||= discover_bib19end2021def self.render_citation(key)22(global_bib, citeproc) = populate_cache2324text = citeproc.render(:bibliography, id: key)[0]25entry = global_bib[key]26text += " #{entry.note}." if entry.note27doi = entry.fetch('doi', nil)28text += " <a href=\"https://doi.org/#{doi}\">#{doi}</a>" if doi29url = entry.fetch('url', nil)30text += " <a href=\"#{url}\">#{url}</a>" if url && !(url.index('doi.org') && entry.doi)31isbn = entry.fetch('isbn', nil)32text += " ISBN: #{isbn}" if isbn3334text35end3637def self.discover_bib38Jekyll.logger.info '[GTN/scholar] Creating global bib cache'39global_bib = BibTeX::Bibliography.new40bib_paths = [Find.find('./topics'), Find.find('./faqs'), Find.find('./news')].lazy.flat_map(&:lazy).grep(/bib$/)41bib_paths.each do |path|42BibTeX.open(path).each do |x|43x = x.convert_latex44global_bib << x45end46end47Jekyll.logger.info "[GTN/scholar] Done: #{global_bib.length}"48style = CSL::Style.load('_layouts/g3.csl')49cp = CiteProc::Processor.new style: style,50format: 'html', locale: 'en'51cp.import global_bib.to_citeproc5253[global_bib, cp]54end55end56end575859