Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Citations #113

Merged
merged 4 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,14 @@ Rails/HasManyOrHasOneDependent:
Exclude:
- 'app/models/endpoint.rb'

# Offense count: 5
# Offense count: 7
Rails/OutputSafety:
Exclude:
- 'app/forms/hyrax/forms/admin/appearance.rb'
- 'app/helpers/blacklight/catalog_helper_behavior.rb'
- 'app/helpers/hyrax/citations_behaviors/formatters/apa_formatter.rb'
- 'app/helpers/hyrax/citations_behaviors/formatters/chicago_formatter.rb'
- 'app/helpers/hyrax/citations_behaviors/formatters/mla_formatter.rb'

# Offense count: 1
# Cop supports --auto-correct.
Expand Down
18 changes: 18 additions & 0 deletions app/assets/stylesheets/atla-overrides.scss
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ div.facets h3 {font-size: 1em;}
/* admin dashboard available works ETD all caps*/
.dashboard label[for="input-Etd"] {text-transform: uppercase;}

/* citations style */
button.btn.btn-default.btn-block.citations-button.center-block {margin-top: 1em;}
.mla-citation, .apa-citation, .chicago-citation {
line-height: 1.25em;
display: inline-block;
width: 100%;
font-size:0.875em;
}
div#collapse-citations{text-align:left;overflow-wrap: break-word;}
div#collapse-citations h4 {font-size:1em;}
div#collapse-citations p {font-size:0.85em;}
button.btn.btn-default.btn-block.citations-button.center-block:hover, button.btn.btn-default.btn-block.citations-button.center-block:focus{
background-color:#337ab7;
border-color: #204d75;
color:white;
}



@media only screen and (min-width: 768px) {
.hyc-banner .hyc-title h1 {margin-bottom: 0em;}
Expand Down
14 changes: 14 additions & 0 deletions app/helpers/hyrax/citation_behaviors/common_behavior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Hyrax
module CitationsBehaviors
module CommonBehavior
def persistent_url(work); end

def clean_end_punctuation(text)
return text[0, text.length - 1] if text && ([".", ",", ":", ";", "/"].include? text[-1, 1])
text
end
end
end
end
30 changes: 30 additions & 0 deletions app/helpers/hyrax/citation_behaviors/formatters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Hyrax Override: Improve Citations Format
# frozen_string_literal: true

module Hyrax
module CitationsBehaviors
module Formatters
class BaseFormatter
include Hyrax::CitationsBehaviors::CommonBehavior
include Hyrax::CitationsBehaviors::NameBehavior

attr_reader :view_context

def initialize(view_context)
@view_context = view_context
end

# Hyrax Override: Adds new functionality for citations
def add_link_to_original(work)
persistent_url(work).to_s
end
# end
end

autoload :ApaFormatter, 'hyrax/citations_behaviors/formatters/apa_formatter'
autoload :ChicagoFormatter, 'hyrax/citations_behaviors/formatters/chicago_formatter'
autoload :MlaFormatter, 'hyrax/citations_behaviors/formatters/mla_formatter'
autoload :OpenUrlFormatter, 'hyrax/citations_behaviors/formatters/open_url_formatter'
end
end
end
94 changes: 94 additions & 0 deletions app/helpers/hyrax/citation_behaviors/formatters/apa_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Hyrax Override: Improve Format of Citations
# frozen_string_literal: true

module Hyrax
module CitationsBehaviors
module Formatters
class ApaFormatter < BaseFormatter
include Hyrax::CitationsBehaviors::PublicationBehavior
include Hyrax::CitationsBehaviors::TitleBehavior

def format(work)
text = ''
text += authors_text_for(work)
text += pub_date_text_for(work)
text += add_title_text_for(work)
# Hyrax Override: adds addtl content for citation
text += " <span class='citation-link'>#{add_link_to_original(work)}</span>"
# end
text.html_safe # rubocop:disable Rails/OutputSafety
end

private

def authors_text_for(work)
# setup formatted author list
authors_list = author_list(work).reject(&:blank?)
author_text = format_authors(authors_list)
if author_text.blank?
author_text
else
"<span class=\"citation-author\">#{author_text}</span> "
end
end

public

def format_authors(authors_list = [])
return '' if authors_list.blank?
authors_list = Array.wrap(authors_list).collect(&:strip)
text = ''
text += convert_to_initials(authors_list.first) if authors_list.first
authors_list[1..-1].each do |author|
text += if author == authors_list.last
", &amp; #{convert_to_initials(author)}"
else
", #{convert_to_initials(author)}"
end
end
text += "." unless text.end_with?(".")
text
end

private

def pub_date_text_for(work)
# Get Pub Date
pub_date = setup_pub_date(work)
format_date(pub_date)
end

def add_title_text_for(work)
# setup title info
title_info = setup_title_info(work)
format_title(title_info)
end

def add_publisher_text_for(work)
# Publisher info
pub_info = clean_end_punctuation(setup_pub_info(work))
if pub_info.blank?
''
else
pub_info + "."
end
end

def convert_to_initials(name)
name = name.split(" ")
name.map { |n| n.equal?(name.last) ? n.capitalize : n[0].capitalize }.join(". ")
end

public

def format_date(pub_date)
pub_date.blank? ? "" : "(" + pub_date + "). "
end

def format_title(title_info)
title_info.nil? ? "" : "<i class=\"citation-title\">#{title_info}</i> "
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Hyrax Override: Improve Citations Format
# frozen_string_literal: true

module Hyrax
module CitationsBehaviors
module Formatters
class ChicagoFormatter < BaseFormatter
include Hyrax::CitationsBehaviors::PublicationBehavior
include Hyrax::CitationsBehaviors::TitleBehavior
def format(work)
text = ""

# setup formatted author list
authors_list = all_authors(work)
text += format_authors(authors_list)
text = "<span class=\"citation-author\">#{text}</span>" if text.present?
text += format_title(work.to_s)
pub_info = setup_pub_info(work, false)
text += " #{whitewash(pub_info)}." if pub_info.present?
pub_date = setup_pub_date(work)
text += " #{whitewash(pub_date)}." unless pub_date.nil?
text += " <span class='citation-link'>#{add_link_to_original(work)}</span>"
# end

text.html_safe # rubocop:disable Rails/OutputSafety
end

def format_authors(authors_list = [])
return '' if authors_list.blank?
text = ''
text += authors_list.first if authors_list.first
authors_list[1..6].each_with_index do |author, index|
text += if index + 2 == authors_list.length # we've skipped the first author
", and #{author}."
else
", #{author}"
end
end
text += " et al." if authors_list.length > 7
# if for some reason the first author ended with a comma
text = text.gsub(',,', ',')
text += "." unless text.end_with?(".")
whitewash(text)
end

def format_date(pub_date); end

def format_title(title_info)
return "" if title_info.blank?
title_text = chicago_citation_title(title_info)
title_text += '.' unless title_text.end_with?(".")
title_text = whitewash(title_text)
" <i class=\"citation-title\">#{title_text}</i>"
end

private

def whitewash(text)
Loofah.fragment(text.to_s).scrub!(:whitewash).to_s
end
end
end
end
end
75 changes: 75 additions & 0 deletions app/helpers/hyrax/citation_behaviors/formatters/mla_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Hyrax Override: Improve Citations Format
# frozen_string_literal: true

module Hyrax
module CitationsBehaviors
module Formatters
class MlaFormatter < BaseFormatter
include Hyrax::CitationsBehaviors::PublicationBehavior
include Hyrax::CitationsBehaviors::TitleBehavior

def format(work)
text = ''

# setup formatted author list
authors = author_list(work).reject(&:blank?)
text += "<span class=\"citation-author\">#{format_authors(authors)}</span>"
# setup title
title_info = setup_title_info(work)
text += format_title(title_info)

# Hyrax Override: adds contributor
text += " #{work.contributor.join(', ')}." if work.contributor.present?

# Publication
pub_info = clean_end_punctuation(setup_pub_info(work, true))
text += "<span class=\"citation-publication-info\">#{pub_info}. </span>" if pub_info.present?
# text += (pub_info + ".") if pub_info.present?

# Hyrax Override: adds addtl content for citation
# text += add_link_to_original(work)
text += " <span class='citation-link'>#{add_link_to_original(work)}</span>"
# end

text.html_safe # rubocop:disable Rails/OutputSafety
end

def format_authors(authors_list = [])
return "" if authors_list.blank?
authors_list = Array.wrap(authors_list)
text = concatenate_authors_from(authors_list)
if text.present?
text += "." unless text.end_with?(".")
text += " "
end
text
end

def concatenate_authors_from(authors_list)
text = ''
text += authors_list.first
if authors_list.length > 1
if authors_list.length < 4
authors_list[1...-1].each do |author|
text += ", #{author}"
end
text += ", and #{authors_list.last}"
else
text += ", et al"
end
end
text
end
private :concatenate_authors_from

def format_date(pub_date)
" #{pub_date.join(', ')}."
end

def format_title(title_info)
title_info.blank? ? "" : "<i class=\"citation-title\">#{mla_citation_title(title_info)}</i> "
end
end
end
end
end
53 changes: 53 additions & 0 deletions app/helpers/hyrax/citation_behaviors/name_behavior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

module Hyrax
module CitationsBehaviors
module NameBehavior
include Hyrax::CitationsBehaviors::CommonBehavior
# return all unique authors with end punctuation removed
def author_list(work)
all_authors(work) { |author| clean_end_punctuation(CGI.escapeHTML(author)) }
end

# return all unique authors of a work or nil if none
def all_authors(work, &block)
authors = work.creator.uniq.compact
block_given? ? authors.map(&block) : authors
end

def given_name_first(name)
name = clean_end_punctuation(name)
return name unless name.include?(',')
temp_name = name.split(/,\s*/)
temp_name.last + " " + temp_name.first
end

def surname_first(name)
name = name.join('') if name.is_a? Array
# make sure we handle "Cher" correctly
return name if name.include?(',')
name_segments = name.split(' ')
given_name = name_segments.first
surnames = name_segments[1..-1]
if surnames
"#{surnames.join(' ')}, #{given_name}"
else
given_name
end
end

def abbreviate_name(name)
abbreviated_name = ''
name = name.join('') if name.is_a? Array

# make sure we handle "Cher" correctly
return name unless name.include?(' ') || name.include?(',')
name = surname_first(name)
name_segments = name.split(/,\s*/)
abbreviated_name += name_segments.first
abbreviated_name += ", #{name_segments.last.first}" if name_segments[1]
abbreviated_name + "."
end
end
end
end
Loading
Loading