Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
galaxyproject
GitHub Repository: galaxyproject/training-material
Path: blob/main/_plugins/jekyll-scholar.rb
1677 views
1
# frozen_string_literal: true
2
3
require './_plugins/gtn/scholar'
4
5
module Jekyll
6
module Tags
7
8
# Citation Tag which generates the link to the bib + text
9
#
10
# Example:
11
#
12
# {% cite X %}
13
class CiteTag < Liquid::Tag
14
def initialize(tag_name, text, tokens)
15
super
16
@text = text.strip
17
end
18
19
def render(context)
20
page = context.registers[:page]
21
site = context.registers[:site]
22
Gtn::Scholar.load_bib(site)
23
24
# Mark this page as having citations
25
page['cited'] = true
26
27
return "@#{@text}" if page['citation_target'] == 'R'
28
29
# Which page is rendering this tag?
30
source_page = page['path']
31
32
# Citation Frequency
33
site.config['citation_count'] = Hash.new(0) if !site.config.key?('citation_count')
34
site.config['citation_count'][@text] += 1
35
36
# If the overall cache is nil, create it
37
site.config['citation_cache'] = {} if site.config['citation_cache'].nil?
38
# If the individual page in the chace is nil, create it.
39
site.config['citation_cache'][source_page] = [] if site.config['citation_cache'][source_page].nil?
40
41
# Push it to our cache.
42
site.config['citation_cache'][source_page].push(@text)
43
44
begin
45
citation_text = site.config['cached_citeproc'].render(:citation, id: @text)
46
layout = page.fetch('layout', nil)
47
if %w[tutorial_slides base_slides introduction_slides].include? layout
48
doi = site.config['cached_citeproc'].items[@text].doi
49
url = site.config['cached_citeproc'].items[@text].url
50
furl = if !doi.nil?
51
"https://doi.org/#{doi}"
52
elsif !url.nil?
53
url
54
end
55
56
res = if furl.nil?
57
%(<span class="citation">#{citation_text}</span>)
58
else
59
%(<span class="citation"><a href="#{furl}">#{citation_text}</a></span>)
60
end
61
else
62
res = %(<span class="citation"><a href="##{@text}">#{citation_text}</a></span>)
63
end
64
rescue StandardError => e
65
Jekyll.logger.warn "[GTN/scholar] Could not render #{@text} from #{source_page} (#{e})"
66
res = %(<span>ERROR INVALID CITATION #{@text}</span>)
67
end
68
69
res.gsub!(/"/, '\"') if page['citation_target'] == 'jupyter'
70
71
res
72
end
73
end
74
75
# Citation URL tag which just pulls out the URL for the article, most useful in quote citations.
76
#
77
# Example:
78
#
79
# > Some Quote
80
# {: .quote cite="{% cite_url Ramalingam_2004 %}"}
81
class CiteUrlTag < Liquid::Tag
82
def initialize(tag_name, text, tokens)
83
super
84
@text = text.strip
85
end
86
87
def render(context)
88
page = context.registers[:page]
89
site = context.registers[:site]
90
Gtn::Scholar.load_bib(site)
91
92
# Mark this page as having citations
93
page['cited'] = true
94
95
return "@#{@text}" if page['citation_target'] == 'R'
96
97
# Which page is rendering this tag?
98
source_page = page['path']
99
100
# Citation Frequency
101
site.config['citation_count'] = Hash.new(0) if !site.config.key?('citation_count')
102
site.config['citation_count'][@text] += 1
103
104
# If the overall cache is nil, create it
105
site.config['citation_cache'] = {} if site.config['citation_cache'].nil?
106
# If the individual page in the chace is nil, create it.
107
site.config['citation_cache'][source_page] = [] if site.config['citation_cache'][source_page].nil?
108
109
# Push it to our cache.
110
site.config['citation_cache'][source_page].push(@text)
111
112
begin
113
doi = site.config['cached_citeproc'].items[@text].doi
114
url = site.config['cached_citeproc'].items[@text].url
115
if !doi.nil?
116
"https://doi.org/#{doi}"
117
elsif !url.nil?
118
url
119
end
120
res = url
121
rescue StandardError => e
122
Jekyll.logger.warn "[GTN/scholar] Could not render #{@text} from #{source_page} (#{e})"
123
res = %(<span>https://example.com/ERROR+INVALID+CITATION+#{@text}</span>)
124
end
125
res
126
end
127
end
128
129
# Generate Bibliography for all citations used on that page.
130
#
131
# Example:
132
#
133
# {% bibliography %}
134
class BibTag < Liquid::Tag
135
def initialize(tag_name, text, tokens)
136
super
137
@text = text.strip
138
end
139
140
def render(context)
141
site = context.registers[:site]
142
Gtn::Scholar.load_bib(site)
143
# Which page is rendering this tag?
144
source_page = context.registers[:page]['path']
145
global_bib = site.config['cached_global_bib']
146
# citeproc = site.config['cached_citeproc']
147
# We have our page's citations
148
citations = site.config['citation_cache'][source_page] || []
149
# For each of these citation IDs, we need to get the formatted version + pull out
150
# year, month for sorting.
151
unique_citations = citations.each_with_object(Hash.new(0)) do |b, a|
152
a[b] += 1
153
end.keys
154
# Remove nil citations
155
unique_citations = unique_citations.reject { |c| global_bib[c].nil? }
156
# And now sort them by date + names
157
sorted_citations = unique_citations.sort do |a, b|
158
global_bib[a].date.to_s + global_bib[a].names.join(' ') <=>
159
global_bib[b].date.to_s + global_bib[b].names.join(' ')
160
end
161
162
out = '<ol class="bibliography">'
163
out += sorted_citations.map do |c|
164
r = Gtn::Scholar.render_citation(c)
165
%(<li id="#{c}">#{r}</li>)
166
end.join("\n")
167
out += '</ol>'
168
out
169
end
170
end
171
end
172
end
173
174
Liquid::Template.register_tag('cite', Jekyll::Tags::CiteTag)
175
Liquid::Template.register_tag('cite_url', Jekyll::Tags::CiteUrlTag)
176
Liquid::Template.register_tag('bibliography', Jekyll::Tags::BibTag)
177
178