Skip to content

Commit

Permalink
ipmplement to_asciibib #28
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew2net committed Aug 13, 2020
1 parent 6383a6d commit cbf0187
Show file tree
Hide file tree
Showing 36 changed files with 932 additions and 131 deletions.
4 changes: 2 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
inherit_from:
- https://raw.githubusercontent.com/riboseinc/oss-guides/master/ci/rubocop.yml
AllCops:
TargetRubyVersion: 2.3
TargetRubyVersion: 2.4
Rails:
Enabled: true
Enabled: false
7 changes: 2 additions & 5 deletions lib/relaton_bib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ class Error < StandardError; end
class RequestError < StandardError; end

class << self
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength

# @param date [String, Integer, Date]
# @return [Date, NilClass]
def parse_date(date)
def parse_date(date) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
return date if date.is_a?(Date)

sdate = date.to_s
Expand All @@ -30,14 +28,13 @@ def parse_date(date)
when /(?<date>\d{4})/ then Date.strptime $~[:date], "%Y" # 2012
end
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength
end

private

# @param array [Array]
# @return [Array<String>, String]
def single_element_array(array)
def single_element_array(array) # rubocop:disable Metrics/CyclomaticComplexity
if array.size > 1
array.map { |e| e.is_a?(String) ? e : e.to_hash }
else
Expand Down
14 changes: 13 additions & 1 deletion lib/relaton_bib/bib_item_locality.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def initialize(type, reference_from, reference_to = nil)
type_ptrn = %r{section|clause|part|paragraph|chapter|page|whole|table|
annex|figure|note|list|example|volume|issue|time|
locality:[a-zA-Z0-9_]+}x
unless type =~ type_ptrn
unless type.match? type_ptrn
warn "[relaton-bib] WARNING: invalid locality type: #{type}"
end

Expand All @@ -39,6 +39,18 @@ def to_hash
hash["reference_to"] = reference_to if reference_to
hash
end

# @param prefix [String]
# @param count [Integeg] number of localities
# @return [String]
def to_asciibib(prefix = "", count = 1)
pref = prefix.empty? ? prefix : prefix + "."
out = count > 1 ? "#{prefix}::\n" : ""
out += "#{pref}type:: #{type}\n"
out += "#{pref}reference_from:: #{reference_from}\n"
out += "#{pref}reference_to:: #{reference_to}\n" if reference_to
out
end
end

class Locality < BibItemLocality
Expand Down
11 changes: 11 additions & 0 deletions lib/relaton_bib/biblio_note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,16 @@ def to_hash
hash["type"] = type
hash
end

# @param prefix [String]
# @param count [Integer] number of notes
# @return [String]
def to_asciibib(prefix = "", count = 1)
pref = prefix.empty? ? prefix : prefix + "."
out = count > 1 ? "#{pref}biblionote::\n" : ""
out + "#{pref}biblionote.type:: #{type}\n" if type
out += super "#{pref}biblionote"
out
end
end
end
12 changes: 12 additions & 0 deletions lib/relaton_bib/biblio_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ def to_hash
hash["draft"] = single_element_array(draft) if draft&.any?
hash
end

# @param prefix [String]
# @return [String]
def to_asciibib(prefix = "")
pref = prefix.empty? ? prefix : prefix + "."
out = ""
if revision_date
out += "#{pref}version.revision_date:: #{revision_date}\n"
end
draft&.each { |d| out += "#{pref}version.draft:: #{d}\n" }
out
end
end
end
end
13 changes: 13 additions & 0 deletions lib/relaton_bib/bibliographic_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ def to_hash
hash
end

# @param prefix [String]
# @param count [Integer] number of dates
# @return [String]
def to_asciibib(prefix = "", count = 1)
pref = prefix.empty? ? prefix : prefix + "."
out = count > 1 ? "#{pref}date::\n" : ""
out += "#{pref}date.type:: #{type}\n"
out += "#{pref}date.on:: #{on}\n" if on
out += "#{pref}date.from:: #{from}\n" if from
out += "#{pref}date.to:: #{to}\n" if to
out
end

private

# Formats date
Expand Down
104 changes: 80 additions & 24 deletions lib/relaton_bib/bibliographic_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,18 @@ def initialize(**args)
end
@series = args.fetch :series, []
@medium = args[:medium]
@place = args.fetch(:place, []).map { |pl| pl.is_a?(String) ? Place.new(name: pl) : pl }
@place = args.fetch(:place, []).map do |pl|
pl.is_a?(String) ? Place.new(name: pl) : pl
end
@extent = args[:extent] || []
@accesslocation = args.fetch :accesslocation, []
@classification = args.fetch :classification, []
@validity = args[:validity]
@fetched = args.fetch :fetched, nil # , Date.today # we should pass the fetched arg from scrappers
@keyword = (args[:keyword] || []).map { |kw| LocalizedString.new(kw) }
# we should pass the fetched arg from scrappers
@fetched = args.fetch :fetched, nil
@keyword = (args[:keyword] || []).map do |kw|
LocalizedString.new(kw)
end
@license = args.fetch :license, []
@doctype = args[:doctype]
@editorialgroup = args[:editorialgroup]
Expand Down Expand Up @@ -278,7 +283,7 @@ def makeid(id, attribute)
end

# @return [String]
def shortref(identifier, **opts)
def shortref(identifier, **opts) # rubocop:disable Metrics/CyclomaticComplexity
pubdate = date.select { |d| d.type == "published" }
year = if opts[:no_year] || pubdate.empty? then ""
else ":" + pubdate&.first&.on&.year.to_s
Expand All @@ -300,10 +305,8 @@ def to_xml(builder = nil, **opts, &block)
end
end

# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity

# @return [Hash]
def to_hash
def to_hash # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
hash = {}
hash["id"] = id if id
hash["title"] = single_element_array(title) if title&.any?
Expand All @@ -312,7 +315,9 @@ def to_hash
hash["docid"] = single_element_array(docidentifier) if docidentifier&.any?
hash["docnumber"] = docnumber if docnumber
hash["date"] = single_element_array(date) if date&.any?
hash["contributor"] = single_element_array(contributor) if contributor&.any?
if contributor&.any?
hash["contributor"] = single_element_array(contributor)
end
hash["edition"] = edition if edition
hash["version"] = version.to_hash if version
hash["revdate"] = revdate if revdate
Expand All @@ -328,8 +333,12 @@ def to_hash
hash["medium"] = medium.to_hash if medium
hash["place"] = single_element_array(place) if place&.any?
hash["extent"] = single_element_array(extent) if extent&.any?
hash["accesslocation"] = single_element_array(accesslocation) if accesslocation&.any?
hash["classification"] = single_element_array(classification) if classification&.any?
if accesslocation&.any?
hash["accesslocation"] = single_element_array(accesslocation)
end
if classification&.any?
hash["classification"] = single_element_array(classification)
end
hash["validity"] = validity.to_hash if validity
hash["fetched"] = fetched.to_s if fetched
hash["keyword"] = single_element_array(keyword) if keyword&.any?
Expand All @@ -342,11 +351,10 @@ def to_hash
end
hash
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

# @param bibtex [BibTeX::Bibliography, NilClass]
# @return [String]
def to_bibtex(bibtex = nil)
def to_bibtex(bibtex = nil) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
item = BibTeX::Entry.new
item.type = bibtex_type
item.key = id
Expand All @@ -369,7 +377,6 @@ def to_bibtex(bibtex = nil)
bibtex << item
bibtex.to_s
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength

# @param lang [String] language code Iso639
# @return [Array<RelatonIsoBib::TypedTitleString>]
Expand All @@ -391,25 +398,27 @@ def abstract=(value)

def deep_clone
dump = Marshal.dump self
Marshal.load dump
Marshal.load dump # rubocop:disable Security/MarshalLoad
end

def disable_id_attribute
@id_attribute = false
end

# remove title part components and abstract
def to_all_parts
def to_all_parts # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
me = deep_clone
me.disable_id_attribute
me.relation << RelatonBib::DocumentRelation.new(
type: "instance", bibitem: self,
)
me.relation << DocumentRelation.new(type: "instance", bibitem: self)
me.language.each do |l|
me.title.delete_if { |t| t.type == "title-part" }
ttl = me.title.select { |t| t.type != "main" && t.title.language&.include?(l) }
ttl = me.title.select do |t|
t.type != "main" && t.title.language&.include?(l)
end
tm_en = ttl.map { |t| t.title.content }.join " – "
me.title.detect { |t| t.type == "main" && t.title.language&.include?(l) }&.title&.content = tm_en
me.title.detect do |t|
t.type == "main" && t.title.language&.include?(l)
end&.title&.content = tm_en
end
me.abstract = []
me.docidentifier.each(&:remove_part)
Expand Down Expand Up @@ -440,14 +449,61 @@ def to_most_recent_reference

# If revision_date exists then returns it else returns published date or nil
# @return [String, NilClass]
def revdate
def revdate # rubocop:disable Metrics/CyclomaticComplexity
@revdate ||= if version&.revision_date
version.revision_date
else
date.detect { |d| d.type == "published" }&.on&.to_s
end
end

# @param prefix [String]
# @return [String]
def to_asciibib(prefix = "") # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
pref = prefix.empty? ? prefix : prefix + "."
out = prefix.empty? ? "[%bibitem]\n== {blank}\n" : ""
out += "#{pref}id:: #{id}\n" if id
out += "#{pref}fetched:: #{fetched}\n" if fetched
title.each { |t| out += t.to_asciibib(prefix, title.size) }
out += "#{pref}type:: #{type}\n" if type
docidentifier.each do |di|
out += di.to_asciibib prefix, docidentifier.size
end
out += "#{pref}docnumber:: #{docnumber}\n" if docnumber
out += "#{pref}edition:: #{edition}\n" if edition
language.each { |l| out += "#{pref}language:: #{l}\n" }
script.each { |s| out += "#{pref}script:: #{s}\n" }
out += version.to_asciibib prefix if version
biblionote&.each { |b| out += b.to_asciibib prefix, biblionote.size }
out += status.to_asciibib prefix if status
date.each { |d| out += d.to_asciibib prefix, date.size }
abstract.each do |a|
out += a.to_asciibib "#{pref}abstract", abstract.size
end
copyright.each { |c| out += c.to_asciibib prefix, copyright.size }
link.each { |l| out += l.to_asciibib prefix, link.size }
out += medium.to_asciibib prefix if medium
place.each { |pl| out += pl.to_asciibib prefix, place.size }
extent.each { |ex| out += ex.to_asciibib "#{pref}extent", extent.size }
accesslocation.each { |al| out += "#{pref}accesslocation:: #{al}\n" }
classification.each do |cl|
out += cl.to_asciibib prefix, classification.size
end
out += validity.to_asciibib prefix if validity
contributor.each do |c|
out += c.to_asciibib "contributor.*", contributor.size
end
out += relation.to_asciibib prefix if relation
series.each { |s| out += s.to_asciibib prefix, series.size }
out += "#{pref}doctype:: #{doctype}\n" if doctype
out += "#{pref}formattedref:: #{formattedref}\n" if formattedref
keyword.each { |kw| out += kw.to_asciibib "#{pref}keyword", keyword.size }
out += editorialgroup.to_asciibib prefix if editorialgroup
ics.each { |i| out += i.to_asciibib prefix, ics.size }
out += structuredidentifier.to_asciibib prefix if structuredidentifier
out
end

private

# @return [String]
Expand All @@ -470,7 +526,7 @@ def bibtex_type
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength

# @param [BibTeX::Entry]
def bibtex_author(item)
def bibtex_author(item) # rubocop:disable Metrics/CyclomaticComplexity
authors = contributor.select do |c|
c.entity.is_a?(Person) && c.role.map(&:type).include?("author")
end.map &:entity
Expand All @@ -487,7 +543,7 @@ def bibtex_author(item)
end

# @param [BibTeX::Entry]
def bibtex_contributor(item)
def bibtex_contributor(item) # rubocop:disable Metrics/CyclomaticComplexity
contributor.each do |c|
rls = c.role.map(&:type)
if rls.include?("publisher") then item.publisher = c.entity.name
Expand All @@ -504,7 +560,7 @@ def bibtex_contributor(item)
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength

# @param [BibTeX::Entry]
def bibtex_note(item)
def bibtex_note(item) # rubocop:disable Metrics/CyclomaticComplexity
biblionote.each do |n|
case n.type
when "annote" then item.annote = n.content
Expand Down
11 changes: 11 additions & 0 deletions lib/relaton_bib/classification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,16 @@ def to_hash
hash["type"] = type if type
hash
end

# @param prefix [String]
# @param count [Integer] number of classifications
# @return [String]
def to_asciibib(prefix = "", count = 1)
pref = prefix.empty? ? "classification" : prefix + ".classification"
out = count > 1 ? "#{pref}::\n" : ""
out += "#{pref}.type:: #{type}\n" if type
out += "#{pref}.value:: #{value}\n"
out
end
end
end
Loading

0 comments on commit cbf0187

Please sign in to comment.