-
Notifications
You must be signed in to change notification settings - Fork 11.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] InspireHEP social and citation count badge (#2638)
[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.
- Loading branch information
1 parent
3ff7579
commit dfc7453
Showing
6 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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) |