relaton-bib 1.20.8 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -1
  3. data/.rubocop.yml +1 -1
  4. data/CLAUDE.md +96 -0
  5. data/Gemfile +1 -4
  6. data/README.adoc +195 -154
  7. data/bin/console +1 -1
  8. data/grammars/basicdoc.rng +1572 -671
  9. data/grammars/biblio-standoc.rng +107 -46
  10. data/grammars/biblio.rng +1012 -377
  11. data/grammars/versions.json +26 -26
  12. data/lib/relaton/bib/converter/asciibib/to_asciibib.rb +663 -0
  13. data/lib/relaton/bib/converter/asciibib.rb +13 -0
  14. data/lib/relaton/bib/converter/bibtex/from_bibtex.rb +245 -0
  15. data/lib/relaton/bib/converter/bibtex/to_bibtex.rb +341 -0
  16. data/lib/relaton/bib/converter/bibtex.rb +23 -0
  17. data/lib/relaton/bib/converter/bibxml/from_rfcxml.rb +383 -0
  18. data/lib/relaton/bib/converter/bibxml/from_rfcxml_referencegroup.rb +71 -0
  19. data/lib/relaton/bib/converter/bibxml/to_rfcxml.rb +305 -0
  20. data/lib/relaton/bib/converter/bibxml/to_rfcxml_referencegroup.rb +52 -0
  21. data/lib/relaton/bib/converter/bibxml.rb +51 -0
  22. data/lib/relaton/bib/hash_parser_v1.rb +754 -0
  23. data/lib/relaton/bib/item_data.rb +229 -0
  24. data/lib/relaton/bib/model/abstract.rb +16 -0
  25. data/lib/relaton/bib/model/address.rb +22 -0
  26. data/lib/relaton/bib/model/affiliation.rb +16 -0
  27. data/lib/relaton/bib/model/bibdata.rb +24 -0
  28. data/lib/relaton/bib/model/bibitem.rb +23 -0
  29. data/lib/relaton/bib/model/contact.rb +18 -0
  30. data/lib/relaton/bib/model/contribution_info.rb +16 -0
  31. data/lib/relaton/bib/model/contributor.rb +71 -0
  32. data/lib/relaton/bib/model/copyright.rb +27 -0
  33. data/lib/relaton/bib/model/date.rb +31 -0
  34. data/lib/relaton/bib/model/depiction.rb +16 -0
  35. data/lib/relaton/bib/model/docidentifier.rb +33 -0
  36. data/lib/relaton/bib/model/doctype.rb +14 -0
  37. data/lib/relaton/bib/model/edition.rb +14 -0
  38. data/lib/relaton/bib/model/ext.rb +26 -0
  39. data/lib/relaton/bib/model/extent.rb +16 -0
  40. data/lib/relaton/bib/model/formattedref.rb +43 -0
  41. data/lib/relaton/bib/model/full_name_type.rb +64 -0
  42. data/lib/relaton/bib/model/fullname.rb +11 -0
  43. data/lib/relaton/bib/model/ics.rb +23 -0
  44. data/lib/relaton/bib/model/image.rb +28 -0
  45. data/lib/relaton/bib/model/item.rb +137 -0
  46. data/lib/relaton/bib/model/item_base.rb +27 -0
  47. data/lib/relaton/bib/model/keyword.rb +30 -0
  48. data/lib/relaton/bib/model/locality.rb +18 -0
  49. data/lib/relaton/bib/model/locality_stack.rb +14 -0
  50. data/lib/relaton/bib/model/localized_string.rb +57 -0
  51. data/lib/relaton/bib/model/localized_string_attrs.rb +24 -0
  52. data/lib/relaton/bib/model/logo.rb +14 -0
  53. data/lib/relaton/bib/model/medium.rb +22 -0
  54. data/lib/relaton/bib/model/note.rb +16 -0
  55. data/lib/relaton/bib/model/organization.rb +13 -0
  56. data/lib/relaton/bib/model/organization_type.rb +42 -0
  57. data/lib/relaton/bib/model/person.rb +36 -0
  58. data/lib/relaton/bib/model/phone.rb +14 -0
  59. data/lib/relaton/bib/model/place.rb +33 -0
  60. data/lib/relaton/bib/model/price.rb +14 -0
  61. data/lib/relaton/bib/model/relation.rb +43 -0
  62. data/lib/relaton/bib/model/series.rb +34 -0
  63. data/lib/relaton/bib/model/size.rb +23 -0
  64. data/lib/relaton/bib/model/source_locality_stack.rb +14 -0
  65. data/lib/relaton/bib/model/status.rb +27 -0
  66. data/lib/relaton/bib/model/structured_identifier.rb +48 -0
  67. data/lib/relaton/bib/model/subdivision.rb +16 -0
  68. data/lib/relaton/bib/model/title.rb +56 -0
  69. data/lib/relaton/bib/model/type/string_date.rb +48 -0
  70. data/lib/relaton/bib/model/uri.rb +18 -0
  71. data/lib/relaton/bib/model/validity.rb +16 -0
  72. data/lib/relaton/bib/model/version.rb +14 -0
  73. data/lib/relaton/bib/namespace_helper.rb +21 -0
  74. data/lib/relaton/bib/util.rb +18 -0
  75. data/lib/relaton/bib/version.rb +5 -0
  76. data/lib/relaton/bib.rb +45 -0
  77. data/relaton-bib.gemspec +11 -8
  78. metadata +125 -67
  79. data/lib/relaton_bib/bib_item_locality.rb +0 -175
  80. data/lib/relaton_bib/biblio_note.rb +0 -72
  81. data/lib/relaton_bib/biblio_version.rb +0 -46
  82. data/lib/relaton_bib/bibliographic_date.rb +0 -119
  83. data/lib/relaton_bib/bibliographic_item.rb +0 -668
  84. data/lib/relaton_bib/bibliographic_size.rb +0 -103
  85. data/lib/relaton_bib/bibtex_parser.rb +0 -246
  86. data/lib/relaton_bib/bibxml_parser.rb +0 -422
  87. data/lib/relaton_bib/classification.rb +0 -40
  88. data/lib/relaton_bib/config.rb +0 -16
  89. data/lib/relaton_bib/contribution_info.rb +0 -117
  90. data/lib/relaton_bib/contributor.rb +0 -277
  91. data/lib/relaton_bib/copyright_association.rb +0 -79
  92. data/lib/relaton_bib/deep_dup.rb +0 -30
  93. data/lib/relaton_bib/document_identifier.rb +0 -118
  94. data/lib/relaton_bib/document_relation.rb +0 -95
  95. data/lib/relaton_bib/document_relation_collection.rb +0 -57
  96. data/lib/relaton_bib/document_status.rb +0 -92
  97. data/lib/relaton_bib/document_type.rb +0 -52
  98. data/lib/relaton_bib/edition.rb +0 -55
  99. data/lib/relaton_bib/editorial_group.rb +0 -41
  100. data/lib/relaton_bib/extent.rb +0 -39
  101. data/lib/relaton_bib/forename.rb +0 -65
  102. data/lib/relaton_bib/formatted_ref.rb +0 -17
  103. data/lib/relaton_bib/formatted_string.rb +0 -133
  104. data/lib/relaton_bib/full_name.rb +0 -108
  105. data/lib/relaton_bib/hash_converter.rb +0 -568
  106. data/lib/relaton_bib/hit.rb +0 -52
  107. data/lib/relaton_bib/hit_collection.rb +0 -105
  108. data/lib/relaton_bib/ics.rb +0 -42
  109. data/lib/relaton_bib/image.rb +0 -95
  110. data/lib/relaton_bib/localized_string.rb +0 -149
  111. data/lib/relaton_bib/medium.rb +0 -76
  112. data/lib/relaton_bib/organization.rb +0 -165
  113. data/lib/relaton_bib/person.rb +0 -129
  114. data/lib/relaton_bib/place.rb +0 -203
  115. data/lib/relaton_bib/renderer/bibtex_builder.rb +0 -312
  116. data/lib/relaton_bib/renderer/bibxml.rb +0 -296
  117. data/lib/relaton_bib/series.rb +0 -119
  118. data/lib/relaton_bib/structured_identifier.rb +0 -173
  119. data/lib/relaton_bib/technical_committee.rb +0 -34
  120. data/lib/relaton_bib/typed_title_string.rb +0 -191
  121. data/lib/relaton_bib/typed_uri.rb +0 -57
  122. data/lib/relaton_bib/util.rb +0 -16
  123. data/lib/relaton_bib/validity.rb +0 -52
  124. data/lib/relaton_bib/version.rb +0 -3
  125. data/lib/relaton_bib/workers_pool.rb +0 -43
  126. data/lib/relaton_bib/workgroup.rb +0 -58
  127. data/lib/relaton_bib/xml_parser.rb +0 -718
  128. data/lib/relaton_bib.rb +0 -111
@@ -1,105 +0,0 @@
1
- require "forwardable"
2
-
3
- module RelatonBib
4
- class HitCollection
5
- extend Forwardable
6
-
7
- def_delegators :@array, :<<, :[], :first, :empty?, :any?, :size, :each,
8
- :each_slice, :reduce, :map
9
-
10
- # @return [TrueClass, FalseClass]
11
- attr_reader :fetched
12
-
13
- # @return [String]
14
- attr_reader :text
15
-
16
- # @return [String]
17
- attr_reader :year
18
-
19
- # @param text [String] reference to search
20
- def initialize(text, year = nil)
21
- @array = []
22
- @text = text
23
- @year = year
24
- @fetched = false
25
- end
26
-
27
- #
28
- # Fetches hits from the data source
29
- #
30
- # @return [self] self object
31
- #
32
- def fetch
33
- workers = WorkersPool.new 4
34
- workers.worker(&:fetch)
35
- each do |hit|
36
- workers << hit
37
- end
38
- workers.end
39
- workers.result
40
- @fetched = true
41
- self
42
- end
43
-
44
- #
45
- # Renders the collection as XML
46
- #
47
- # @param opts [Hash] options
48
- # @option opts [Nokogiri::XML::Builder] :builder XML builder
49
- # @option opts [Boolean] :bibdata render bibdata if true
50
- # @option opts [String, Symbol] :lang language
51
- #
52
- # @return [String] XML representation of the collection
53
- #
54
- def to_xml(**opts)
55
- builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
56
- xml.documents do
57
- @array.each do |hit|
58
- hit.fetch
59
- hit.to_xml(**opts.merge(builder: xml))
60
- end
61
- end
62
- end
63
- builder.to_xml
64
- end
65
-
66
- #
67
- # Selects matching hits and returns a new collection
68
- #
69
- # @param [Proc] &block proc to select hits
70
- #
71
- # @return [RelatonBib::HitCollection] new hit collection
72
- #
73
- def select(&block)
74
- me = deep_dup
75
- array_dup = instance_variable_get(:@array).deep_dup
76
- me.instance_variable_set(:@array, array_dup)
77
- array_dup.select!(&block)
78
- array_dup.each { |h| h.hit_collection = WeakRef.new me }
79
- me
80
- end
81
-
82
- def reduce!(sum, &block)
83
- @array = @array.reduce sum, &block
84
- self
85
- end
86
-
87
- #
88
- # Returns String representation of the collection
89
- #
90
- # @return [String] String representation of the collection
91
- #
92
- def to_s
93
- inspect
94
- end
95
-
96
- #
97
- # Returns String representation of the collection
98
- #
99
- # @return [String] String representation of the collection
100
- #
101
- def inspect
102
- "<#{self.class}:#{format('%#.14x', object_id << 1)} @ref=#{@text} @fetched=#{@fetched}>"
103
- end
104
- end
105
- end
@@ -1,42 +0,0 @@
1
- module RelatonBib
2
- class ICS
3
- # @return [String]
4
- attr_reader :code
5
-
6
- # @return [String, nil]
7
- attr_reader :text
8
-
9
- # @param code [String]
10
- # @param text [String, nil]
11
- def initialize(code:, text: nil)
12
- @code = code
13
- @text = text
14
- end
15
-
16
- # @param builder [Nokogiri::XML::Builder]
17
- def to_xml(builder)
18
- builder.ics do |b|
19
- b.code code
20
- b.text_ text if text
21
- end
22
- end
23
-
24
- # @return [Hash]
25
- def to_hash
26
- hash = { "code" => code }
27
- hash["text"] = text if text
28
- hash
29
- end
30
-
31
- # @param prefix [String]
32
- # @param count [Integer] number of ics
33
- # @return [String]
34
- def to_asciibib(prefix = "", count = 1)
35
- pref = prefix.empty? ? "ics" : "#{prefix}.ics"
36
- out = count > 1 ? "#{pref}::\n" : ""
37
- out += "#{pref}.code:: #{code}\n"
38
- out += "#{pref}.text:: #{text}\n" if text
39
- out
40
- end
41
- end
42
- end
@@ -1,95 +0,0 @@
1
- module RelatonBib
2
- class Image
3
- # @return [String]
4
- attr_accessor :src, :mimetype
5
-
6
- # @return [String, nil]
7
- attr_accessor :id, :filename, :width, :height, :alt, :title, :longdesc
8
-
9
- #
10
- # Initializes a new Image object.
11
- #
12
- # @param src [String] the source URL of the image
13
- # @param mimetype [String] the MIME type of the image
14
- # @param args [Hash] additional arguments
15
- # @option id [String, nil] the ID of the image
16
- # @option args [String, nil] :filename the filename of the image
17
- # @option args [String, nil] :width the width of the image
18
- # @option args [String, nil] :height the height of the image
19
- # @option args [String, nil] :alt the alternative text for the image
20
- # @option args [String, nil] :title the title of the image
21
- # @option args [String, nil] :longdesc the long description of the image
22
- #
23
- def initialize(src:, mimetype:, **args)
24
- @src = src
25
- @mimetype = mimetype
26
- args.each { |k, v| send "#{k}=", v }
27
- end
28
-
29
- def ==(other)
30
- other.is_a?(Image) && id == other.id && src == other.src && mimetype == other.mimetype &&
31
- filename == other.filename && width == other.width && height == other.height &&
32
- alt == other.alt && title == other.title && longdesc == other.longdesc
33
- end
34
-
35
- #
36
- # Converts the image object to XML format.
37
- #
38
- # @param [Nokogiri::XML::Builder] builder The XML builder object.
39
- #
40
- # @return [void]
41
- #
42
- def to_xml(builder) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength
43
- builder.image do
44
- builder.parent[:id] = id if id
45
- builder.parent[:src] = src
46
- builder.parent[:mimetype] = mimetype
47
- builder.parent[:filename] = filename if filename
48
- builder.parent[:width] = width if width
49
- builder.parent[:height] = height if height
50
- builder.parent[:alt] = alt if alt
51
- builder.parent[:title] = title if title
52
- builder.parent[:longdesc] = longdesc if longdesc
53
- end
54
- end
55
-
56
- #
57
- # Converts the Image object to a hash representation.
58
- #
59
- # @return [Hash] The hash representation of the Image object.
60
- #
61
- def to_hash # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity
62
- hash = { "image" => { "src" => src, "mimetype" => mimetype } }
63
- hash["image"]["id"] = id if id
64
- hash["image"]["filename"] = filename if filename
65
- hash["image"]["width"] = width if width
66
- hash["image"]["height"] = height if height
67
- hash["image"]["alt"] = alt if alt
68
- hash["image"]["title"] = title if title
69
- hash["image"]["longdesc"] = longdesc if longdesc
70
- hash
71
- end
72
-
73
- #
74
- # Converts the image object to AsciiBib format.
75
- #
76
- # @param prefix [String] The prefix to be added to the AsciiBib output.
77
- #
78
- # @return [String] The image object converted to AsciiBib format.
79
- #
80
- def to_asciibib(prefix = "") # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength
81
- pref = prefix.empty? ? "image." : "#{prefix}.image."
82
- out = ""
83
- out += "#{pref}id:: #{id}\n" if id
84
- out += "#{pref}src:: #{src}\n"
85
- out += "#{pref}mimetype:: #{mimetype}\n"
86
- out += "#{pref}filename:: #{filename}\n" if filename
87
- out += "#{pref}width:: #{width}\n" if width
88
- out += "#{pref}height:: #{height}\n" if height
89
- out += "#{pref}alt:: #{alt}\n" if alt
90
- out += "#{pref}title:: #{title}\n" if title
91
- out += "#{pref}longdesc:: #{longdesc}\n" if longdesc
92
- out
93
- end
94
- end
95
- end
@@ -1,149 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RelatonBib
4
- # Localized string.
5
- class LocalizedString
6
- include RelatonBib
7
-
8
- # @return [Array<String>] language Iso639 code
9
- attr_reader :language
10
-
11
- # @return [Array<String>] script Iso15924 code
12
- attr_reader :script
13
-
14
- # @return [String, Array<RelatonBib::LocalizedString>]
15
- attr_accessor :content
16
-
17
- # @param content [String, Array<RelatonBib::LocalizedString>]
18
- # @param language [String] language code Iso639
19
- # @param script [String] script code Iso15924
20
- def initialize(content, language = nil, script = nil) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
21
- if content.is_a?(Array) && content.none?
22
- raise ArgumentError, "LocalizedString content is empty"
23
- end
24
-
25
- @language = language.is_a?(String) ? [language] : language
26
- @script = script.is_a?(String) ? [script] : script
27
- @content = if content.is_a?(Array)
28
- content.map do |c|
29
- case c
30
- when Hash
31
- LocalizedString.new c[:content], c[:language], c[:script]
32
- when String then LocalizedString.new c
33
- else c
34
- end
35
- end
36
- else cleanup content
37
- end
38
- end
39
-
40
- def ==(other)
41
- return false unless other.is_a? LocalizedString
42
-
43
- content == other.content && language == other.language && script == other.script
44
- end
45
-
46
- #
47
- # String representation.
48
- #
49
- # @return [String]
50
- #
51
- def to_s
52
- content.is_a?(Array) ? content.first.to_s : content.to_s
53
- end
54
-
55
- #
56
- # Returns true if content is empty.
57
- #
58
- # @return [Boolean]
59
- #
60
- def empty?
61
- content.empty?
62
- end
63
-
64
- # @param builder [Nokogiri::XML::Builder]
65
- def to_xml(builder) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
66
- return unless content
67
-
68
- if content.is_a?(Array)
69
- content.each { |c| builder.variant { c.to_xml builder } }
70
- else
71
- builder.parent["language"] = language.join(",") if language&.any?
72
- builder.parent["script"] = script.join(",") if script&.any?
73
- builder.parent.inner_html = encode content
74
- end
75
- end
76
-
77
- #
78
- # Encode content.
79
- #
80
- # @param [String] cnt content
81
- #
82
- # @return [String] encoded content
83
- #
84
- def encode(cnt)
85
- escp cnt
86
- end
87
-
88
- #
89
- # Escope HTML entities.
90
- #
91
- # @param [String] str input string
92
- #
93
- # @return [String] output string
94
- #
95
- def escp(str)
96
- return unless str
97
-
98
- coder = HTMLEntities.new
99
- coder.encode coder.decode(str.dup.force_encoding("UTF-8"))
100
- end
101
-
102
- # @return [Hash, Array<Hash>]
103
- def to_hash # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
104
- if content.nil? || content.is_a?(String)
105
- # return content unless language || script
106
-
107
- hash = {}
108
- hash["content"] = content # unless content.nil? || content.empty?
109
- hash["language"] = single_element_array(language) if language&.any?
110
- hash["script"] = single_element_array(script) if script&.any?
111
- hash
112
- else content&.map &:to_hash
113
- end
114
- end
115
-
116
- # @param prefix [String]
117
- # @param count [Integer] number of elements
118
- # @return [String]
119
- def to_asciibib(prefix = "", count = 1, has_attrs = false) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength
120
- pref = prefix.empty? ? prefix : "#{prefix}."
121
- case content
122
- when String
123
- unless language&.any? || script&.any? || has_attrs
124
- return "#{prefix}:: #{content}\n"
125
- end
126
-
127
- out = count > 1 ? "#{prefix}::\n" : ""
128
- out += "#{pref}content:: #{content}\n"
129
- language&.each { |l| out += "#{pref}language:: #{l}\n" }
130
- script&.each { |s| out += "#{pref}script:: #{s}\n" }
131
- out
132
- when Array
133
- content.map { |c| c.to_asciibib "#{pref}variant", content.size }.join
134
- else count > 1 ? "#{prefix}::\n" : ""
135
- end
136
- end
137
-
138
- #
139
- # Should be implemented in subclass.
140
- #
141
- # @param [String] str content
142
- #
143
- # @return [String] cleaned content
144
- #
145
- def cleanup(str)
146
- str
147
- end
148
- end
149
- end
@@ -1,76 +0,0 @@
1
- module RelatonBib
2
- class Medium
3
- # @return [String, nil]
4
- attr_reader :content, :genre, :form, :carrier, :size, :scale
5
-
6
- #
7
- # Initialize a Medium object.
8
- #
9
- # @param content [String, nil] content of the medium
10
- # @param genre [String, nil] genre of the medium
11
- # @param form [String, nil] form of the medium
12
- # @param carrier [String, nil] carrier of the medium
13
- # @param size [String, nil] size of the medium
14
- # @param scale [String, nil]
15
- #
16
- def initialize(**args)
17
- @content = args[:content]
18
- @genre = args[:genre]
19
- @form = args[:form]
20
- @carrier = args[:carrier]
21
- @size = args[:size]
22
- @scale = args[:scale]
23
- end
24
-
25
- #
26
- # Render Medium object to XML.
27
- #
28
- # @param builder [Nokogiri::XML::Builder]
29
- #
30
- def to_xml(builder) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
31
- builder.medium do
32
- builder.content content if content
33
- builder.genre genre if genre
34
- builder.form form if form
35
- builder.carrier carrier if carrier
36
- builder.size size if size
37
- builder.scale scale if scale
38
- end
39
- end
40
-
41
- #
42
- # Render Medium object to hash.
43
- #
44
- # @return [Hash]
45
- #
46
- def to_hash # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
47
- hash = {}
48
- hash["content"] = content if content
49
- hash["genre"] = genre if genre
50
- hash["form"] = form if form
51
- hash["carrier"] = carrier if carrier
52
- hash["size"] = size if size
53
- hash["scale"] = scale if scale
54
- hash
55
- end
56
-
57
- #
58
- # Render Medium object to AsciiBib.
59
- #
60
- # @param prefix [String]
61
- #
62
- # @return [String]
63
- #
64
- def to_asciibib(prefix = "") # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
65
- pref = prefix.empty? ? "medium." : "#{prefix}.medium."
66
- out = ""
67
- out += "#{pref}content:: #{content}\n" if content
68
- out += "#{pref}genre:: #{genre}\n" if genre
69
- out += "#{pref}form:: #{form}\n" if form
70
- out += "#{pref}carrier:: #{carrier}\n" if carrier
71
- out += "#{pref}size:: #{size}\n" if size
72
- out += "#{pref}scale:: #{scale}\n" if scale
73
- out
74
- end
75
- end
76
- end
@@ -1,165 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "relaton_bib/contributor"
4
-
5
- module RelatonBib
6
- # Organization identifier.
7
- class OrgIdentifier
8
- # ORCID = "orcid"
9
- # URI = "uri"
10
-
11
- # @return [String]
12
- attr_reader :type
13
-
14
- # @return [String]
15
- attr_reader :value
16
-
17
- # @param type [String]
18
- # @param value [String]
19
- def initialize(type, value)
20
- # unless [ORCID, URI].include? type
21
- # raise ArgumentError, 'Invalid type. It should be "orsid" or "uri".'
22
- # end
23
-
24
- @type = type
25
- @value = value
26
- end
27
-
28
- def ==(other)
29
- type == other.type && value == other.value
30
- end
31
-
32
- # @param builder [Nokogiri::XML::Builder]
33
- def to_xml(builder)
34
- builder.identifier(value, type: type)
35
- end
36
-
37
- # @return [Hash]
38
- def to_hash
39
- { "type" => type, "id" => value }
40
- end
41
-
42
- # @param prefix [String]
43
- # @param count [Integer]
44
- # @return [String]
45
- def to_asciibib(prefix = "", count = 1)
46
- pref = prefix.empty? ? prefix : "#{prefix}."
47
- out = count > 1 ? "#{pref}identifier::\n" : ""
48
- out += "#{pref}identifier.type:: #{type}\n"
49
- out += "#{pref}identifier.value:: #{value}\n"
50
- out
51
- end
52
- end
53
-
54
- # Organization.
55
- class Organization < Contributor
56
- # @return [Array<RelatonBib::LocalizedString>]
57
- attr_reader :name
58
-
59
- # @return [RelatonBib::LocalizedString, nil]
60
- attr_reader :abbreviation
61
-
62
- # @return [Array<RelatonBib::LocalizedString>]
63
- attr_reader :subdivision
64
-
65
- # @return [Array<RelatonBib::OrgIdentifier>]
66
- attr_reader :identifier
67
-
68
- # @return [RelatonBib::Image, nil]
69
- attr_reader :logo
70
-
71
- # @param name [String, Hash, Array<String, Hash>]
72
- # @param abbreviation [RelatoBib::LocalizedString, String]
73
- # @param subdivision [Array<RelatoBib::LocalizedString>]
74
- # @param url [String]
75
- # @param identifier [Array<RelatonBib::OrgIdentifier>]
76
- # @param contact [Array<RelatonBib::Address, RelatonBib::Contact>]
77
- # @param logo [RelatonBib::Image, nil]
78
- def initialize(**args) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
79
- raise ArgumentError, "missing keyword: name" unless args[:name]
80
-
81
- super(url: args[:url], contact: args[:contact] || [])
82
-
83
- @name = if args[:name].is_a?(Array)
84
- args[:name].map { |n| localized_string(n) }
85
- else
86
- [localized_string(args[:name])]
87
- end
88
-
89
- @abbreviation = localized_string args[:abbreviation]
90
- @subdivision = (args[:subdivision] || []).map do |sd|
91
- localized_string sd
92
- end
93
- @identifier = args[:identifier] || []
94
- @logo = args[:logo]
95
- end
96
-
97
- def ==(other)
98
- name == other.name && abbreviation == other.abbreviation &&
99
- subdivision == other.subdivision && identifier == other.identifier &&
100
- logo == other.logo && super
101
- end
102
-
103
- # @param opts [Hash]
104
- # @option opts [Nokogiri::XML::Builder] :builder XML builder
105
- # @option opts [String] :lang language
106
- def to_xml(**opts) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength
107
- opts[:builder].organization do |builder|
108
- nm = name.select { |n| n.language&.include? opts[:lang] }
109
- nm = name unless nm.any?
110
- nm.each { |n| builder.name { |b| n.to_xml b } }
111
- sbdv = subdivision.select { |sd| sd.language&.include? opts[:lang] }
112
- sbdv = subdivision unless sbdv.any?
113
- sbdv.each { |sd| builder.subdivision { sd.to_xml builder } }
114
- builder.abbreviation { |a| abbreviation.to_xml a } if abbreviation
115
- builder.uri url if uri
116
- identifier.each { |identifier| identifier.to_xml builder }
117
- super builder
118
- builder.logo { |b| logo.to_xml b } if logo
119
- end
120
- end
121
-
122
- # @return [Hash]
123
- def to_hash # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity
124
- hash = { "name" => single_element_array(name) }
125
- hash["abbreviation"] = abbreviation.to_hash if abbreviation
126
- hash["identifier"] = single_element_array(identifier) if identifier&.any?
127
- if subdivision&.any?
128
- hash["subdivision"] = single_element_array(subdivision)
129
- end
130
- hash["logo"] = logo.to_hash if logo
131
- { "organization" => hash.merge(super) }
132
- end
133
-
134
- # @param prefix [String]
135
- # @param count [Integer]
136
- # @return [String]
137
- def to_asciibib(prefix = "", count = 1) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
138
- pref = prefix.sub(/\*$/, "organization")
139
- out = count > 1 ? "#{pref}::\n" : ""
140
- name.each { |n| out += n.to_asciibib "#{pref}.name", name.size }
141
- out += abbreviation.to_asciibib "#{pref}.abbreviation" if abbreviation
142
- subdivision.each do |sd|
143
- out += "#{pref}.subdivision::" if subdivision.size > 1
144
- out += sd.to_asciibib "#{pref}.subdivision"
145
- end
146
- identifier.each { |n| out += n.to_asciibib pref, identifier.size }
147
- out += super pref
148
- out += logo.to_asciibib "#{pref}.logo" if logo
149
- out
150
- end
151
-
152
- private
153
-
154
- # @param arg [String, Hash, RelatoBib::LocalizedString]
155
- # @return [RelatoBib::LocalizedString]
156
- def localized_string(arg)
157
- case arg
158
- when String then LocalizedString.new(arg)
159
- when Hash
160
- LocalizedString.new(arg[:content], arg[:language], arg[:script])
161
- when LocalizedString then arg
162
- end
163
- end
164
- end
165
- end