relaton-bib 1.14.4 → 1.14.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6d516a0a4756006072259b99674e50cefab3405f8824761c50faeca40bac5c71
4
- data.tar.gz: f349fc209e2f2f684e71ad1044b3c23d37dcf905f4852841509f0319bfcd42be
3
+ metadata.gz: b2ee0b39ee5595e61f628004eeff3f73bb136de969d1897911fb414dbafee43c
4
+ data.tar.gz: 76ebf35457db552b454bcad1ca85387338cd85a1cb78761b0a701065a21f56c9
5
5
  SHA512:
6
- metadata.gz: 99f2a47867468501d678c7b3807eb3be541dfd522ebe5f373b48499b82a3c4095c4bfe572967cd1f871e87b98e58d66c8207dc339a06a91355f25f560b06c4ad
7
- data.tar.gz: a69cad9aaee6bd53fb2e17dc55102e2c37738142d6f7a6978d5db3d4524b3b29a276df85ca9feb95567f173dfae6d574c46c2a5da97c771774c8de5de084dd24
6
+ metadata.gz: e9526cb48e9a8a36d71c0b1b560b15a4d20f53bda8836ec15f4d020314881c53791e2f9b5e59f8a27acd1191e1cb16a80e34dc3b7c0fb2823784be9b6ae992de
7
+ data.tar.gz: 7085340f765f6c27e588d79deefbaa72326a6b5f7c13130b880c0c2e2d97031d96668b4a88fe7d5dfcfd613c26a6434945e38f83558fe5ebed3fa3372b87ad64
@@ -62,9 +62,10 @@ module RelatonBib
62
62
  when "chapter" then item.chapter = reference_from
63
63
  when "page"
64
64
  value = reference_from
65
- value += "-#{reference_to}" if reference_to
65
+ value += "--#{reference_to}" if reference_to
66
66
  item.pages = value
67
67
  when "volume" then item.volume = reference_from
68
+ when "issue" then item.number = reference_from
68
69
  end
69
70
  end
70
71
  end
@@ -448,7 +448,7 @@ module RelatonBib
448
448
  # @return [String]
449
449
  #
450
450
  def to_bibtex(bibtex = nil)
451
- bibtext_item(bibtex).to_s
451
+ Renderer::BibtexBuilder.build(self, bibtex).to_s
452
452
  end
453
453
 
454
454
  #
@@ -459,7 +459,9 @@ module RelatonBib
459
459
  # @return [Hash] citeproc
460
460
  #
461
461
  def to_citeproc(bibtex = nil)
462
- bibtext_item(bibtex).to_citeproc.map { |cp| cp.transform_keys(&:to_s) }
462
+ Renderer::BibtexBuilder.build(self, bibtex).to_citeproc.map do |cp|
463
+ cp.transform_keys(&:to_s)
464
+ end
463
465
  end
464
466
 
465
467
  # @param lang [String, nil] language code Iso639
@@ -590,162 +592,6 @@ module RelatonBib
590
592
 
591
593
  private
592
594
 
593
- #
594
- # Create BibTeX item for this document
595
- #
596
- # @param [BibTeX::Bibliography, nil] bibtex <description>
597
- #
598
- # @return [BibTeX::Bibliography] BibTeX bibliography
599
- #
600
- def bibtext_item(bibtex) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
601
- item = BibTeX::Entry.new
602
- item.type = bibtex_type
603
- item.key = id
604
- title.to_bibtex item
605
- item.edition = edition.content if edition
606
- bibtex_author item
607
- bibtex_contributor item
608
- item.address = place.first.name if place.any?
609
- bibtex_note item
610
- bibtex_relation item
611
- bibtex_extent item
612
- bibtex_date item
613
- bibtex_series item
614
- bibtex_classification item
615
- item.keywords = keyword.map(&:content).join(", ") if keyword.any?
616
- bibtex_docidentifier item
617
- item.timestamp = fetched.to_s if fetched
618
- bibtex_link item
619
- bibtex ||= BibTeX::Bibliography.new
620
- bibtex << item
621
- bibtex
622
- end
623
-
624
- # @return [String]
625
- def bibtex_type
626
- case type
627
- when "standard", nil then "misc"
628
- else type
629
- end
630
- end
631
-
632
- # @param [BibTeX::Entry]
633
- def bibtex_author(item) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/AbcSize
634
- authors = contributor.select do |c|
635
- c.entity.is_a?(Person) && c.role.map(&:type).include?("author")
636
- end.map &:entity
637
-
638
- return unless authors.any?
639
-
640
- item.author = authors.map do |a|
641
- if a.name.surname
642
- "#{a.name.surname}, #{a.name.forename.map(&:to_s).join(' ')}"
643
- else
644
- a.name.completename.to_s
645
- end
646
- end.join " and "
647
- end
648
-
649
- # @param [BibTeX::Entry]
650
- def bibtex_contributor(item) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength
651
- contributor.each do |c|
652
- rls = c.role.map(&:type)
653
- if rls.include?("publisher") then item.publisher = c.entity.name
654
- elsif rls.include?("distributor")
655
- case type
656
- when "techreport" then item.institution = c.entity.name
657
- when "inproceedings", "conference", "manual", "proceedings"
658
- item.organization = c.entity.name
659
- when "mastersthesis", "phdthesis" then item.school = c.entity.name
660
- end
661
- end
662
- end
663
- end
664
-
665
- # @param [BibTeX::Entry]
666
- def bibtex_note(item) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/AbcSize
667
- biblionote.each do |n|
668
- case n.type
669
- when "annote" then item.annote = n.content
670
- when "howpublished" then item.howpublished = n.content
671
- when "comment" then item.comment = n.content
672
- when "tableOfContents" then item.content = n.content
673
- when nil then item.note = n.content
674
- end
675
- end
676
- end
677
-
678
- # @param [BibTeX::Entry]
679
- def bibtex_relation(item)
680
- rel = relation.detect { |r| r.type == "partOf" }
681
- if rel
682
- title_main = rel.bibitem.title.detect { |t| t.type == "main" }
683
- item.booktitle = title_main.title.content
684
- end
685
- end
686
-
687
- # @param [BibTeX::Entry]
688
- def bibtex_extent(item)
689
- extent.each { |e| e.to_bibtex(item) }
690
- end
691
-
692
- # @param [BibTeX::Entry]
693
- def bibtex_date(item)
694
- date.each do |d|
695
- case d.type
696
- when "published"
697
- item.year = d.on :year
698
- item.month = d.on :month
699
- when "accessed" then item.urldate = d.on.to_s
700
- end
701
- end
702
- end
703
-
704
- # @param [BibTeX::Entry]
705
- def bibtex_series(item)
706
- series.each do |s|
707
- case s.type
708
- when "journal"
709
- item.journal = s.title.title
710
- item.number = s.number if s.number
711
- when nil then item.series = s.title.title
712
- end
713
- end
714
- end
715
-
716
- # @param [BibTeX::Entry]
717
- def bibtex_classification(item)
718
- classification.each do |c|
719
- case c.type
720
- when "type" then item["type"] = c.value
721
- # when "keyword" then item.keywords = c.value
722
- when "mendeley" then item["mendeley-tags"] = c.value
723
- end
724
- end
725
- end
726
-
727
- # @param [BibTeX::Entry]
728
- def bibtex_docidentifier(item)
729
- docidentifier.each do |i|
730
- case i.type
731
- when "isbn" then item.isbn = i.id
732
- when "lccn" then item.lccn = i.id
733
- when "issn" then item.issn = i.id
734
- end
735
- end
736
- end
737
-
738
- # @param [BibTeX::Entry]
739
- def bibtex_link(item)
740
- link.each do |l|
741
- case l.type
742
- when "doi" then item.doi = l.content
743
- when "file" then item.file2 = l.content
744
- when "src" then item.url = l.content
745
- end
746
- end
747
- end
748
-
749
595
  # @param opts [Hash]
750
596
  # @option opts [Nokogiri::XML::Builder] :builder XML builder
751
597
  # @option opts [Boolean] bibdata
@@ -2,6 +2,7 @@ require "bibtex"
2
2
  require "iso639"
3
3
 
4
4
  module RelatonBib
5
+ # @todo: move this class to the RelatonBib::Bibtex module
5
6
  class BibtexParser
6
7
  class << self
7
8
  # @param bibtex [String]
@@ -0,0 +1,312 @@
1
+ # Monkey patch to fix the issue with month quotes in BibTeX
2
+ module BibTeX
3
+ class Value
4
+ def to_s(options = {})
5
+ if options.key?(:filter)
6
+ opts = options.reject { |k,| k == :filter || (k == :quotes && (!atomic? || symbol?)) }
7
+ return convert(options[:filter]).to_s(opts)
8
+ end
9
+
10
+ return value.to_s unless options.key?(:quotes) && atomic?
11
+
12
+ q = Array(options[:quotes])
13
+ [q[0], value, q[-1]].compact.join
14
+ end
15
+ end
16
+ end
17
+
18
+ module RelatonBib
19
+ # BibTeX builder.
20
+ module Renderer
21
+ class BibtexBuilder
22
+ ATTRS = %i[
23
+ type id title author editor booktitle series number edition contributor
24
+ date address note relation extent classification keyword docidentifier
25
+ timestamp link
26
+ ].freeze
27
+
28
+ #
29
+ # Build BibTeX bibliography.
30
+ #
31
+ # @param bib [RelatonBib::BibliographicItem]
32
+ # @param bibtex [BibTeX::Bibliography, nil] BibTeX bibliography
33
+ #
34
+ # @return [BibTeX::Bibliography] BibTeX bibliography
35
+ #
36
+ def self.build(bib, bibtex = nil)
37
+ new(bib).build bibtex
38
+ end
39
+
40
+ #
41
+ # Initialize BibTeX builder.
42
+ #
43
+ # @param bib [RelatonBib::BibliographicItem]
44
+ def initialize(bib)
45
+ @bib = bib
46
+ end
47
+
48
+ #
49
+ # Build BibTeX bibliography.
50
+ #
51
+ # @param bibtex [BibTeX::Bibliography, nil] BibTeX bibliography
52
+ #
53
+ # @return [BibTeX::Bibliography] BibTeX bibliography
54
+ #
55
+ def build(bibtex = nil)
56
+ @item = BibTeX::Entry.new
57
+ ATTRS.each { |a| send("add_#{a}") }
58
+ bibtex ||= BibTeX::Bibliography.new
59
+ bibtex << @item
60
+ bibtex
61
+ end
62
+
63
+ private
64
+
65
+ #
66
+ # Add type to BibTeX item
67
+ #
68
+ def add_type
69
+ @item.type = bibtex_type
70
+ end
71
+
72
+ # @return [String] BibTeX type
73
+ def bibtex_type
74
+ case @bib.type
75
+ when "standard", nil then "misc"
76
+ else @bib.type
77
+ end
78
+ end
79
+
80
+ #
81
+ # Add ID to BibTeX item
82
+ #
83
+ def add_id
84
+ @item.key = @bib.id
85
+ end
86
+
87
+ #
88
+ # Add title to BibTeX item
89
+ #
90
+ def add_title
91
+ @bib.title.to_bibtex @item
92
+ end
93
+
94
+ #
95
+ # Add booktitle to BibTeX item
96
+ #
97
+ def add_booktitle
98
+ included_in = @bib.relation.detect { |r| r.type == "includedIn" }
99
+ return unless included_in
100
+
101
+ @item.booktitle = included_in.bibitem.title.first.title
102
+ end
103
+
104
+ #
105
+ # Add author to BibTeX item
106
+ #
107
+ def add_author
108
+ add_author_editor "author"
109
+ end
110
+
111
+ #
112
+ # Add editor to BibTeX item
113
+ #
114
+ def add_editor
115
+ add_author_editor "editor"
116
+ end
117
+
118
+ #
119
+ # Add author or editor to BibTeX item
120
+ #
121
+ # @param [String] type "author" or "editor"
122
+ #
123
+ def add_author_editor(type)
124
+ contribs = @bib.contributor.select do |c|
125
+ c.entity.is_a?(Person) && c.role.any? { |e| e.type == type }
126
+ end.map &:entity
127
+
128
+ return unless contribs.any?
129
+
130
+ @item.send "#{type}=", concat_names(contribs)
131
+ end
132
+
133
+ #
134
+ # Concatenate names of contributors
135
+ #
136
+ # @param [Array<RelatonBib::Person>] contribs contributors
137
+ #
138
+ # @return [String] concatenated names
139
+ #
140
+ def concat_names(contribs)
141
+ contribs.map do |p|
142
+ if p.name.surname
143
+ "#{p.name.surname}, #{p.name.forename.map(&:to_s).join(' ')}"
144
+ else
145
+ p.name.completename.to_s
146
+ end
147
+ end.join(" and ")
148
+ end
149
+
150
+ #
151
+ # Add series to BibTeX item
152
+ #
153
+ def add_series
154
+ @bib.series.each do |s|
155
+ case s.type
156
+ when "journal"
157
+ @item.journal = s.title.title
158
+ @item.number = s.number if s.number
159
+ when nil then @item.series = s.title.title
160
+ end
161
+ end
162
+ end
163
+
164
+ #
165
+ # Add number to BibTeX item
166
+ #
167
+ def add_number
168
+ return unless %w[techreport manual].include? @bib.type
169
+
170
+ did = @bib.docidentifier.detect { |i| i.primary == true }
171
+ did ||= @bib.docidentifier.first
172
+ @item.number = did.id if did
173
+ end
174
+
175
+ #
176
+ # Add edition to BibTeX item
177
+ #
178
+ def add_edition
179
+ @item.edition = @bib.edition.content if @bib.edition
180
+ end
181
+
182
+ #
183
+ # Add contributor to BibTeX item
184
+ #
185
+ def add_contributor # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
186
+ @bib.contributor.each do |c|
187
+ rls = c.role.map(&:type)
188
+ if rls.include?("publisher") then @item.publisher = c.entity.name
189
+ elsif rls.include?("distributor")
190
+ case @bib.type
191
+ when "techreport" then @item.institution = c.entity.name
192
+ when "inproceedings", "conference", "manual", "proceedings"
193
+ @item.organization = c.entity.name
194
+ when "mastersthesis", "phdthesis" then @item.school = c.entity.name
195
+ end
196
+ end
197
+ end
198
+ end
199
+
200
+ #
201
+ # Add date to BibTeX item
202
+ #
203
+ def add_date
204
+ @bib.date.each do |d|
205
+ case d.type
206
+ when "published"
207
+ @item.year = d.on :year
208
+ month = d.on :month
209
+ @item.month = month if month
210
+ when "accessed" then @item.urldate = d.on.to_s
211
+ end
212
+ end
213
+ end
214
+
215
+ #
216
+ # Add address to BibTeX item
217
+ #
218
+ def add_address # rubocop:disable Metrics/AbcSize
219
+ return unless @bib.place.any?
220
+
221
+ reg = @bib.place[0].region[0].name if @bib.place[0].region.any?
222
+ addr = [@bib.place[0].name, @bib.place[0].city, reg]
223
+ @item.address = addr.compact.join(", ")
224
+ end
225
+
226
+ #
227
+ # Add note to BibTeX item
228
+ #
229
+ def add_note # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
230
+ @bib.biblionote.each do |n|
231
+ case n.type
232
+ when "annote" then @item.annote = n.content
233
+ when "howpublished" then @item.howpublished = n.content
234
+ when "comment" then @item.comment = n.content
235
+ when "tableOfContents" then @item.content = n.content
236
+ when nil then @item.note = n.content
237
+ end
238
+ end
239
+ end
240
+
241
+ #
242
+ # Add relation to BibTeX item
243
+ #
244
+ def add_relation
245
+ rel = @bib.relation.detect { |r| r.type == "partOf" }
246
+ if rel
247
+ title_main = rel.bibitem.title.detect { |t| t.type == "main" }
248
+ @item.booktitle = title_main.title.content
249
+ end
250
+ end
251
+
252
+ #
253
+ # Add extent to BibTeX item
254
+ #
255
+ def add_extent
256
+ @bib.extent.each { |e| e.to_bibtex(@item) }
257
+ end
258
+
259
+ #
260
+ # Add classification to BibTeX item
261
+ #
262
+ def add_classification
263
+ @bib.classification.each do |c|
264
+ case c.type
265
+ when "type" then @item["type"] = c.value
266
+ when "mendeley" then @item["mendeley-tags"] = c.value
267
+ end
268
+ end
269
+ end
270
+
271
+ #
272
+ # Add keywords to BibTeX item
273
+ #
274
+ def add_keyword
275
+ @item.keywords = @bib.keyword.map(&:content).join(", ") if @bib.keyword.any?
276
+ end
277
+
278
+ #
279
+ # Add docidentifier to BibTeX item
280
+ #
281
+ def add_docidentifier
282
+ @bib.docidentifier.each do |i|
283
+ case i.type
284
+ when "isbn" then @item.isbn = i.id
285
+ when "lccn" then @item.lccn = i.id
286
+ when "issn" then @item.issn = i.id
287
+ end
288
+ end
289
+ end
290
+
291
+ #
292
+ # Add identifier to BibTeX item
293
+ #
294
+ def add_timestamp
295
+ @item.timestamp = @bib.fetched.to_s if @bib.fetched
296
+ end
297
+
298
+ #
299
+ # Add link to BibTeX item
300
+ #
301
+ def add_link
302
+ @bib.link.each do |l|
303
+ case l.type&.downcase
304
+ when "doi" then @item.doi = l.content
305
+ when "file" then @item.file2 = l.content
306
+ when "src" then @item.url = l.content
307
+ end
308
+ end
309
+ end
310
+ end
311
+ end
312
+ end
@@ -1,3 +1,3 @@
1
1
  module RelatonBib
2
- VERSION = "1.14.4".freeze
2
+ VERSION = "1.14.5".freeze
3
3
  end
data/lib/relaton_bib.rb CHANGED
@@ -11,6 +11,7 @@ require "relaton_bib/bibliographic_item"
11
11
  require "relaton_bib/hit_collection"
12
12
  require "relaton_bib/hit"
13
13
  require "relaton_bib/bibxml_parser"
14
+ require "relaton_bib/renderer/bibtex_builder"
14
15
 
15
16
  module RelatonBib
16
17
  class Error < StandardError; end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relaton-bib
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.14.4
4
+ version: 1.14.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-01-07 00:00:00.000000000 Z
11
+ date: 2023-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug
@@ -266,6 +266,7 @@ files:
266
266
  - lib/relaton_bib/organization.rb
267
267
  - lib/relaton_bib/person.rb
268
268
  - lib/relaton_bib/place.rb
269
+ - lib/relaton_bib/renderer/bibtex_builder.rb
269
270
  - lib/relaton_bib/series.rb
270
271
  - lib/relaton_bib/structured_identifier.rb
271
272
  - lib/relaton_bib/technical_committee.rb