nhkore 0.3.4 → 0.3.9

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,10 +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
25
+ extend AttrBool::Ext
26
+
37
27
  # Order matters!
38
28
  FMTS = [
39
29
  '%Y-%m-%d %H:%M',
@@ -52,13 +42,13 @@ module NHKore
52
42
  '%H:%M',
53
43
  '%d',
54
44
  '%Y',
55
- ]
56
-
45
+ ].freeze
46
+
57
47
  def self.guess_year(year)
58
48
  if year < 1000
59
49
  century = Util::JST_YEAR / 100 * 100 # 2120 -> 2100
60
50
  millennium = Util::JST_YEAR / 1000 * 1000 # 2120 -> 2000
61
-
51
+
62
52
  # If year <= 23 (2022 -> 23)...
63
53
  if year <= ((Util::JST_YEAR % 100) + 1)
64
54
  # Assume this century.
@@ -69,34 +59,34 @@ module NHKore
69
59
  # Assume this millennium.
70
60
  # So if the current year is 2200, and year is 150,
71
61
  # then it will be 2000 + 150 = 2150.
72
- year = millennium + year
73
62
  else
74
63
  # Assume previous millennium (2000 -> 1000),
75
64
  # so year 999 will become 1999.
76
65
  millennium -= 1000 if millennium >= 1000
77
- year = millennium + year
78
66
  end
67
+
68
+ year = millennium + year
79
69
  else
80
70
  # Assume previous century (2000 -> 1900).
81
71
  century -= 100 if century >= 100
82
72
  year = century + year
83
73
  end
84
74
  end
85
-
75
+
86
76
  return year
87
77
  end
88
-
78
+
89
79
  def self.parse_range(value)
90
80
  # Do not use unspace_web_str(), want spaces for formats.
91
81
  value = Util.strip_web_str(Util.reduce_space(value))
92
82
  values = value.split('...',2)
93
-
94
- return nil if values.empty?() # For '' or '...'
95
-
83
+
84
+ return nil if values.empty? # For '' or '...'
85
+
96
86
  # For '2020...' or '...2020'.
97
87
  if value.include?('...')
98
88
  # values.length is always 2 because of 2 in split() above.
99
-
89
+
100
90
  # For '2020...'.
101
91
  if Util.empty_web_str?(values[1])
102
92
  values[1] = :infinity
@@ -105,81 +95,79 @@ module NHKore
105
95
  values[0] = :infinity
106
96
  end
107
97
  end
108
-
98
+
109
99
  datetimes = [
110
- DatetimeParser.new(), # "From" date time
111
- DatetimeParser.new(), # "To" date time
100
+ DatetimeParser.new, # "From" date time
101
+ DatetimeParser.new, # "To" date time
112
102
  ]
113
-
114
- values.each_with_index() do |v,i|
103
+
104
+ values.each_with_index do |v,i|
115
105
  dt = datetimes[i]
116
-
106
+
117
107
  # Minimum/Maximum date time for '2020...' or '...2020'.
118
108
  if v == :infinity
119
109
  # "From" date time.
120
110
  if i == 0
121
- dt.min!()
111
+ dt.min!
122
112
  # "To" date time.
123
113
  else
124
- dt.max!()
114
+ dt.max!
125
115
  end
126
116
  else
127
117
  v = Util.strip_web_str(v)
128
-
129
- FMTS.each_with_index() do |fmt,i|
130
- begin
131
- # If don't do this, "%d" values will be parsed using "%d %H".
132
- # It seems as though strptime() ignores space.
133
- raise ArgumentError if fmt.include?(' ') && !v.include?(' ')
134
-
135
- # If don't do this, "%y..." values will be parsed using "%d...".
136
- raise ArgumentError if fmt.start_with?('%d') && v.split(' ')[0].length > 2
137
-
138
- dt.parse!(v,fmt)
139
-
140
- break # No problem; this format worked
141
- rescue ArgumentError
142
- # Out of formats.
143
- raise if i >= (FMTS.length - 1)
144
- 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)
145
133
  end
146
134
  end
147
135
  end
148
-
136
+
149
137
  from = datetimes[0]
150
138
  to = datetimes[1]
151
-
139
+
152
140
  from.autofill!(:from,to)
153
141
  to.autofill!(:to,from)
154
-
155
- return [from.jst_time(),to.jst_time()]
142
+
143
+ return [from.jst_time,to.jst_time]
156
144
  end
157
-
145
+
158
146
  attr_accessor :day
159
147
  attr_accessor :hour
160
148
  attr_accessor :min
161
149
  attr_accessor :month
162
150
  attr_accessor :sec
163
151
  attr_accessor :year
164
-
152
+
165
153
  attr_accessor? :has_day
166
154
  attr_accessor? :has_hour
167
155
  attr_accessor? :has_min
168
156
  attr_accessor? :has_month
169
157
  attr_accessor? :has_sec
170
158
  attr_accessor? :has_year
171
-
159
+
172
160
  attr_reader? :min_or_max
173
-
161
+
174
162
  def initialize(year=nil,month=nil,day=nil,hour=nil,min=nil,sec=nil)
175
163
  super()
176
-
164
+
177
165
  set!(year,month,day,hour,min,sec)
178
-
166
+
179
167
  self.has = false
180
168
  @min_or_max = false
181
169
  end
182
-
170
+
183
171
  def autofill!(type,other)
184
172
  case type
185
173
  when :from
@@ -189,15 +177,15 @@ module NHKore
189
177
  else
190
178
  raise ArgumentError,"invalid type[#{type}]"
191
179
  end
192
-
180
+
193
181
  return self if @min_or_max
194
-
182
+
195
183
  has_small = false
196
184
  jst_now = Util.jst_now()
197
-
185
+
198
186
  # Must be from smallest to biggest.
199
-
200
- if @has_sec || other.has_sec?()
187
+
188
+ if @has_sec || other.has_sec?
201
189
  @sec = other.sec unless @has_sec
202
190
  has_small = true
203
191
  else
@@ -207,8 +195,8 @@ module NHKore
207
195
  @sec = is_from ? 0 : 59
208
196
  end
209
197
  end
210
-
211
- if @has_min || other.has_min?()
198
+
199
+ if @has_min || other.has_min?
212
200
  @min = other.min unless @has_min
213
201
  has_small = true
214
202
  else
@@ -218,8 +206,8 @@ module NHKore
218
206
  @min = is_from ? 0 : 59
219
207
  end
220
208
  end
221
-
222
- if @has_hour || other.has_hour?()
209
+
210
+ if @has_hour || other.has_hour?
223
211
  @hour = other.hour unless @has_hour
224
212
  has_small = true
225
213
  else
@@ -229,8 +217,8 @@ module NHKore
229
217
  @hour = is_from ? 0 : 23
230
218
  end
231
219
  end
232
-
233
- if @has_day || other.has_day?()
220
+
221
+ if @has_day || other.has_day?
234
222
  @day = other.day unless @has_day
235
223
  has_small = true
236
224
  else
@@ -240,8 +228,8 @@ module NHKore
240
228
  @day = is_from ? 1 : :last_day
241
229
  end
242
230
  end
243
-
244
- if @has_month || other.has_month?()
231
+
232
+ if @has_month || other.has_month?
245
233
  @month = other.month unless @has_month
246
234
  has_small = true
247
235
  else
@@ -251,10 +239,10 @@ module NHKore
251
239
  @month = is_from ? 1 : 12
252
240
  end
253
241
  end
254
-
255
- if @has_year || other.has_year?()
242
+
243
+ if @has_year || other.has_year?
256
244
  @year = other.year unless @has_year
257
- has_small = true
245
+ has_small = true # rubocop:disable Lint/UselessAssignment
258
246
  else
259
247
  if has_small
260
248
  @year = jst_now.year
@@ -262,49 +250,49 @@ module NHKore
262
250
  @year = is_from ? Util::MIN_SANE_YEAR : jst_now.year
263
251
  end
264
252
  end
265
-
253
+
266
254
  # Must be after setting @year & @month.
267
255
  if @day == :last_day
268
256
  @day = Date.new(@year,@month,-1).day
269
257
  end
270
-
258
+
271
259
  return self
272
260
  end
273
-
274
- def max!()
261
+
262
+ def max!
275
263
  @min_or_max = true
276
-
264
+
277
265
  # Ex: 2020-12-31 23:59:59
278
266
  return set!(Util::JST_YEAR,12,31,23,59,59)
279
267
  end
280
-
281
- def min!()
268
+
269
+ def min!
282
270
  @min_or_max = true
283
-
271
+
284
272
  # Ex: 1924-01-01 00:00:00
285
273
  return set!(Util::MIN_SANE_YEAR,1,1,0,0,0)
286
274
  end
287
-
275
+
288
276
  def parse!(value,fmt)
289
277
  value = Time.strptime(value,fmt,&self.class.method(:guess_year))
290
-
278
+
291
279
  @has_day = fmt.include?('%d')
292
280
  @has_hour = fmt.include?('%H')
293
281
  @has_min = fmt.include?('%M')
294
282
  @has_month = fmt.include?('%m')
295
283
  @has_sec = fmt.include?('%S')
296
284
  @has_year = fmt.include?('%Y')
297
-
285
+
298
286
  @day = value.day if @has_day
299
287
  @hour = value.hour if @has_hour
300
288
  @min = value.min if @has_min
301
289
  @month = value.month if @has_month
302
290
  @sec = value.sec if @has_sec
303
291
  @year = value.year if @has_year
304
-
292
+
305
293
  return self
306
294
  end
307
-
295
+
308
296
  def set!(year=nil,month=nil,day=nil,hour=nil,min=nil,sec=nil)
309
297
  @year = year
310
298
  @month = month
@@ -312,10 +300,10 @@ module NHKore
312
300
  @hour = hour
313
301
  @min = min
314
302
  @sec = sec
315
-
303
+
316
304
  return self
317
305
  end
318
-
306
+
319
307
  def has=(value)
320
308
  @has_day = value
321
309
  @has_hour = value
@@ -323,19 +311,17 @@ module NHKore
323
311
  @has_month = value
324
312
  @has_sec = value
325
313
  @has_year = value
326
-
327
- return self
328
314
  end
329
-
330
- def jst_time()
331
- return Util.jst_time(time())
315
+
316
+ def jst_time
317
+ return Util.jst_time(time)
332
318
  end
333
-
334
- def time()
319
+
320
+ def time
335
321
  return Time.new(@year,@month,@day,@hour,@min,@sec)
336
322
  end
337
-
338
- def to_s()
323
+
324
+ def to_s
339
325
  return "#{@year}-#{@month}-#{@day} #{@hour}:#{@min}:#{@sec}"
340
326
  end
341
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