Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
galaxyproject
GitHub Repository: galaxyproject/training-material
Path: blob/main/_plugins/gtn/scholar.rb
1677 views
1
# frozen_string_literal: true
2
3
require 'bibtex'
4
require 'citeproc/ruby'
5
require 'csl/styles'
6
7
module Gtn
8
# GTN implementation of Jekyll::Scholar except faster.
9
module Scholar
10
def self.load_bib(site)
11
return if site.config.key?('cached_global_bib')
12
13
(global_bib, cp) = populate_cache
14
site.config['cached_global_bib'] = global_bib
15
site.config['cached_citeproc'] = cp
16
end
17
18
def self.populate_cache
19
@@cache ||= discover_bib
20
end
21
22
def self.render_citation(key)
23
(global_bib, citeproc) = populate_cache
24
25
text = citeproc.render(:bibliography, id: key)[0]
26
entry = global_bib[key]
27
text += " #{entry.note}." if entry.note
28
doi = entry.fetch('doi', nil)
29
text += " <a href=\"https://doi.org/#{doi}\">#{doi}</a>" if doi
30
url = entry.fetch('url', nil)
31
text += " <a href=\"#{url}\">#{url}</a>" if url && !(url.index('doi.org') && entry.doi)
32
isbn = entry.fetch('isbn', nil)
33
text += " ISBN: #{isbn}" if isbn
34
35
text
36
end
37
38
def self.discover_bib
39
Jekyll.logger.info '[GTN/scholar] Creating global bib cache'
40
global_bib = BibTeX::Bibliography.new
41
bib_paths = [Find.find('./topics'), Find.find('./faqs'), Find.find('./news')].lazy.flat_map(&:lazy).grep(/bib$/)
42
bib_paths.each do |path|
43
BibTeX.open(path).each do |x|
44
x = x.convert_latex
45
global_bib << x
46
end
47
end
48
Jekyll.logger.info "[GTN/scholar] Done: #{global_bib.length}"
49
style = CSL::Style.load('_layouts/g3.csl')
50
cp = CiteProc::Processor.new style: style,
51
format: 'html', locale: 'en'
52
cp.import global_bib.to_citeproc
53
54
[global_bib, cp]
55
end
56
end
57
end
58
59