class Jekyll::CiteTag

{% cite X %} which generates the link to the bib + text

Public Class Methods

new(tag_name, text, tokens) click to toggle source
Calls superclass method
# File _plugins/jekyll-scholar.rb, line 8
def initialize(tag_name, text, tokens)
  super
  @text = text.strip
end

Public Instance Methods

render(context) click to toggle source
# File _plugins/jekyll-scholar.rb, line 13
def render(context)
  page = context.registers[:page]
  site = context.registers[:site]
  Gtn::Scholar.load_bib(site)

  # Mark this page as having citations
  page['cited'] = true

  return "@#{@text}" if page['citation_target'] == 'R'

  # Which page is rendering this tag?
  source_page = page['path']

  # Citation Frequency
  site.config['citation_count'] = Hash.new(0) if !site.config.key?('citation_count')
  site.config['citation_count'][@text] += 1

  # If the overall cache is nil, create it
  site.config['citation_cache'] = {} if site.config['citation_cache'].nil?
  # If the individual page in the chace is nil, create it.
  site.config['citation_cache'][source_page] = [] if site.config['citation_cache'][source_page].nil?

  # Push it to our cache.
  site.config['citation_cache'][source_page].push(@text)

  begin
    citation_text = site.config['cached_citeproc'].render(:citation, id: @text)
    layout = page.fetch('layout', nil)
    if %w[tutorial_slides base_slides introduction_slides].include? layout
      doi = site.config['cached_citeproc'].items[@text].doi
      url = site.config['cached_citeproc'].items[@text].url
      furl = if !doi.nil?
               "https://doi.org/#{doi}"
             elsif !url.nil?
               url
             end

      res = if furl.nil?
              %(<span class="citation">#{citation_text}</span>)
            else
              %(<span class="citation"><a href="#{furl}">#{citation_text}</a></span>)
            end
    else
      res = %(<span class="citation"><a href="##{@text}">#{citation_text}</a></span>)
    end
  rescue StandardError => e
    Jekyll.logger.warn "[GTN/scholar] Could not render #{@text} from #{source_page} (#{e})"
    res = %(<span>ERROR INVALID CITATION #{@text}</span>)
  end

  res.gsub!(/"/, '\"') if page['citation_target'] == 'jupyter'

  res
end