require "date" module Relaton class Bibdata ATTRIBS = %i[ docidentifier doctype title stage relation xml pdf doc html uri rxl revdate abstract technical_committee copyright_from copyright_owner contributor_author_role contributor_author_organization contributor_author_person contributor_publisher_role contributor_publisher_organization language script edition datetype bib_rxl ref ] attr_accessor *ATTRIBS def initialize(options) options.each_pair do |k,v| send("#{k.to_s}=", v) end self end # From http://gavinmiller.io/2016/creating-a-secure-sanitization-function/ FILENAME_BAD_CHARS = [ '/', '\\', '?', '%', '*', ':', '|', '"', '<', '>', '.', ' ' ] def docidentifier_code return "" if docidentifier.nil? a = FILENAME_BAD_CHARS.inject(docidentifier.downcase) do |result, bad_char| result.gsub(bad_char, '-') end end DOC_NUMBER_REGEX = /([\w\/]+)\s+(\d+):?(\d*)/ def doc_number docidentifier&.match(DOC_NUMBER_REGEX) ? $2.to_i : 999999 end def self.from_xml(source) new(Relaton::XmlDocument.parse(source)) end def to_xml #datetype = stage&.casecmp("published") == 0 ? "published" : "circulated" ret = ref ? "\n" : "\n" ret += "#{Date.today.to_s}\n" ret += "#{title}\n" ret += "#{docidentifier}\n" if docidentifier ret += "#{uri}\n" if uri ret += "#{xml}\n" if xml ret += "#{html}\n" if html ret += "#{pdf}\n" if pdf ret += "#{doc}\n" if doc ret += "#{rxl}\n" if rxl ret += "#{language}\n" ret += "\n" if copyright_from ret += "" ret += "#{copyright_from}\n" if copyright_from ret += "#{copyright_owner}\n" if copyright_owner ret += "" end if contributor_author_role ret += "\n" ret += "\n" ret += "#{contributor_author_organization}\n" ret += "\n" end if contributor_author_person Array(contributor_author_person).each do |name| ret += "\n" ret += "\n" ret += "#{name}\n" ret += "\n" end end if contributor_publisher_role ret += "\n" ret += "\n" ret += "#{contributor_publisher_organization}\n" ret += "\n" end ret += "#{revdate}\n" if revdate # ret += "#{agency}" if agency # ret += "#{agency}" if agency ret += "#{edition}\n" if edition ret += "#{language}\n" if language ret += "\n" if script ret += "#{abstract}\n" if abstract ret += "#{stage}\n" if stage ret += "#{technical_committee}\n" if technical_committee ret += ref ? "\n" : "\n" end def to_h ATTRIBS.inject({}) do |acc, k| value = send(k) acc[k.to_s] = value unless value.nil? acc end end def to_yaml to_h.to_yaml end end end