[INSPIRE](http://inspirehep.net/) is a trusted community hub that facilitates the sharing and discovery of accurate scholarly information in high energy physics. By integrating the social and citation count badge, al-folio users within this community will gain significant benefits. In continuation of #2634, I am creating this pull request. ## Details ### Social Icon - Add your INSPIRE author ID in the `config.yml` under `inspirehep_id`. ### Citation Count - Enable this feature by setting `inspirehep` to `true` under `enable_publication_badges` in your `config.yml` file. - In your bibliography file (e.g., `papers.bib`), add `inspirehep_id = {the literature's recid}` under the citation of a literature source.
58 lines
1.7 KiB
Ruby
58 lines
1.7 KiB
Ruby
require "active_support/all"
|
|
require 'net/http'
|
|
require 'json'
|
|
require 'uri'
|
|
|
|
module Helpers
|
|
extend ActiveSupport::NumberHelper
|
|
end
|
|
|
|
module Jekyll
|
|
class InspireHEPCitationsTag < Liquid::Tag
|
|
Citations = { }
|
|
|
|
def initialize(tag_name, params, tokens)
|
|
super
|
|
@recid = params.strip
|
|
end
|
|
|
|
def render(context)
|
|
recid = context[@recid.strip]
|
|
api_url = "https://inspirehep.net/api/literature/?fields=citation_count&q=recid:#{recid}"
|
|
|
|
begin
|
|
# If the citation count has already been fetched, return it
|
|
if InspireHEPCitationsTag::Citations[recid]
|
|
return InspireHEPCitationsTag::Citations[recid]
|
|
end
|
|
|
|
# Fetch the citation count from the API
|
|
uri = URI(api_url)
|
|
response = Net::HTTP.get(uri)
|
|
data = JSON.parse(response)
|
|
|
|
# # Log the response for debugging
|
|
# puts "API Response: #{data.inspect}"
|
|
|
|
# Extract citation count from the JSON data
|
|
citation_count = data["hits"]["hits"][0]["metadata"]["citation_count"].to_i
|
|
|
|
# Format the citation count for readability
|
|
citation_count = Helpers.number_to_human(citation_count, format: '%n%u', precision: 2, units: { thousand: 'K', million: 'M', billion: 'B' })
|
|
|
|
rescue Exception => e
|
|
# Handle any errors that may occur during fetching
|
|
citation_count = "N/A"
|
|
|
|
# Print the error message including the exception class and message
|
|
puts "Error fetching citation count for #{recid}: #{e.class} - #{e.message}"
|
|
end
|
|
|
|
InspireHEPCitationsTag::Citations[recid] = citation_count
|
|
return "#{citation_count}"
|
|
end
|
|
end
|
|
end
|
|
|
|
Liquid::Template.register_tag('inspirehep_citations', Jekyll::InspireHEPCitationsTag)
|