asker-tool 2.1.5 → 2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -17
- data/bin/asker +2 -1
- data/lib/asker/ai/ai.rb +10 -3
- data/lib/asker/ai/ai_calculate.rb +20 -6
- data/lib/asker/ai/code/base_code_ai.rb +104 -0
- data/lib/asker/{code/ai → ai/code}/code_ai_factory.rb +11 -1
- data/lib/asker/{code/ai → ai/code}/javascript_code_ai.rb +2 -5
- data/lib/asker/ai/code/problem_code_ai.rb +176 -0
- data/lib/asker/{code/ai → ai/code}/python_code_ai.rb +2 -5
- data/lib/asker/{code/ai → ai/code}/ruby_code_ai.rb +14 -7
- data/lib/asker/{code/ai → ai/code}/sql_code_ai.rb +2 -5
- data/lib/asker/ai/concept_ai.rb +13 -3
- data/lib/asker/ai/question.rb +28 -6
- data/lib/asker/ai/stages/base_stage.rb +45 -6
- data/lib/asker/ai/stages/stage_b.rb +100 -50
- data/lib/asker/ai/stages/stage_d.rb +75 -90
- data/lib/asker/ai/stages/stage_f.rb +64 -36
- data/lib/asker/ai/stages/stage_i.rb +79 -92
- data/lib/asker/ai/stages/stage_s.rb +41 -36
- data/lib/asker/ai/stages/stage_t.rb +149 -108
- data/lib/asker/application.rb +19 -11
- data/lib/asker/cli.rb +31 -44
- data/lib/asker/data/code.rb +62 -0
- data/lib/asker/data/column.rb +31 -21
- data/lib/asker/data/concept.rb +109 -65
- data/lib/asker/data/data_field.rb +14 -0
- data/lib/asker/data/project_data.rb +63 -0
- data/lib/asker/data/row.rb +75 -52
- data/lib/asker/data/table.rb +91 -42
- data/lib/asker/data/template.rb +3 -1
- data/lib/asker/data/world.rb +51 -35
- data/lib/asker/displayer/code_displayer.rb +7 -0
- data/lib/asker/displayer/concept_ai_displayer.erb +10 -0
- data/lib/asker/displayer/concept_ai_displayer.rb +74 -53
- data/lib/asker/displayer/concept_displayer.rb +16 -9
- data/lib/asker/displayer/stats_displayer.rb +12 -3
- data/lib/asker/exporter/concept_ai_gift_exporter.rb +19 -11
- data/lib/asker/exporter/concept_ai_moodle_exporter.rb +44 -0
- data/lib/asker/exporter/concept_ai_yaml_exporter.rb +13 -6
- data/lib/asker/exporter/concept_doc_exporter.rb +14 -1
- data/lib/asker/exporter/data_gift_exporter.rb +29 -0
- data/lib/asker/exporter/output_file_exporter.rb +9 -6
- data/lib/asker/files/{config.ini → asker.ini} +48 -1
- data/lib/asker/files/example-concept.haml +0 -1
- data/lib/asker/files/language/du/connectors.yaml +81 -0
- data/lib/asker/{lang/locales/fr → files/language/du}/mistakes.yaml +1 -1
- data/lib/asker/files/language/du/templates.yaml +29 -0
- data/lib/asker/{lang/locales → files/language}/en/connectors.yaml +0 -0
- data/lib/asker/{lang/locales → files/language}/en/mistakes.yaml +0 -0
- data/lib/asker/files/language/en/templates.yaml +29 -0
- data/lib/asker/{lang/locales → files/language}/es/connectors.yaml +0 -0
- data/lib/asker/files/language/es/mistakes.yaml +84 -0
- data/lib/asker/files/language/es/templates.yaml +29 -0
- data/lib/asker/files/language/fr/connectors.yaml +76 -0
- data/lib/asker/{lang/locales/es → files/language/fr}/mistakes.yaml +0 -0
- data/lib/asker/files/language/fr/templates.yaml +29 -0
- data/lib/asker/{lang/locales → files/language}/javascript/connectors.yaml +0 -0
- data/lib/asker/{lang/locales → files/language}/javascript/mistakes.yaml +0 -0
- data/lib/asker/{lang/locales → files/language}/javascript/templates.yaml +0 -0
- data/lib/asker/{lang/locales → files/language}/math/connectors.yaml +0 -0
- data/lib/asker/{lang/locales → files/language}/math/mistakes.yaml +0 -0
- data/lib/asker/{lang/locales → files/language}/math/templates.yaml +0 -0
- data/lib/asker/{lang/locales → files/language}/python/connectors.yaml +0 -0
- data/lib/asker/{lang/locales → files/language}/python/mistakes.yaml +0 -0
- data/lib/asker/{lang/locales → files/language}/python/templates.yaml +0 -0
- data/lib/asker/{lang/locales → files/language}/ruby/connectors.yaml +0 -0
- data/lib/asker/{lang/locales → files/language}/ruby/mistakes.yaml +0 -0
- data/lib/asker/{lang/locales → files/language}/ruby/templates.yaml +0 -0
- data/lib/asker/{lang/locales → files/language}/sql/connectors.yaml +0 -0
- data/lib/asker/{lang/locales → files/language}/sql/mistakes.yaml +0 -0
- data/lib/asker/{lang/locales → files/language}/sql/templates.yaml +0 -0
- data/lib/asker/formatter/concept_doc_formatter.rb +0 -4
- data/lib/asker/formatter/concept_string_formatter.rb +7 -4
- data/lib/asker/formatter/moodle/matching.erb +38 -0
- data/lib/asker/formatter/moodle/multichoice.erb +49 -0
- data/lib/asker/formatter/moodle/shortanswer.erb +30 -0
- data/lib/asker/formatter/moodle/truefalse.erb +47 -0
- data/lib/asker/formatter/question_gift_formatter.rb +30 -20
- data/lib/asker/formatter/question_moodle_formatter.rb +27 -0
- data/lib/asker/{checker.rb → input_checker.rb} +126 -49
- data/lib/asker/lang/lang.rb +17 -10
- data/lib/asker/lang/lang_factory.rb +32 -5
- data/lib/asker/lang/text_actions.rb +87 -69
- data/lib/asker/loader/code_loader.rb +4 -4
- data/lib/asker/loader/content_loader.rb +21 -19
- data/lib/asker/loader/directory_loader.rb +1 -8
- data/lib/asker/loader/embedded_file.rb +42 -0
- data/lib/asker/loader/file_loader.rb +3 -14
- data/lib/asker/loader/haml_loader.rb +15 -0
- data/lib/asker/loader/image_url_loader.rb +8 -12
- data/lib/asker/loader/input_loader.rb +13 -15
- data/lib/asker/loader/project_loader.rb +25 -15
- data/lib/asker/logger.rb +36 -9
- data/lib/asker/skeleton.rb +23 -13
- data/lib/asker.rb +76 -42
- metadata +53 -54
- data/lib/asker/code/ai/base_code_ai.rb +0 -48
- data/lib/asker/code/code.rb +0 -53
- data/lib/asker/lang/locales/du/templates.yaml +0 -50
- data/lib/asker/lang/locales/en/templates.yaml +0 -29
- data/lib/asker/lang/locales/es/templates.yaml +0 -29
- data/lib/asker/lang/locales/fr/connectors.yaml +0 -92
- data/lib/asker/lang/locales/fr/templates.yaml +0 -29
- data/lib/asker/project.rb +0 -172
@@ -1,40 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
3
|
require 'rainbow'
|
3
4
|
|
4
5
|
##
|
5
6
|
# Check HAML file syntax
|
6
|
-
module
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
7
|
+
module InputChecker
|
8
|
+
|
9
|
+
def self.check(filepath, verbose = true)
|
10
|
+
exist = check_file_exist(filepath, verbose)
|
11
|
+
return false unless exist
|
12
|
+
check_file_content(filepath, verbose)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.check_file_exist(filepath, verbose = true)
|
14
16
|
unless File.exist? filepath
|
15
|
-
puts Rainbow(
|
17
|
+
puts Rainbow('File not found!').red.bright if verbose
|
16
18
|
return false
|
17
19
|
end
|
18
20
|
unless File.extname(filepath) == '.haml'
|
19
|
-
puts Rainbow(
|
21
|
+
puts Rainbow('Only check HAML files!').yellow.bright if verbose
|
20
22
|
return false
|
21
23
|
end
|
22
|
-
|
24
|
+
true
|
23
25
|
end
|
24
26
|
|
25
|
-
|
26
|
-
# Check HAML syntax
|
27
|
-
# @param filepath (String)
|
28
|
-
def self.check_filepath(filepath)
|
27
|
+
def self.check_file_content(filepath, verbose = true)
|
29
28
|
data = Data.new(filepath)
|
30
29
|
data.check
|
31
|
-
data.show_errors
|
32
|
-
data.
|
30
|
+
data.show_errors if verbose
|
31
|
+
data.ok?
|
33
32
|
end
|
34
33
|
|
34
|
+
# rubocop:disable Metrics/ClassLength
|
35
35
|
class Data
|
36
|
-
attr_reader :inputs
|
37
|
-
|
36
|
+
attr_reader :inputs, :outputs
|
37
|
+
|
38
|
+
# rubocop:disable Metrics/MethodLength
|
38
39
|
def initialize(filepath)
|
39
40
|
@inputs = File.read(filepath).split("\n")
|
40
41
|
@outputs = []
|
@@ -49,8 +50,9 @@ module Checker
|
|
49
50
|
end
|
50
51
|
@ok = false
|
51
52
|
end
|
53
|
+
# rubocop:enable Metrics/MethodLength
|
52
54
|
|
53
|
-
def
|
55
|
+
def ok?
|
54
56
|
@ok
|
55
57
|
end
|
56
58
|
|
@@ -62,16 +64,31 @@ module Checker
|
|
62
64
|
|
63
65
|
def show_errors
|
64
66
|
errors = 0
|
67
|
+
# puts "Line : Error description"
|
65
68
|
@outputs.each do |i|
|
66
69
|
next if i[:state] == :ok
|
70
|
+
|
67
71
|
errors += 1
|
68
|
-
|
69
|
-
|
72
|
+
if errors < 11
|
73
|
+
data = { id: i[:id], msg: i[:msg], source: i[:source][0, 40] }
|
74
|
+
order = i[:id] + 1
|
75
|
+
data = { order: order, msg: i[:msg], source: i[:source][0, 40] }
|
76
|
+
puts format(' %<order>03d : %<msg>s. => %<source>s', data)
|
77
|
+
end
|
78
|
+
puts '...' if errors == 11
|
79
|
+
end
|
80
|
+
|
81
|
+
if errors.positive?
|
82
|
+
puts Rainbow("[ERROR] #{errors} errors " \
|
83
|
+
"from #{@inputs.size} lines!").red.bright
|
70
84
|
end
|
71
|
-
puts Rainbow(
|
72
|
-
puts Rainbow("Syntax OK!").green if errors == 0
|
85
|
+
puts Rainbow('Syntax OK!').green if errors.zero?
|
73
86
|
end
|
87
|
+
# rubocop:enable Metrics/AbcSize
|
88
|
+
# rubocop:enable Metrics/MethodLength
|
74
89
|
|
90
|
+
# rubocop:disable Metrics/MethodLength
|
91
|
+
# rubocop:disable Metrics/AbcSize
|
75
92
|
def check
|
76
93
|
@ok = true
|
77
94
|
@inputs.each_with_index do |line, index|
|
@@ -95,19 +112,23 @@ module Checker
|
|
95
112
|
end
|
96
113
|
@ok
|
97
114
|
end
|
115
|
+
# rubocop:enable Metrics/MethodLength
|
116
|
+
# rubocop:enable Metrics/AbcSize
|
98
117
|
|
99
118
|
private
|
100
119
|
|
101
120
|
def check_empty_lines(line, index)
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
121
|
+
return unless line.strip.size.zero? || line.start_with?('#')
|
122
|
+
|
123
|
+
@outputs[index][:type] = :empty
|
124
|
+
@outputs[index][:level] = -1
|
125
|
+
@outputs[index][:state] = :ok
|
107
126
|
end
|
108
127
|
|
128
|
+
# rubocop:disable Metrics/MethodLength
|
129
|
+
# rubocop:disable Metrics/AbcSize
|
109
130
|
def check_map(line, index)
|
110
|
-
if index
|
131
|
+
if index.zero?
|
111
132
|
@outputs[index][:type] = :map
|
112
133
|
if line.start_with?('%map{')
|
113
134
|
@outputs[index][:state] = :ok
|
@@ -115,13 +136,17 @@ module Checker
|
|
115
136
|
@outputs[index][:state] = :err
|
116
137
|
@outputs[index][:msg] = 'Start with %map{'
|
117
138
|
end
|
118
|
-
elsif index
|
139
|
+
elsif index.positive? && line.include?('%map{')
|
119
140
|
@outputs[index][:state] = :err
|
120
141
|
@outputs[index][:type] = :map
|
121
142
|
@outputs[index][:msg] = 'Write %map on line 0'
|
122
143
|
end
|
123
144
|
end
|
145
|
+
# rubocop:enable Metrics/MethodLength
|
146
|
+
# rubocop:enable Metrics/AbcSize
|
124
147
|
|
148
|
+
# rubocop:disable Metrics/MethodLength
|
149
|
+
# rubocop:disable Metrics/AbcSize
|
125
150
|
def check_concept(line, index)
|
126
151
|
return unless @outputs[index][:state] == :none
|
127
152
|
return unless line.include? '%concept'
|
@@ -137,7 +162,11 @@ module Checker
|
|
137
162
|
@outputs[index][:msg] = 'Write 2 spaces before %concept'
|
138
163
|
end
|
139
164
|
end
|
165
|
+
# rubocop:enable Metrics/MethodLength
|
166
|
+
# rubocop:enable Metrics/AbcSize
|
140
167
|
|
168
|
+
# rubocop:disable Metrics/AbcSize
|
169
|
+
# rubocop:disable Metrics/MethodLength
|
141
170
|
def check_names(line, index)
|
142
171
|
return unless @outputs[index][:state] == :none
|
143
172
|
return unless line.include? '%names'
|
@@ -153,7 +182,11 @@ module Checker
|
|
153
182
|
@outputs[index][:msg] = 'Write 4 spaces before %names'
|
154
183
|
end
|
155
184
|
end
|
185
|
+
# rubocop:enable Metrics/AbcSize
|
186
|
+
# rubocop:enable Metrics/MethodLength
|
156
187
|
|
188
|
+
# rubocop:disable Metrics/AbcSize
|
189
|
+
# rubocop:disable Metrics/MethodLength
|
157
190
|
def check_tags(line, index)
|
158
191
|
return unless @outputs[index][:state] == :none
|
159
192
|
return unless line.include? '%tags'
|
@@ -169,7 +202,11 @@ module Checker
|
|
169
202
|
@outputs[index][:msg] = 'Write 4 spaces before %tags'
|
170
203
|
end
|
171
204
|
end
|
205
|
+
# rubocop:enable Metrics/AbcSize
|
206
|
+
# rubocop:enable Metrics/MethodLength
|
172
207
|
|
208
|
+
# rubocop:disable Metrics/AbcSize
|
209
|
+
# rubocop:disable Metrics/MethodLength
|
173
210
|
def check_def(line, index)
|
174
211
|
return unless @outputs[index][:state] == :none
|
175
212
|
return unless line.include? '%def'
|
@@ -185,7 +222,11 @@ module Checker
|
|
185
222
|
@outputs[index][:msg] = 'Write 4 spaces before %def'
|
186
223
|
end
|
187
224
|
end
|
225
|
+
# rubocop:enable Metrics/AbcSize
|
226
|
+
# rubocop:enable Metrics/MethodLength
|
188
227
|
|
228
|
+
# rubocop:disable Metrics/AbcSize
|
229
|
+
# rubocop:disable Metrics/MethodLength
|
189
230
|
def check_table(line, index)
|
190
231
|
return unless @outputs[index][:state] == :none
|
191
232
|
return unless line.include? '%table'
|
@@ -201,7 +242,11 @@ module Checker
|
|
201
242
|
@outputs[index][:msg] = 'Write 4 spaces before %table'
|
202
243
|
end
|
203
244
|
end
|
245
|
+
# rubocop:enable Metrics/AbcSize
|
246
|
+
# rubocop:enable Metrics/MethodLength
|
204
247
|
|
248
|
+
# rubocop:disable Metrics/AbcSize
|
249
|
+
# rubocop:disable Metrics/MethodLength
|
205
250
|
def check_row(line, index)
|
206
251
|
return unless @outputs[index][:state] == :none
|
207
252
|
return unless line.include? '%row'
|
@@ -209,14 +254,15 @@ module Checker
|
|
209
254
|
@outputs[index][:type] = :row
|
210
255
|
@outputs[index][:state] = :ok
|
211
256
|
|
212
|
-
|
257
|
+
case count_spaces(line)
|
258
|
+
when 6
|
213
259
|
@outputs[index][:level] = 3
|
214
260
|
parent = find_parent(index)
|
215
|
-
unless [
|
261
|
+
unless %i[table features].include? parent
|
216
262
|
@outputs[index][:state] = :err
|
217
263
|
@outputs[index][:msg] = 'Parent(table/features) not found!'
|
218
264
|
end
|
219
|
-
|
265
|
+
when 8
|
220
266
|
@outputs[index][:level] = 4
|
221
267
|
if find_parent(index) != :template
|
222
268
|
@outputs[index][:state] = :err
|
@@ -227,20 +273,25 @@ module Checker
|
|
227
273
|
@outputs[index][:msg] = 'Write 6 or 8 spaces before %row'
|
228
274
|
end
|
229
275
|
end
|
276
|
+
# rubocop:enable Metrics/AbcSize
|
277
|
+
# rubocop:enable Metrics/MethodLength
|
230
278
|
|
279
|
+
# rubocop:disable Metrics/AbcSize
|
280
|
+
# rubocop:disable Metrics/MethodLength
|
231
281
|
def check_col(line, index)
|
232
282
|
return unless @outputs[index][:state] == :none
|
233
283
|
return unless line.include? '%col'
|
234
284
|
|
235
285
|
@outputs[index][:type] = :col
|
236
286
|
@outputs[index][:state] = :ok
|
237
|
-
|
287
|
+
case count_spaces(line)
|
288
|
+
when 8
|
238
289
|
@outputs[index][:level] = 4
|
239
290
|
if find_parent(index) != :row
|
240
291
|
@outputs[index][:state] = :err
|
241
292
|
@outputs[index][:msg] = 'Parent(row) not found!'
|
242
293
|
end
|
243
|
-
|
294
|
+
when 10
|
244
295
|
@outputs[index][:level] = 5
|
245
296
|
if find_parent(index) != :row
|
246
297
|
@outputs[index][:state] = :err
|
@@ -250,8 +301,26 @@ module Checker
|
|
250
301
|
@outputs[index][:state] = :err
|
251
302
|
@outputs[index][:msg] = 'Write 8 or 10 spaces before %col'
|
252
303
|
end
|
304
|
+
check_text(line, index)
|
305
|
+
end
|
306
|
+
# rubocop:enable Metrics/AbcSize
|
307
|
+
# rubocop:enable Metrics/MethodLength
|
308
|
+
|
309
|
+
def check_text(line, index)
|
310
|
+
return unless @outputs[index][:state] == :ok
|
311
|
+
|
312
|
+
ok = ''
|
313
|
+
%w[< >].each do |char|
|
314
|
+
ok = char if line.include? char
|
315
|
+
end
|
316
|
+
return if ok == ''
|
317
|
+
|
318
|
+
@outputs[index][:state] = :err
|
319
|
+
@outputs[index][:msg] = "Char #{ok} not allow!"
|
253
320
|
end
|
254
321
|
|
322
|
+
# rubocop:disable Metrics/MethodLength
|
323
|
+
# rubocop:disable Metrics/AbcSize
|
255
324
|
def check_template(line, index)
|
256
325
|
return unless @outputs[index][:state] == :none
|
257
326
|
return unless line.include? '%template'
|
@@ -267,7 +336,11 @@ module Checker
|
|
267
336
|
@outputs[index][:msg] = 'Write 6 spaces before %template'
|
268
337
|
end
|
269
338
|
end
|
339
|
+
# rubocop:enable Metrics/AbcSize
|
340
|
+
# rubocop:enable Metrics/MethodLength
|
270
341
|
|
342
|
+
# rubocop:disable Metrics/MethodLength
|
343
|
+
# rubocop:disable Metrics/AbcSize
|
271
344
|
def check_code(line, index)
|
272
345
|
return unless @outputs[index][:state] == :none
|
273
346
|
return unless line.include? '%code'
|
@@ -283,7 +356,11 @@ module Checker
|
|
283
356
|
@outputs[index][:msg] = 'Write 2 spaces before %code'
|
284
357
|
end
|
285
358
|
end
|
359
|
+
# rubocop:enable Metrics/MethodLength
|
360
|
+
# rubocop:enable Metrics/AbcSize
|
286
361
|
|
362
|
+
# rubocop:disable Metrics/MethodLength
|
363
|
+
# rubocop:disable Metrics/AbcSize
|
287
364
|
def check_type(line, index)
|
288
365
|
return unless @outputs[index][:state] == :none
|
289
366
|
return unless line.include? '%type'
|
@@ -299,7 +376,11 @@ module Checker
|
|
299
376
|
@outputs[index][:msg] = 'Write 4 spaces before %type'
|
300
377
|
end
|
301
378
|
end
|
379
|
+
# rubocop:enable Metrics/AbcSize
|
380
|
+
# rubocop:enable Metrics/MethodLength
|
302
381
|
|
382
|
+
# rubocop:disable Metrics/MethodLength
|
383
|
+
# rubocop:disable Metrics/AbcSize
|
303
384
|
def check_path(line, index)
|
304
385
|
return unless @outputs[index][:state] == :none
|
305
386
|
return unless line.include? '%path'
|
@@ -315,7 +396,11 @@ module Checker
|
|
315
396
|
@outputs[index][:msg] = 'Write 4 spaces before %type'
|
316
397
|
end
|
317
398
|
end
|
399
|
+
# rubocop:enable Metrics/AbcSize
|
400
|
+
# rubocop:enable Metrics/MethodLength
|
318
401
|
|
402
|
+
# rubocop:disable Metrics/MethodLength
|
403
|
+
# rubocop:disable Metrics/AbcSize
|
319
404
|
def check_features(line, index)
|
320
405
|
return unless @outputs[index][:state] == :none
|
321
406
|
return unless line.include? '%features'
|
@@ -331,6 +416,8 @@ module Checker
|
|
331
416
|
@outputs[index][:msg] = 'Write 4 spaces before %features'
|
332
417
|
end
|
333
418
|
end
|
419
|
+
# rubocop:enable Metrics/AbcSize
|
420
|
+
# rubocop:enable Metrics/MethodLength
|
334
421
|
|
335
422
|
def check_unknown(line, index)
|
336
423
|
return unless @outputs[index][:state] == :none
|
@@ -355,19 +442,9 @@ module Checker
|
|
355
442
|
end
|
356
443
|
|
357
444
|
def count_spaces(line)
|
358
|
-
|
359
|
-
|
360
|
-
return 2 if line.start_with? ' %'
|
361
|
-
return 3 if line.start_with? ' %'
|
362
|
-
return 4 if line.start_with? ' %'
|
363
|
-
return 5 if line.start_with? ' %'
|
364
|
-
return 6 if line.start_with? ' %'
|
365
|
-
return 7 if line.start_with? ' %'
|
366
|
-
return 8 if line.start_with? ' %'
|
367
|
-
return 9 if line.start_with? ' %'
|
368
|
-
return 10 if line.start_with? ' %'
|
369
|
-
|
370
|
-
-1
|
445
|
+
a = line.split('%')
|
446
|
+
a[0].count(' ')
|
371
447
|
end
|
372
448
|
end
|
449
|
+
# rubocop:enable Metrics/ClassLength
|
373
450
|
end
|
data/lib/asker/lang/lang.rb
CHANGED
@@ -23,19 +23,26 @@ class Lang
|
|
23
23
|
private
|
24
24
|
|
25
25
|
def load_files
|
26
|
-
dirbase = File.dirname(__FILE__)
|
27
|
-
|
26
|
+
dirbase = File.join(File.dirname(__FILE__), '..', 'files', 'language')
|
27
|
+
filepath = File.join(dirbase, @code, 'templates.yaml')
|
28
|
+
@templates = load_yaml_file(filepath)
|
29
|
+
filepath = File.join(dirbase, @code, 'connectors.yaml')
|
30
|
+
@connectors = load_yaml_file(filepath)
|
31
|
+
filepath = File.join(dirbase, @code, 'mistakes.yaml')
|
32
|
+
@mistakes = load_yaml_file(filepath)
|
33
|
+
end
|
34
|
+
|
35
|
+
# rubocop:disable Security/YAMLLoad
|
36
|
+
def load_yaml_file(filepath)
|
28
37
|
begin
|
29
|
-
|
38
|
+
content = YAML.load(File.new(filepath))
|
30
39
|
rescue StandardError => e
|
31
|
-
Logger.verboseln
|
32
|
-
|
40
|
+
Logger.verboseln '[ERROR] Lang.initialize():' \
|
41
|
+
" Reading YAML file <#{filepath}>"
|
42
|
+
Logger.verboseln '[ADVISE] Revise apostrophe into string without \ symbol'
|
33
43
|
raise e
|
34
44
|
end
|
35
|
-
|
36
|
-
@connectors = YAML.load(File.new(filename))
|
37
|
-
|
38
|
-
filename = File.join(dirbase, 'locales', @code, 'mistakes.yaml')
|
39
|
-
@mistakes = YAML.load(File.new(filename))
|
45
|
+
content
|
40
46
|
end
|
47
|
+
# rubocop:enable Security/YAMLLoad
|
41
48
|
end
|
@@ -2,18 +2,45 @@
|
|
2
2
|
|
3
3
|
require 'singleton'
|
4
4
|
require_relative 'lang'
|
5
|
-
require_relative '../
|
5
|
+
require_relative '../application'
|
6
6
|
|
7
|
-
|
7
|
+
##
|
8
|
+
# LangFactory singleton class.
|
9
|
+
# * Read all language codes defined into configuration file
|
10
|
+
# * and load every language
|
11
|
+
# Lang objects are reused
|
8
12
|
class LangFactory
|
9
13
|
include Singleton
|
10
14
|
|
15
|
+
##
|
16
|
+
# Read all language codes from configuration file and load every language
|
11
17
|
def initialize
|
18
|
+
@default = Application.instance.config['languages']['default'].downcase
|
12
19
|
@langs = {}
|
13
|
-
|
20
|
+
Application.instance.config['languages'].each_pair do |key, value|
|
21
|
+
code = key.downcase
|
22
|
+
next if code == 'default'
|
23
|
+
|
24
|
+
@langs[code] = Lang.new(code) if value.downcase == 'yes'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
##
|
29
|
+
# Return Lang object associated to code
|
30
|
+
# @param code (String)
|
31
|
+
def get(code)
|
32
|
+
return @langs[code] unless @langs[code].nil?
|
33
|
+
|
34
|
+
puts Rainbow("[ERROR] Unkown Lang code: #{code}").bright
|
35
|
+
puts Rainbow(' => Change input file code lang').bright
|
36
|
+
puts Rainbow(' => Revise configuration from config.ini').bright
|
37
|
+
puts Rainbow(' => Revise template files').bright
|
38
|
+
exit 1
|
14
39
|
end
|
15
40
|
|
16
|
-
|
17
|
-
|
41
|
+
##
|
42
|
+
# Return default Lang object
|
43
|
+
def default
|
44
|
+
get(@default)
|
18
45
|
end
|
19
46
|
end
|
@@ -1,134 +1,152 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
+
##
|
4
|
+
# Set of functions used by Lang class
|
3
5
|
module TextActions
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
6
|
+
##
|
7
|
+
# Return text indicated by lang code...
|
8
|
+
def text_for(option, *input)
|
9
|
+
text1 = input[0]
|
10
|
+
text2 = input[1]
|
11
|
+
text3 = input[2]
|
12
|
+
text4 = input[3]
|
13
|
+
text5 = input[4]
|
14
|
+
text6 = input[5]
|
15
|
+
text7 = input[6]
|
16
|
+
|
17
|
+
# Check if exists option before use it
|
18
|
+
raise "[ERROR] Unkown template #{option}" if @templates[option].nil?
|
19
|
+
|
20
|
+
renderer = ERB.new(@templates[option])
|
21
|
+
renderer.result(binding)
|
18
22
|
end
|
19
23
|
|
20
|
-
|
21
|
-
|
24
|
+
##
|
25
|
+
# Convert input text into output text struct
|
26
|
+
# @param input (String) Input text
|
27
|
+
# @param filter (Boolean) true => apply filter, false => dont filter
|
28
|
+
# @return Array
|
29
|
+
def text_filter_connectors(input, filter)
|
30
|
+
input_lines = input.split('.')
|
22
31
|
output_lines = []
|
23
32
|
output_words = []
|
24
33
|
input_lines.each_with_index do |line, rowindex|
|
25
|
-
row=[]
|
34
|
+
row = []
|
26
35
|
line.split(' ').each_with_index do |word, colindex|
|
27
36
|
flag = @connectors.include? word.downcase
|
28
37
|
|
29
38
|
# if <word> is a conector and <pFilter>==true Then Choose this <word>
|
30
39
|
# if <word> isn't a conector and <pFilter>==true and <word>.length>1 Then Choose this <word>
|
31
|
-
if (flag and
|
32
|
-
output_words << {:word => word,
|
33
|
-
:row => rowindex,
|
34
|
-
:col => colindex }
|
40
|
+
if (flag and filter) || (!flag and !filter and word.length > 1)
|
41
|
+
output_words << {:word => word, :row => rowindex, :col => colindex }
|
35
42
|
row << (output_words.size-1)
|
36
43
|
else
|
37
44
|
row << word
|
38
|
-
|
45
|
+
end
|
39
46
|
end
|
40
47
|
row << '.'
|
41
48
|
output_lines << row
|
42
|
-
|
49
|
+
end
|
43
50
|
|
44
51
|
indexes = []
|
45
|
-
exclude = ['[', ']', '(', ')', "
|
52
|
+
exclude = ['[', ']', '(', ')', '"']
|
46
53
|
output_words.each_with_index do |item, index|
|
47
54
|
flag = true
|
48
|
-
exclude.each { |e| flag = false if
|
55
|
+
exclude.each { |e| flag = false if item[:word].include? e }
|
49
56
|
indexes << index if flag
|
50
57
|
end
|
51
58
|
|
52
|
-
|
53
|
-
return result
|
59
|
+
{ lines: output_lines, words: output_words, indexes: indexes }
|
54
60
|
end
|
55
61
|
|
62
|
+
##
|
63
|
+
# Return text with connectors
|
56
64
|
def text_with_connectors(text)
|
57
65
|
text_filter_connectors(text, false)
|
58
66
|
end
|
59
67
|
|
68
|
+
##
|
69
|
+
# Return text without connectors
|
60
70
|
def text_without_connectors(text)
|
61
|
-
|
71
|
+
text_filter_connectors(text, true)
|
62
72
|
end
|
63
73
|
|
64
|
-
def build_text_from_filtered(
|
65
|
-
lines
|
66
|
-
|
67
|
-
counter
|
68
|
-
|
74
|
+
def build_text_from_filtered(input_struct, input_indexes)
|
75
|
+
lines = input_struct[:lines]
|
76
|
+
indexes = input_indexes.sort
|
77
|
+
counter = 1
|
78
|
+
text = ''
|
69
79
|
|
70
80
|
lines.each do |line|
|
71
81
|
line.each do |value|
|
72
82
|
if value.class == String
|
73
|
-
|
83
|
+
text += (' ' + value)
|
74
84
|
elsif value == value.to_i
|
75
85
|
# INFO: ruby 2.4 unifies Fixnum and Bignum into Integer
|
76
86
|
# Avoid using deprecated classes.
|
77
|
-
if
|
78
|
-
|
87
|
+
if indexes.include? value
|
88
|
+
text += " [#{counter}]"
|
79
89
|
counter += 1
|
80
90
|
else
|
81
|
-
|
82
|
-
|
91
|
+
word = input_struct[:words][value][:word]
|
92
|
+
text += (' ' + word)
|
83
93
|
end
|
84
94
|
end
|
85
95
|
end
|
86
96
|
end
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
97
|
+
text.gsub!(' .', '.')
|
98
|
+
text.gsub!(' ,', ',')
|
99
|
+
text = text[1, text.size] if text[0] == ' '
|
100
|
+
text
|
91
101
|
end
|
92
102
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
t.
|
101
|
-
t.gsub!("
|
102
|
-
t.gsub!(
|
103
|
-
t.gsub!("
|
104
|
-
t.
|
103
|
+
##
|
104
|
+
# Count words
|
105
|
+
# @param input (String)
|
106
|
+
# @return Integer
|
107
|
+
def count_words(input)
|
108
|
+
return 0 if input.nil?
|
109
|
+
|
110
|
+
t = input.clone
|
111
|
+
t.gsub!("\n", ' ')
|
112
|
+
t.gsub!('/', ' ')
|
113
|
+
# t.gsub!("-"," ")
|
114
|
+
t.gsub!('.', ' ')
|
115
|
+
t.gsub!(',', ' ')
|
116
|
+
t.gsub!(' ', ' ')
|
117
|
+
t.gsub!(' ', ' ')
|
118
|
+
t.split(' ').count
|
105
119
|
end
|
106
120
|
|
107
|
-
|
108
|
-
|
121
|
+
##
|
122
|
+
# Do mistake to input
|
123
|
+
# @param input (String)
|
124
|
+
# @return String
|
125
|
+
def do_mistake_to(input = '')
|
126
|
+
text = input.dup
|
109
127
|
keys = @mistakes.keys
|
110
128
|
|
111
129
|
# Try to do mistake with one item from the key list
|
112
130
|
keys.shuffle!
|
113
131
|
keys.each do |key|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
132
|
+
next unless text.include? key.to_s
|
133
|
+
|
134
|
+
values = @mistakes[key].split(',')
|
135
|
+
values.shuffle!
|
136
|
+
text = text.sub(key.to_s, values[0].to_s)
|
137
|
+
return text
|
120
138
|
end
|
121
139
|
|
122
140
|
# Force mistake by swapping letters
|
123
|
-
if
|
124
|
-
i = rand(
|
125
|
-
aux =
|
126
|
-
|
127
|
-
|
141
|
+
if text.size > 1
|
142
|
+
i = rand(text.size - 2)
|
143
|
+
aux = text[i]
|
144
|
+
text[i] = text[i + 1]
|
145
|
+
text[i + 1] = aux
|
128
146
|
end
|
129
|
-
return
|
147
|
+
return text if text != input
|
130
148
|
|
131
|
-
|
149
|
+
text + 's'
|
132
150
|
end
|
133
151
|
|
134
152
|
def hide_text(input_text)
|