Path: blob/main/_plugins/jekyll-scholar.rb
1677 views
# frozen_string_literal: true12require './_plugins/gtn/scholar'34module Jekyll5module Tags67# Citation Tag which generates the link to the bib + text8#9# Example:10#11# {% cite X %}12class CiteTag < Liquid::Tag13def initialize(tag_name, text, tokens)14super15@text = text.strip16end1718def render(context)19page = context.registers[:page]20site = context.registers[:site]21Gtn::Scholar.load_bib(site)2223# Mark this page as having citations24page['cited'] = true2526return "@#{@text}" if page['citation_target'] == 'R'2728# Which page is rendering this tag?29source_page = page['path']3031# Citation Frequency32site.config['citation_count'] = Hash.new(0) if !site.config.key?('citation_count')33site.config['citation_count'][@text] += 13435# If the overall cache is nil, create it36site.config['citation_cache'] = {} if site.config['citation_cache'].nil?37# If the individual page in the chace is nil, create it.38site.config['citation_cache'][source_page] = [] if site.config['citation_cache'][source_page].nil?3940# Push it to our cache.41site.config['citation_cache'][source_page].push(@text)4243begin44citation_text = site.config['cached_citeproc'].render(:citation, id: @text)45layout = page.fetch('layout', nil)46if %w[tutorial_slides base_slides introduction_slides].include? layout47doi = site.config['cached_citeproc'].items[@text].doi48url = site.config['cached_citeproc'].items[@text].url49furl = if !doi.nil?50"https://doi.org/#{doi}"51elsif !url.nil?52url53end5455res = if furl.nil?56%(<span class="citation">#{citation_text}</span>)57else58%(<span class="citation"><a href="#{furl}">#{citation_text}</a></span>)59end60else61res = %(<span class="citation"><a href="##{@text}">#{citation_text}</a></span>)62end63rescue StandardError => e64Jekyll.logger.warn "[GTN/scholar] Could not render #{@text} from #{source_page} (#{e})"65res = %(<span>ERROR INVALID CITATION #{@text}</span>)66end6768res.gsub!(/"/, '\"') if page['citation_target'] == 'jupyter'6970res71end72end7374# Citation URL tag which just pulls out the URL for the article, most useful in quote citations.75#76# Example:77#78# > Some Quote79# {: .quote cite="{% cite_url Ramalingam_2004 %}"}80class CiteUrlTag < Liquid::Tag81def initialize(tag_name, text, tokens)82super83@text = text.strip84end8586def render(context)87page = context.registers[:page]88site = context.registers[:site]89Gtn::Scholar.load_bib(site)9091# Mark this page as having citations92page['cited'] = true9394return "@#{@text}" if page['citation_target'] == 'R'9596# Which page is rendering this tag?97source_page = page['path']9899# Citation Frequency100site.config['citation_count'] = Hash.new(0) if !site.config.key?('citation_count')101site.config['citation_count'][@text] += 1102103# If the overall cache is nil, create it104site.config['citation_cache'] = {} if site.config['citation_cache'].nil?105# If the individual page in the chace is nil, create it.106site.config['citation_cache'][source_page] = [] if site.config['citation_cache'][source_page].nil?107108# Push it to our cache.109site.config['citation_cache'][source_page].push(@text)110111begin112doi = site.config['cached_citeproc'].items[@text].doi113url = site.config['cached_citeproc'].items[@text].url114if !doi.nil?115"https://doi.org/#{doi}"116elsif !url.nil?117url118end119res = url120rescue StandardError => e121Jekyll.logger.warn "[GTN/scholar] Could not render #{@text} from #{source_page} (#{e})"122res = %(<span>https://example.com/ERROR+INVALID+CITATION+#{@text}</span>)123end124res125end126end127128# Generate Bibliography for all citations used on that page.129#130# Example:131#132# {% bibliography %}133class BibTag < Liquid::Tag134def initialize(tag_name, text, tokens)135super136@text = text.strip137end138139def render(context)140site = context.registers[:site]141Gtn::Scholar.load_bib(site)142# Which page is rendering this tag?143source_page = context.registers[:page]['path']144global_bib = site.config['cached_global_bib']145# citeproc = site.config['cached_citeproc']146# We have our page's citations147citations = site.config['citation_cache'][source_page] || []148# For each of these citation IDs, we need to get the formatted version + pull out149# year, month for sorting.150unique_citations = citations.each_with_object(Hash.new(0)) do |b, a|151a[b] += 1152end.keys153# Remove nil citations154unique_citations = unique_citations.reject { |c| global_bib[c].nil? }155# And now sort them by date + names156sorted_citations = unique_citations.sort do |a, b|157global_bib[a].date.to_s + global_bib[a].names.join(' ') <=>158global_bib[b].date.to_s + global_bib[b].names.join(' ')159end160161out = '<ol class="bibliography">'162out += sorted_citations.map do |c|163r = Gtn::Scholar.render_citation(c)164%(<li id="#{c}">#{r}</li>)165end.join("\n")166out += '</ol>'167out168end169end170end171end172173Liquid::Template.register_tag('cite', Jekyll::Tags::CiteTag)174Liquid::Template.register_tag('cite_url', Jekyll::Tags::CiteUrlTag)175Liquid::Template.register_tag('bibliography', Jekyll::Tags::BibTag)176177178