nhkore 0.3.7 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,23 +1,11 @@
1
- #!/usr/bin/env ruby
2
1
  # encoding: UTF-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  #--
6
5
  # This file is part of NHKore.
7
- # Copyright (c) 2020 Jonathan Bradley Whited (@esotericpig)
8
- #
9
- # NHKore is free software: you can redistribute it and/or modify
10
- # it under the terms of the GNU Lesser General Public License as published by
11
- # the Free Software Foundation, either version 3 of the License, or
12
- # (at your option) any later version.
13
- #
14
- # NHKore is distributed in the hope that it will be useful,
15
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
- # GNU Lesser General Public License for more details.
18
- #
19
- # You should have received a copy of the GNU Lesser General Public License
20
- # along with NHKore. If not, see <https://www.gnu.org/licenses/>.
6
+ # Copyright (c) 2020-2021 Jonathan Bradley Whited
7
+ #
8
+ # SPDX-License-Identifier: LGPL-3.0-or-later
21
9
  #++
22
10
 
23
11
 
@@ -30,12 +18,12 @@ require 'nhkore/util'
30
18
 
31
19
  module NHKore
32
20
  ###
33
- # @author Jonathan Bradley Whited (@esotericpig)
21
+ # @author Jonathan Bradley Whited
34
22
  # @since 0.3.4
35
23
  ###
36
24
  class DatetimeParser
37
25
  extend AttrBool::Ext
38
-
26
+
39
27
  # Order matters!
40
28
  FMTS = [
41
29
  '%Y-%m-%d %H:%M',
@@ -54,13 +42,13 @@ module NHKore
54
42
  '%H:%M',
55
43
  '%d',
56
44
  '%Y',
57
- ]
58
-
45
+ ].freeze
46
+
59
47
  def self.guess_year(year)
60
48
  if year < 1000
61
49
  century = Util::JST_YEAR / 100 * 100 # 2120 -> 2100
62
50
  millennium = Util::JST_YEAR / 1000 * 1000 # 2120 -> 2000
63
-
51
+
64
52
  # If year <= 23 (2022 -> 23)...
65
53
  if year <= ((Util::JST_YEAR % 100) + 1)
66
54
  # Assume this century.
@@ -71,34 +59,34 @@ module NHKore
71
59
  # Assume this millennium.
72
60
  # So if the current year is 2200, and year is 150,
73
61
  # then it will be 2000 + 150 = 2150.
74
- year = millennium + year
75
62
  else
76
63
  # Assume previous millennium (2000 -> 1000),
77
64
  # so year 999 will become 1999.
78
65
  millennium -= 1000 if millennium >= 1000
79
- year = millennium + year
80
66
  end
67
+
68
+ year = millennium + year
81
69
  else
82
70
  # Assume previous century (2000 -> 1900).
83
71
  century -= 100 if century >= 100
84
72
  year = century + year
85
73
  end
86
74
  end
87
-
75
+
88
76
  return year
89
77
  end
90
-
78
+
91
79
  def self.parse_range(value)
92
80
  # Do not use unspace_web_str(), want spaces for formats.
93
81
  value = Util.strip_web_str(Util.reduce_space(value))
94
82
  values = value.split('...',2)
95
-
96
- return nil if values.empty?() # For '' or '...'
97
-
83
+
84
+ return nil if values.empty? # For '' or '...'
85
+
98
86
  # For '2020...' or '...2020'.
99
87
  if value.include?('...')
100
88
  # values.length is always 2 because of 2 in split() above.
101
-
89
+
102
90
  # For '2020...'.
103
91
  if Util.empty_web_str?(values[1])
104
92
  values[1] = :infinity
@@ -107,81 +95,79 @@ module NHKore
107
95
  values[0] = :infinity
108
96
  end
109
97
  end
110
-
98
+
111
99
  datetimes = [
112
- DatetimeParser.new(), # "From" date time
113
- DatetimeParser.new(), # "To" date time
100
+ DatetimeParser.new, # "From" date time
101
+ DatetimeParser.new, # "To" date time
114
102
  ]
115
-
116
- values.each_with_index() do |v,i|
103
+
104
+ values.each_with_index do |v,i|
117
105
  dt = datetimes[i]
118
-
106
+
119
107
  # Minimum/Maximum date time for '2020...' or '...2020'.
120
108
  if v == :infinity
121
109
  # "From" date time.
122
110
  if i == 0
123
- dt.min!()
111
+ dt.min!
124
112
  # "To" date time.
125
113
  else
126
- dt.max!()
114
+ dt.max!
127
115
  end
128
116
  else
129
117
  v = Util.strip_web_str(v)
130
-
131
- FMTS.each_with_index() do |fmt,i|
132
- begin
133
- # If don't do this, "%d" values will be parsed using "%d %H".
134
- # It seems as though strptime() ignores space.
135
- raise ArgumentError if fmt.include?(' ') && !v.include?(' ')
136
-
137
- # If don't do this, "%y..." values will be parsed using "%d...".
138
- raise ArgumentError if fmt.start_with?('%d') && v.split(' ')[0].length > 2
139
-
140
- dt.parse!(v,fmt)
141
-
142
- break # No problem; this format worked
143
- rescue ArgumentError
144
- # Out of formats.
145
- raise if i >= (FMTS.length - 1)
146
- end
118
+
119
+ FMTS.each_with_index do |fmt,j|
120
+ # If don't do this, "%d" values will be parsed using "%d %H".
121
+ # It seems as though strptime() ignores space.
122
+ raise ArgumentError if fmt.include?(' ') && !v.include?(' ')
123
+
124
+ # If don't do this, "%y..." values will be parsed using "%d...".
125
+ raise ArgumentError if fmt.start_with?('%d') && v.split(' ')[0].length > 2
126
+
127
+ dt.parse!(v,fmt)
128
+
129
+ break # No problem; this format worked
130
+ rescue ArgumentError
131
+ # Out of formats.
132
+ raise if j >= (FMTS.length - 1)
147
133
  end
148
134
  end
149
135
  end
150
-
136
+
151
137
  from = datetimes[0]
152
138
  to = datetimes[1]
153
-
139
+
154
140
  from.autofill!(:from,to)
155
141
  to.autofill!(:to,from)
156
-
157
- return [from.jst_time(),to.jst_time()]
142
+
143
+ return [from.jst_time,to.jst_time]
158
144
  end
159
-
145
+
160
146
  attr_accessor :day
161
147
  attr_accessor :hour
162
148
  attr_accessor :min
163
149
  attr_accessor :month
164
150
  attr_accessor :sec
165
151
  attr_accessor :year
166
-
152
+
167
153
  attr_accessor? :has_day
168
154
  attr_accessor? :has_hour
169
155
  attr_accessor? :has_min
170
156
  attr_accessor? :has_month
171
157
  attr_accessor? :has_sec
172
158
  attr_accessor? :has_year
173
-
159
+
174
160
  attr_reader? :min_or_max
175
-
161
+
176
162
  def initialize(year=nil,month=nil,day=nil,hour=nil,min=nil,sec=nil)
177
163
  super()
178
-
164
+
179
165
  set!(year,month,day,hour,min,sec)
180
-
166
+
181
167
  self.has = false
182
168
  @min_or_max = false
183
169
  end
184
-
170
+
185
171
  def autofill!(type,other)
186
172
  case type
187
173
  when :from
@@ -191,15 +177,15 @@ module NHKore
191
177
  else
192
178
  raise ArgumentError,"invalid type[#{type}]"
193
179
  end
194
-
180
+
195
181
  return self if @min_or_max
196
-
182
+
197
183
  has_small = false
198
184
  jst_now = Util.jst_now()
199
-
185
+
200
186
  # Must be from smallest to biggest.
201
-
202
- if @has_sec || other.has_sec?()
187
+
188
+ if @has_sec || other.has_sec?
203
189
  @sec = other.sec unless @has_sec
204
190
  has_small = true
205
191
  else
@@ -209,8 +195,8 @@ module NHKore
209
195
  @sec = is_from ? 0 : 59
210
196
  end
211
197
  end
212
-
213
- if @has_min || other.has_min?()
198
+
199
+ if @has_min || other.has_min?
214
200
  @min = other.min unless @has_min
215
201
  has_small = true
216
202
  else
@@ -220,8 +206,8 @@ module NHKore
220
206
  @min = is_from ? 0 : 59
221
207
  end
222
208
  end
223
-
224
- if @has_hour || other.has_hour?()
209
+
210
+ if @has_hour || other.has_hour?
225
211
  @hour = other.hour unless @has_hour
226
212
  has_small = true
227
213
  else
@@ -231,8 +217,8 @@ module NHKore
231
217
  @hour = is_from ? 0 : 23
232
218
  end
233
219
  end
234
-
235
- if @has_day || other.has_day?()
220
+
221
+ if @has_day || other.has_day?
236
222
  @day = other.day unless @has_day
237
223
  has_small = true
238
224
  else
@@ -242,8 +228,8 @@ module NHKore
242
228
  @day = is_from ? 1 : :last_day
243
229
  end
244
230
  end
245
-
246
- if @has_month || other.has_month?()
231
+
232
+ if @has_month || other.has_month?
247
233
  @month = other.month unless @has_month
248
234
  has_small = true
249
235
  else
@@ -253,10 +239,10 @@ module NHKore
253
239
  @month = is_from ? 1 : 12
254
240
  end
255
241
  end
256
-
257
- if @has_year || other.has_year?()
242
+
243
+ if @has_year || other.has_year?
258
244
  @year = other.year unless @has_year
259
- has_small = true
245
+ has_small = true # rubocop:disable Lint/UselessAssignment
260
246
  else
261
247
  if has_small
262
248
  @year = jst_now.year
@@ -264,49 +250,49 @@ module NHKore
264
250
  @year = is_from ? Util::MIN_SANE_YEAR : jst_now.year
265
251
  end
266
252
  end
267
-
253
+
268
254
  # Must be after setting @year & @month.
269
255
  if @day == :last_day
270
256
  @day = Date.new(@year,@month,-1).day
271
257
  end
272
-
258
+
273
259
  return self
274
260
  end
275
-
276
- def max!()
261
+
262
+ def max!
277
263
  @min_or_max = true
278
-
264
+
279
265
  # Ex: 2020-12-31 23:59:59
280
266
  return set!(Util::JST_YEAR,12,31,23,59,59)
281
267
  end
282
-
283
- def min!()
268
+
269
+ def min!
284
270
  @min_or_max = true
285
-
271
+
286
272
  # Ex: 1924-01-01 00:00:00
287
273
  return set!(Util::MIN_SANE_YEAR,1,1,0,0,0)
288
274
  end
289
-
275
+
290
276
  def parse!(value,fmt)
291
277
  value = Time.strptime(value,fmt,&self.class.method(:guess_year))
292
-
278
+
293
279
  @has_day = fmt.include?('%d')
294
280
  @has_hour = fmt.include?('%H')
295
281
  @has_min = fmt.include?('%M')
296
282
  @has_month = fmt.include?('%m')
297
283
  @has_sec = fmt.include?('%S')
298
284
  @has_year = fmt.include?('%Y')
299
-
285
+
300
286
  @day = value.day if @has_day
301
287
  @hour = value.hour if @has_hour
302
288
  @min = value.min if @has_min
303
289
  @month = value.month if @has_month
304
290
  @sec = value.sec if @has_sec
305
291
  @year = value.year if @has_year
306
-
292
+
307
293
  return self
308
294
  end
309
-
295
+
310
296
  def set!(year=nil,month=nil,day=nil,hour=nil,min=nil,sec=nil)
311
297
  @year = year
312
298
  @month = month
@@ -314,10 +300,10 @@ module NHKore
314
300
  @hour = hour
315
301
  @min = min
316
302
  @sec = sec
317
-
303
+
318
304
  return self
319
305
  end
320
-
306
+
321
307
  def has=(value)
322
308
  @has_day = value
323
309
  @has_hour = value
@@ -325,19 +311,17 @@ module NHKore
325
311
  @has_month = value
326
312
  @has_sec = value
327
313
  @has_year = value
328
-
329
- return self
330
314
  end
331
-
332
- def jst_time()
333
- return Util.jst_time(time())
315
+
316
+ def jst_time
317
+ return Util.jst_time(time)
334
318
  end
335
-
336
- def time()
319
+
320
+ def time
337
321
  return Time.new(@year,@month,@day,@hour,@min,@sec)
338
322
  end
339
-
340
- def to_s()
323
+
324
+ def to_s
341
325
  return "#{@year}-#{@month}-#{@day} #{@hour}:#{@min}:#{@sec}"
342
326
  end
343
327
  end
data/lib/nhkore/defn.rb CHANGED
@@ -1,23 +1,11 @@
1
- #!/usr/bin/env ruby
2
1
  # encoding: UTF-8
3
2
  # frozen_string_literal: true
4
3
 
5
4
  #--
6
5
  # This file is part of NHKore.
7
- # Copyright (c) 2020 Jonathan Bradley Whited (@esotericpig)
8
- #
9
- # NHKore is free software: you can redistribute it and/or modify
10
- # it under the terms of the GNU Lesser General Public License as published by
11
- # the Free Software Foundation, either version 3 of the License, or
12
- # (at your option) any later version.
13
- #
14
- # NHKore is distributed in the hope that it will be useful,
15
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
- # GNU Lesser General Public License for more details.
18
- #
19
- # You should have received a copy of the GNU Lesser General Public License
20
- # along with NHKore. If not, see <https://www.gnu.org/licenses/>.
6
+ # Copyright (c) 2020-2021 Jonathan Bradley Whited
7
+ #
8
+ # SPDX-License-Identifier: LGPL-3.0-or-later
21
9
  #++
22
10
 
23
11
 
@@ -29,75 +17,80 @@ require 'nhkore/word'
29
17
 
30
18
  module NHKore
31
19
  ###
32
- # @author Jonathan Bradley Whited (@esotericpig)
20
+ # @author Jonathan Bradley Whited
33
21
  # @since 0.2.0
34
22
  ###
35
23
  class Defn
36
24
  attr_reader :hyoukis
37
25
  attr_accessor :text
38
26
  attr_reader :words
39
-
40
- def initialize()
27
+
28
+ def initialize
41
29
  super()
42
-
30
+
43
31
  @hyoukis = []
44
- @text = ''.dup()
32
+ @text = ''.dup
45
33
  @words = []
46
34
  end
47
-
35
+
48
36
  # If no data, don't raise errors; don't care if have a definition or not.
49
37
  def self.scrape(hash,missingno: nil,url: nil)
50
- defn = Defn.new()
51
-
38
+ defn = Defn.new
39
+
52
40
  hyoukis = hash['hyouki']
53
-
54
- if !hyoukis.nil?()
55
- hyoukis.each() do |hyouki|
56
- next if hyouki.nil?()
57
- next if (hyouki = Util.strip_web_str(hyouki)).empty?()
58
-
59
- defn.hyoukis << hyouki
60
- end
41
+
42
+ hyoukis&.each() do |hyouki|
43
+ next if hyouki.nil?
44
+ next if (hyouki = Util.strip_web_str(hyouki)).empty?
45
+
46
+ defn.hyoukis << hyouki
61
47
  end
62
-
48
+
63
49
  def_str = hash['def']
64
-
50
+
65
51
  if Util.empty_web_str?(def_str)
66
- return defn.hyoukis.empty?() ? nil : defn
52
+ return defn.hyoukis.empty? ? nil : defn
67
53
  end
68
-
54
+
69
55
  doc = Nokogiri::HTML(def_str)
70
- doc = doc.css('body') # Auto-added by Nokogiri
71
-
72
- doc.children.each() do |child|
73
- name = Util.unspace_web_str(child.name).downcase() if child.respond_to?(:name)
74
-
56
+ doc = doc.css('body') # Auto-added by Nokogiri.
57
+
58
+ doc.children.each do |child|
59
+ name = Util.unspace_web_str(child.name).downcase if child.respond_to?(:name)
60
+
75
61
  is_text = false
76
- word = nil
77
-
62
+ words = []
63
+
78
64
  if name == 'ruby'
79
- word = Word.scrape_ruby_tag(child,missingno: missingno,url: url)
80
- elsif child.respond_to?(:text) # Don't do child.text?(), just want content
81
- word = Word.scrape_text_node(child,url: url)
65
+ # Returns an array.
66
+ words = Word.scrape_ruby_tag(child,missingno: missingno,url: url)
67
+ elsif child.respond_to?(:text) # Don't do child.text?(), just want content.
68
+ words << Word.scrape_text_node(child,url: url)
82
69
  is_text = true
83
70
  end
84
-
85
- if word.nil?()
71
+
72
+ # All word-scraping methods can return nil,
73
+ # so remove all nils for empty?() check.
74
+ words = words&.compact
75
+
76
+ if words.nil? || words.empty?
86
77
  defn.text << Util.reduce_jpn_space(child.text) if is_text
87
78
  else
88
- defn.text << Util.reduce_jpn_space(word.word)
89
- defn.words << word unless Util.empty_web_str?(word.word)
79
+ words.each do |word|
80
+ defn.text << Util.reduce_jpn_space(word.word)
81
+ defn.words << word unless Util.empty_web_str?(word.word)
82
+ end
90
83
  end
91
84
  end
92
-
93
- return nil if defn.hyoukis.empty?() && defn.words.empty?()
94
-
85
+
86
+ return nil if defn.hyoukis.empty? && defn.words.empty?
87
+
95
88
  defn.text = Util.strip_web_str(defn.text)
96
-
89
+
97
90
  return defn
98
91
  end
99
-
100
- def to_s()
92
+
93
+ def to_s
101
94
  return @text
102
95
  end
103
96
  end