relaton-bib 1.12.4 → 1.12.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: af4361f2c87e045e86e3ca8dd81e4e729f88598476daf14be487d8d5972bb9c8
4
- data.tar.gz: 962832013669c49bd25b813d003f454a552d7427b45774a1e936c2146f31c391
3
+ metadata.gz: be4b986a04f391a2e552c13d8ee897e73c398f3acb27deda3bb203da627f585b
4
+ data.tar.gz: 05540e11934396b938df0c902d926c37b1c49c8aa215b7888827bb42d19d4042
5
5
  SHA512:
6
- metadata.gz: cee39b14e0fe58a1db12205f3294144103c46fe8e82cf48a81c95195952d9130d999e5f8bb809ce2a500100d05fa3692d2bbf19a341598c37304f86a4f2d077c
7
- data.tar.gz: afd5a003dc908a9bc895269ff4e3d0ca46762c16f7c3ec7d4007f86b35d150f3251994e9ee3dffd63095fe6fba75607be5ef461903064c408cbbb91230a02895
6
+ metadata.gz: f82094e85351832f3738216ce8ba72c68f7f26e9bcd7379cbe49b2e9a2bc0318ce1d4507462d3dbfed61ba57fc9bc692daf4998cbc84fb3bc2f54a14bed7e72a
7
+ data.tar.gz: 66de7c5f7b612c203694d6adacb0fdbf09dd34380f24bfe6e3d298f50d61d52af844f2803f8e75644839ce0b256726db55fc9058a649f9f42c8c3198f6e279ab
@@ -109,8 +109,8 @@ module RelatonBib
109
109
  # @return [Date]
110
110
  def parse_date(date)
111
111
  case date
112
- when /^\d{4}-\d{2}-\d{2}/ then Date.parse(date) # 2012-02-11
113
- when /^\d{4}-\d{2}/ then Date.strptime(date, "%Y-%m") # 2012-02
112
+ when /^\d{4}-\d{1,2}-\d{1,2}/ then Date.parse(date) # 2012-02-11
113
+ when /^\d{4}-\d{1,2}/ then Date.strptime(date, "%Y-%m") # 2012-02
114
114
  when /^\d{4}/ then Date.strptime(date, "%Y") # 2012
115
115
  else date
116
116
  end
@@ -17,7 +17,7 @@ module RelatonBib
17
17
  ].freeze
18
18
 
19
19
  # @return [String]
20
- attr_reader :type
20
+ attr_accessor :type
21
21
 
22
22
  # @return [RelatonBib::FormattedString, NilClass]
23
23
  attr_reader :description
@@ -6,7 +6,7 @@ module RelatonBib
6
6
  extend Forwardable
7
7
 
8
8
  def_delegators :@array, :<<, :[], :first, :last, :empty?, :any?, :size,
9
- :each, :detect, :map, :reduce, :length, :unshift
9
+ :each, :detect, :map, :reduce, :length, :unshift, :max_by
10
10
 
11
11
  # @param relation [Array<RelatonBib::DocumentRelation, Hash>]
12
12
  # @option relation [String] :type
@@ -21,6 +21,21 @@ module RelatonBib
21
21
  @array = relation.map { |r| r.is_a?(Hash) ? DocumentRelation.new(**r) : r }
22
22
  end
23
23
 
24
+ #
25
+ # Returns new DocumentRelationCollection with selected relations.
26
+ #
27
+ # @example Select relations with type "obsoletes"
28
+ # relations.select { |r| r.type == "obsoletes" }
29
+ # #=> <RelatonBib::DocRelationCollection:0x00007f9a0191d5f0 @array=[...]>
30
+ #
31
+ # @return [RelatonBib::DocRelationCollection] new DocumentRelationCollection
32
+ # with selected relations
33
+ #
34
+ def select(&block)
35
+ arr = @array.select(&block)
36
+ self.class.new arr
37
+ end
38
+
24
39
  # @todo We don't have replace type anymore. Suppose we should update this
25
40
  # method or delete it.
26
41
  #
@@ -6,15 +6,15 @@ module RelatonBib
6
6
  # @return [RelatonBib::DocumentStatus::Stage]
7
7
  attr_reader :stage
8
8
 
9
- # @return [RelatonBib::DocumentStatus::Stage, NilClass]
9
+ # @return [RelatonBib::DocumentStatus::Stage, nil]
10
10
  attr_reader :substage
11
11
 
12
- # @return [String, NilClass]
12
+ # @return [String, nil]
13
13
  attr_reader :iteration
14
14
 
15
15
  # @param stage [String, Hash, RelatonBib::DocumentStatus::Stage]
16
- # @param substage [String, Hash, NilClass, RelatonBib::DocumentStatus::Stage]
17
- # @param iteration [String, NilClass]
16
+ # @param substage [String, Hash, nil, RelatonBib::DocumentStatus::Stage]
17
+ # @param iteration [String, nil]
18
18
  def initialize(stage:, substage: nil, iteration: nil)
19
19
  @stage = stage_new stage
20
20
  @substage = stage_new substage
@@ -51,7 +51,7 @@ module RelatonBib
51
51
 
52
52
  private
53
53
 
54
- # @param stg [RelatonBib::DocumentStatus::Stage, Hash, String, NilClass]
54
+ # @param stg [RelatonBib::DocumentStatus::Stage, Hash, String, nil]
55
55
  # @return [RelatonBib::DocumentStatus::Stage]
56
56
  def stage_new(stg)
57
57
  case stg
@@ -65,11 +65,11 @@ module RelatonBib
65
65
  # @return [String]
66
66
  attr_reader :value
67
67
 
68
- # @return [String, NilClass]
68
+ # @return [String, nil]
69
69
  attr_reader :abbreviation
70
70
 
71
71
  # @param value [String]
72
- # @param abbreviation [String, NilClass]
72
+ # @param abbreviation [String, nil]
73
73
  def initialize(value:, abbreviation: nil)
74
74
  @value = value
75
75
  @abbreviation = abbreviation
@@ -68,14 +68,17 @@ module RelatonBib
68
68
  # @return [String] encoded content
69
69
  #
70
70
  def encode(cnt) # rubocop:disable Metrics/MethodLength
71
- regex = /(?<prf>.*?)(?<xml><(?<tag>\w+)>.*<\/\k<tag>>)(?<sfx>.*)/m
71
+ return unless cnt
72
+
73
+ # regex = /(?<prf>.*?)(?<xml><(?<tag>\w+)>.*<\/\k<tag>>)(?<sfx>.*)/m
74
+ regex = /(?<prf>.*?)(?<xml><(?<tag>\w+)[^>]*(?:>.*<\/\k<tag>)?>)(?<rest>.*)/m
72
75
  if cnt.match(regex)
73
76
  prf = Regexp.last_match(:prf).lstrip
74
77
  xml = Regexp.last_match[:xml]
75
- sfx = Regexp.last_match(:sfx).rstrip
76
- parts = xml.scan(/\s*<(?<tago>\w+)>(?<cnt1>.*?)(?=<\/?\w+>)|(?<cnt2>.*?)<\/(?<tagc>\w+)>/)
78
+ rest = Regexp.last_match(:rest).rstrip
79
+ parts = xml.scan(/\s*<(?<tago>\w+)(?<attrs>[^>]*)>(?:(?<cnt1>.*?)(?=<\/\w+>|<\w+[^>]*>))?|(?<cnt2>.*?)<\/(?<tagc>\w+)>/)
77
80
  out = scan_xml parts
78
- "#{escp(prf)}#{out}#{escp(sfx)}"
81
+ "#{escp(prf)}#{out}#{encode(rest)}"
79
82
  else
80
83
  escp cnt
81
84
  end
@@ -84,22 +87,24 @@ module RelatonBib
84
87
  #
85
88
  # Scan XML and escape HTML entities.
86
89
  #
87
- # @param [Array<Array<String,nik>>] parts XML parts
90
+ # @param [Array<Array<String,nil>>] parts XML parts
88
91
  #
89
92
  # @return [String] output string
90
93
  #
91
94
  def scan_xml(parts) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength
92
95
  return "" unless parts.any? && parts.first[0]
93
96
 
94
- tago, cnt1, = parts.shift
95
- if tago && tago == parts.first[3]
96
- _, _, cnt2, tagc = parts.shift
97
- "<#{tago}>#{escp(cnt1)}#{escp(cnt2)}</#{tagc}>"
97
+ tago, attrs, cnt1, = parts.shift
98
+ if tago && tago == parts.first&.last
99
+ _, _, _, cnt2, tagc = parts.shift
100
+ "<#{tago}#{attrs}>#{escp(cnt1)}#{escp(cnt2)}</#{tagc}>"
101
+ elsif tago && attrs && attrs[-1] == "/"
102
+ "<#{tago}#{attrs}>"
98
103
  elsif tago
99
104
  inr = scan_xml parts
100
- _, _, cnt2, tagc = parts.shift
105
+ _, _, _, cnt2, tagc = parts.shift
101
106
  if tago == tagc
102
- "<#{tago}>#{escp(cnt1)}#{inr}#{escp(cnt2)}</#{tagc}>"
107
+ "<#{tago}#{attrs}>#{escp(cnt1)}#{inr}#{escp(cnt2)}</#{tagc}>"
103
108
  else
104
109
  "#{escp("<#{tago}>#{cnt1}")}#{inr}#{escp("#{cnt2}</#{tagc}>")}"
105
110
  end
@@ -68,7 +68,7 @@ module RelatonBib
68
68
  # @param [BibTeX::Entry] item bibtex entry
69
69
  #
70
70
  def to_bibtex(item)
71
- tl = titles.detect { |t| t.type == "main" }
71
+ tl = titles.detect { |t| t.type == "main" } || titles.first
72
72
  return unless tl
73
73
 
74
74
  item.title = tl.title.content
@@ -1,3 +1,3 @@
1
1
  module RelatonBib
2
- VERSION = "1.12.4".freeze
2
+ VERSION = "1.12.7".freeze
3
3
  end
@@ -472,7 +472,7 @@ module RelatonBib
472
472
  return unless ident
473
473
 
474
474
  FormattedRef.new(
475
- content: ident&.text, format: ident[:format],
475
+ content: ident.children.to_s, format: ident[:format],
476
476
  language: ident[:language], script: ident[:script]
477
477
  )
478
478
  end
data/lib/relaton_bib.rb CHANGED
@@ -23,20 +23,33 @@ module RelatonBib
23
23
 
24
24
  case date.to_s
25
25
  when /(?<date>\w+\s\d{4})/ # February 2012
26
- d = Date.strptime($~[:date], "%B %Y")
27
- str ? d.strftime("%Y-%m") : d
26
+ format_date $~[:date], "%B %Y", str, "%Y-%m"
28
27
  when /(?<date>\w+\s\d{1,2},\s\d{4})/ # February 11, 2012
29
- d = Date.strptime($~[:date], "%B %d, %Y")
30
- str ? d.strftime("%Y-%m-%d") : d
31
- when /(?<date>\d{4}-\d{2}-\d{2})/ # 2012-02-11
32
- str ? $~[:date] : Date.strptime($~[:date], "%Y-%m-%d")
33
- when /(?<date>\d{4}-\d{2})/ # 2012-02
34
- str ? $~[:date] : Date.strptime($~[:date], "%Y-%m")
28
+ format_date $~[:date], "%B %d, %Y", str, "%Y-%m-%d"
29
+ when /(?<date>\d{4}-\d{1,2}-\d{1,2})/ # 2012-02-03 or 2012-2-3
30
+ format_date $~[:date], "%Y-%m-%d", str
31
+ when /(?<date>\d{4}-\d{1,2})/ # 2012-02 or 2012-2
32
+ format_date $~[:date], "%Y-%m", str
35
33
  when /(?<date>\d{4})/ # 2012
36
- str ? $~[:date] : Date.strptime($~[:date], "%Y")
34
+ format_date $~[:date], "%Y", str
37
35
  end
38
36
  end
39
37
 
38
+ #
39
+ # Parse date string to Date object and format it
40
+ #
41
+ # @param [String] date date string
42
+ # @param [String] format format string
43
+ # @param [Boolean] str return string if true in other case return Date
44
+ # @param [String, nil] outformat output format
45
+ #
46
+ # @return [Date, String] date object or formatted date string
47
+ #
48
+ def format_date(date, format, str, outformat = nil)
49
+ date = Date.strptime(date, format)
50
+ str ? date.strftime(outformat || format) : date
51
+ end
52
+
40
53
  # @param arr [NilClass, Array, #is_a?]
41
54
  # @return [Array]
42
55
  def array(arr)
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.12.4
4
+ version: 1.12.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-29 00:00:00.000000000 Z
11
+ date: 2022-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug