Skip to content

Commit

Permalink
Add chicago citation option (#1293)
Browse files Browse the repository at this point in the history
* Add chicago citation option

* Refine chicago citation implementation

* Update citation_string_processor.rb
  • Loading branch information
abelemlih authored Sep 19, 2022
1 parent 79b97da commit 9d2e399
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Metrics/AbcSize:
- 'app/models/concerns/blacklight/solr/document/marc_export.rb'
- 'app/services/oai_processing_service.rb'
- 'app/services/oai_processing_single_service.rb'
- 'lib/citation_formatter.rb'
- 'app/helpers/citation_modal_helper.rb'

Metrics/BlockLength:
Exclude:
Expand Down Expand Up @@ -48,8 +50,10 @@ Metrics/MethodLength:
- 'app/models/concerns/blacklight/solr/document/marc_export.rb'
- 'app/services/oai_processing_service.rb'
- 'app/services/oai_processing_single_service.rb'
- 'app/helpers/citation_modal_helper.rb'
- 'lib/traject/extract_publication_date.rb'
- 'lib/traject/extract_url_fulltext.rb'
- 'lib/citation_formatter.rb'
- 'app/controllers/sessions/social_login.rb'
- 'config/initializers/bookmarks_index_override.rb'
- 'config/initializers/catalog_index_override.rb'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ gem 'blacklight_range_limit'
gem 'bootstrap', '~> 4.0'
gem 'bootstrap-select-rails', '>= 1.13'
gem 'cancancan'
gem 'citeproc-ruby'
gem 'coffee-rails', '~> 4.2'
gem 'csl-styles'
gem 'devise'
gem 'devise-guests', '~> 0.6'
gem 'dotenv-rails'
Expand Down
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ GEM
xpath (~> 3.2)
childprocess (3.0.0)
chronic (0.10.2)
citeproc (1.0.10)
namae (~> 1.0)
citeproc-ruby (2.0.0)
citeproc (~> 1.0, >= 1.0.9)
csl (~> 2.0)
coderay (1.1.3)
coffee-rails (4.2.2)
coffee-script (>= 2.2.0)
Expand All @@ -156,6 +161,11 @@ GEM
tins (~> 1.6)
crack (0.4.4)
crass (1.0.6)
csl (2.0.0)
namae (~> 1.0)
rexml
csl-styles (2.0.1)
csl (~> 2.0)
datetime_picker_rails (0.0.7)
momentjs-rails (>= 2.8.1)
deprecation (1.0.0)
Expand Down Expand Up @@ -271,6 +281,7 @@ GEM
railties (>= 3.1)
multipart-post (2.1.1)
mysql2 (0.5.3)
namae (1.1.1)
net-scp (3.0.0)
net-ssh (>= 2.6.5, < 7.0.0)
net-ssh (6.1.0)
Expand Down Expand Up @@ -550,8 +561,10 @@ DEPENDENCIES
capistrano-rails (~> 1.6)
capistrano-yarn
capybara (>= 2.15)
citeproc-ruby
coffee-rails (~> 4.2)
coveralls
csl-styles
devise
devise-guests (~> 0.6)
dotenv-rails
Expand Down
13 changes: 10 additions & 3 deletions app/helpers/citation_modal_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ def process_mult_documents_citations(documents)
apa_cit_arr = documents.inject([]) do |arr, doc|
arr << tag.p(doc.send(:export_as_apa_citation_txt).html_safe, class: 'citation-text')
end
chicago_cit_arr = documents.inject([]) do |arr, doc|
arr << tag.p(doc.send(:export_as_chicago_citation_txt).html_safe, class: 'citation-text')
end

safe_join(
[
tag.h2(t('blacklight.citation.mla'), class: 'citation-header'),
mla_cit_arr,
tag.h2(t('blacklight.citation.apa'), class: 'citation-header'),
apa_cit_arr
apa_cit_arr,
tag.h2(t('blacklight.citation.chicago'), class: 'citation-header'),
chicago_cit_arr
].flatten, ''
)
end
Expand All @@ -30,12 +35,14 @@ def process_single_document_citations(documents)
tag.h2(t('blacklight.citation.mla'), class: 'citation-header'),
tag.p(documents.first.send(:export_as_mla_citation_txt).html_safe, class: 'citation-text'),
tag.h2(t('blacklight.citation.apa'), class: 'citation-header'),
tag.p(documents.first.send(:export_as_apa_citation_txt).html_safe, class: 'citation-text')
tag.p(documents.first.send(:export_as_apa_citation_txt).html_safe, class: 'citation-text'),
tag.h2(t('blacklight.citation.chicago'), class: 'citation-header'),
tag.p(documents.first.send(:export_as_chicago_citation_txt).html_safe, class: 'citation-text')
].flatten, ''
)
end

def cit_method_respond_test(documents)
documents.all? { |d| d.respond_to?(:export_as_mla_citation_txt) && d.respond_to?(:export_as_apa_citation_txt) }
documents.all? { |d| d.respond_to?(:export_as_mla_citation_txt) && d.respond_to?(:export_as_apa_citation_txt) && d.respond_to?(:export_as_chicago_citation_txt) }
end
end
8 changes: 7 additions & 1 deletion app/models/concerns/blacklight/solr/document/marc_export.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'openurl'
require './lib/citation_formatter'

# Overwrites the apa_citation and mla_citation methods from module of same name
# v7.0.0, as well as adds new methods to assist the new logic.
Expand Down Expand Up @@ -40,7 +41,12 @@ def export_as_mla_citation_txt
end

def export_as_chicago_citation_txt
chicago_citation(to_marc)
generator = CitationFormatter.new(self)
begin
generator.citation_for('chicago-fullnote-bibliography')
rescue
chicago_citation(to_marc)
end
end

# Exports as an OpenURL KEV (key-encoded value) query string.
Expand Down
1 change: 1 addition & 0 deletions config/locales/blacklight.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ en:
citation:
mla: 'MLA (8th Edition)'
apa: 'APA (7th Edition)'
chicago: 'Chicago (17th Edition)'
footer:
links:
about_library_search: 'About Library Search'
Expand Down
32 changes: 32 additions & 0 deletions lib/citation_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true
require 'citeproc'
require 'csl/styles'
require './lib/citation_string_processor'

class CitationFormatter
include CitationStringProcessor
attr_accessor :obj, :default_citations

def initialize(obj)
@obj = obj
end

def citation_for(style)
CiteProc::Processor.new(style: style, format: 'html').import(item).render(:bibliography, id: :item).first
end

private

def item
CiteProc::Item.new({
"id": :item,
"author": chicago_author(obj),
"issued": obj[:pub_date_isim].first,
"publisher": chicago_publisher(obj),
"publisher-place": obj[:publisher_location_ssim]&.join(', '),
"title": obj[:title_citation_ssi],
"type": obj[:format_ssim]&.first&.downcase,
"DOI": chicago_doi(obj)
})
end
end
26 changes: 26 additions & 0 deletions lib/citation_string_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module CitationStringProcessor
# Chicago Citation
def chicago_author(obj)
clean_end_punctuation(obj[:author_tesim]&.join(', '))
end

def chicago_publisher(obj)
publisher = obj[:published_tesim]&.first&.strip
return nil if publisher.blank?

publisher = publisher.gsub(/\[|\]/, '')
clean_end_punctuation(publisher) if publisher.present?
end

def chicago_doi(obj)
doi = obj['other_standard_ids_tesim']&.first&.strip
clean_end_punctuation(doi) if doi.present?
end

# Helper Methods
def clean_end_punctuation(text)
[".", ",", ":", ";", "/"].include?(text[-1]) ? text[0...-1] : text
end
end

0 comments on commit 9d2e399

Please sign in to comment.