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.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +13 -17
  3. data/bin/asker +2 -1
  4. data/lib/asker/ai/ai.rb +10 -3
  5. data/lib/asker/ai/ai_calculate.rb +20 -6
  6. data/lib/asker/ai/code/base_code_ai.rb +104 -0
  7. data/lib/asker/{code/ai → ai/code}/code_ai_factory.rb +11 -1
  8. data/lib/asker/{code/ai → ai/code}/javascript_code_ai.rb +2 -5
  9. data/lib/asker/ai/code/problem_code_ai.rb +176 -0
  10. data/lib/asker/{code/ai → ai/code}/python_code_ai.rb +2 -5
  11. data/lib/asker/{code/ai → ai/code}/ruby_code_ai.rb +14 -7
  12. data/lib/asker/{code/ai → ai/code}/sql_code_ai.rb +2 -5
  13. data/lib/asker/ai/concept_ai.rb +13 -3
  14. data/lib/asker/ai/question.rb +28 -6
  15. data/lib/asker/ai/stages/base_stage.rb +45 -6
  16. data/lib/asker/ai/stages/stage_b.rb +100 -50
  17. data/lib/asker/ai/stages/stage_d.rb +75 -90
  18. data/lib/asker/ai/stages/stage_f.rb +64 -36
  19. data/lib/asker/ai/stages/stage_i.rb +79 -92
  20. data/lib/asker/ai/stages/stage_s.rb +41 -36
  21. data/lib/asker/ai/stages/stage_t.rb +149 -108
  22. data/lib/asker/application.rb +19 -11
  23. data/lib/asker/cli.rb +31 -44
  24. data/lib/asker/data/code.rb +62 -0
  25. data/lib/asker/data/column.rb +31 -21
  26. data/lib/asker/data/concept.rb +109 -65
  27. data/lib/asker/data/data_field.rb +14 -0
  28. data/lib/asker/data/project_data.rb +63 -0
  29. data/lib/asker/data/row.rb +75 -52
  30. data/lib/asker/data/table.rb +91 -42
  31. data/lib/asker/data/template.rb +3 -1
  32. data/lib/asker/data/world.rb +51 -35
  33. data/lib/asker/displayer/code_displayer.rb +7 -0
  34. data/lib/asker/displayer/concept_ai_displayer.erb +10 -0
  35. data/lib/asker/displayer/concept_ai_displayer.rb +74 -53
  36. data/lib/asker/displayer/concept_displayer.rb +16 -9
  37. data/lib/asker/displayer/stats_displayer.rb +12 -3
  38. data/lib/asker/exporter/concept_ai_gift_exporter.rb +19 -11
  39. data/lib/asker/exporter/concept_ai_moodle_exporter.rb +44 -0
  40. data/lib/asker/exporter/concept_ai_yaml_exporter.rb +13 -6
  41. data/lib/asker/exporter/concept_doc_exporter.rb +14 -1
  42. data/lib/asker/exporter/data_gift_exporter.rb +29 -0
  43. data/lib/asker/exporter/output_file_exporter.rb +9 -6
  44. data/lib/asker/files/{config.ini → asker.ini} +48 -1
  45. data/lib/asker/files/example-concept.haml +0 -1
  46. data/lib/asker/files/language/du/connectors.yaml +81 -0
  47. data/lib/asker/{lang/locales/fr → files/language/du}/mistakes.yaml +1 -1
  48. data/lib/asker/files/language/du/templates.yaml +29 -0
  49. data/lib/asker/{lang/locales → files/language}/en/connectors.yaml +0 -0
  50. data/lib/asker/{lang/locales → files/language}/en/mistakes.yaml +0 -0
  51. data/lib/asker/files/language/en/templates.yaml +29 -0
  52. data/lib/asker/{lang/locales → files/language}/es/connectors.yaml +0 -0
  53. data/lib/asker/files/language/es/mistakes.yaml +84 -0
  54. data/lib/asker/files/language/es/templates.yaml +29 -0
  55. data/lib/asker/files/language/fr/connectors.yaml +76 -0
  56. data/lib/asker/{lang/locales/es → files/language/fr}/mistakes.yaml +0 -0
  57. data/lib/asker/files/language/fr/templates.yaml +29 -0
  58. data/lib/asker/{lang/locales → files/language}/javascript/connectors.yaml +0 -0
  59. data/lib/asker/{lang/locales → files/language}/javascript/mistakes.yaml +0 -0
  60. data/lib/asker/{lang/locales → files/language}/javascript/templates.yaml +0 -0
  61. data/lib/asker/{lang/locales → files/language}/math/connectors.yaml +0 -0
  62. data/lib/asker/{lang/locales → files/language}/math/mistakes.yaml +0 -0
  63. data/lib/asker/{lang/locales → files/language}/math/templates.yaml +0 -0
  64. data/lib/asker/{lang/locales → files/language}/python/connectors.yaml +0 -0
  65. data/lib/asker/{lang/locales → files/language}/python/mistakes.yaml +0 -0
  66. data/lib/asker/{lang/locales → files/language}/python/templates.yaml +0 -0
  67. data/lib/asker/{lang/locales → files/language}/ruby/connectors.yaml +0 -0
  68. data/lib/asker/{lang/locales → files/language}/ruby/mistakes.yaml +0 -0
  69. data/lib/asker/{lang/locales → files/language}/ruby/templates.yaml +0 -0
  70. data/lib/asker/{lang/locales → files/language}/sql/connectors.yaml +0 -0
  71. data/lib/asker/{lang/locales → files/language}/sql/mistakes.yaml +0 -0
  72. data/lib/asker/{lang/locales → files/language}/sql/templates.yaml +0 -0
  73. data/lib/asker/formatter/concept_doc_formatter.rb +0 -4
  74. data/lib/asker/formatter/concept_string_formatter.rb +7 -4
  75. data/lib/asker/formatter/moodle/matching.erb +38 -0
  76. data/lib/asker/formatter/moodle/multichoice.erb +49 -0
  77. data/lib/asker/formatter/moodle/shortanswer.erb +30 -0
  78. data/lib/asker/formatter/moodle/truefalse.erb +47 -0
  79. data/lib/asker/formatter/question_gift_formatter.rb +30 -20
  80. data/lib/asker/formatter/question_moodle_formatter.rb +27 -0
  81. data/lib/asker/{checker.rb → input_checker.rb} +126 -49
  82. data/lib/asker/lang/lang.rb +17 -10
  83. data/lib/asker/lang/lang_factory.rb +32 -5
  84. data/lib/asker/lang/text_actions.rb +87 -69
  85. data/lib/asker/loader/code_loader.rb +4 -4
  86. data/lib/asker/loader/content_loader.rb +21 -19
  87. data/lib/asker/loader/directory_loader.rb +1 -8
  88. data/lib/asker/loader/embedded_file.rb +42 -0
  89. data/lib/asker/loader/file_loader.rb +3 -14
  90. data/lib/asker/loader/haml_loader.rb +15 -0
  91. data/lib/asker/loader/image_url_loader.rb +8 -12
  92. data/lib/asker/loader/input_loader.rb +13 -15
  93. data/lib/asker/loader/project_loader.rb +25 -15
  94. data/lib/asker/logger.rb +36 -9
  95. data/lib/asker/skeleton.rb +23 -13
  96. data/lib/asker.rb +76 -42
  97. metadata +53 -54
  98. data/lib/asker/code/ai/base_code_ai.rb +0 -48
  99. data/lib/asker/code/code.rb +0 -53
  100. data/lib/asker/lang/locales/du/templates.yaml +0 -50
  101. data/lib/asker/lang/locales/en/templates.yaml +0 -29
  102. data/lib/asker/lang/locales/es/templates.yaml +0 -29
  103. data/lib/asker/lang/locales/fr/connectors.yaml +0 -92
  104. data/lib/asker/lang/locales/fr/templates.yaml +0 -29
  105. 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 Checker
7
- ##
8
- # Check:
9
- # * file exist
10
- # * filename extension
11
- # * and HAML syntax
12
- # @param filepath (String)
13
- def self.check(filepath)
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("File not found!").red.bright
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("Only check HAML files!").yellow.bright
21
+ puts Rainbow('Only check HAML files!').yellow.bright if verbose
20
22
  return false
21
23
  end
22
- check_filepath(filepath)
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.is_ok?
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
- attr_reader :outputs
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 is_ok?
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
- puts "%02d" % i[:id] + ": %s." % i[:msg] + " => #{i[:source][0,40]}" if errors < 11
69
- puts "..." if errors == 11
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("[ERROR] #{errors} errors from #{@inputs.size} lines!").red.bright if errors > 0
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
- if line.strip.size.zero? or line.start_with? '#'
103
- @outputs[index][:type] = :empty
104
- @outputs[index][:level] = -1
105
- @outputs[index][:state] = :ok
106
- end
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 == 0
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 > 0 and line.include?('%map{')
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
- if count_spaces(line) == 6
257
+ case count_spaces(line)
258
+ when 6
213
259
  @outputs[index][:level] = 3
214
260
  parent = find_parent(index)
215
- unless [:table, :features].include? parent
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
- elsif count_spaces(line) == 8
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
- if count_spaces(line) == 8
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
- elsif count_spaces(line) == 10
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
- return 0 if line.start_with? '%'
359
- return 1 if line.start_with? ' %'
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
@@ -23,19 +23,26 @@ class Lang
23
23
  private
24
24
 
25
25
  def load_files
26
- dirbase = File.dirname(__FILE__)
27
- filename = File.join(dirbase, 'locales', @code, 'templates.yaml')
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
- @templates = YAML.load(File.new(filename))
38
+ content = YAML.load(File.new(filepath))
30
39
  rescue StandardError => e
31
- Logger.verboseln "[ERROR] lang.initialize(): Reading YAML file <#{filename}>"
32
- Logger.verboseln "[ADVISE] Revise apostrophe into string without \\ char\n"
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
- filename = File.join(dirbase, 'locales', @code, 'connectors.yaml')
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 '../project'
5
+ require_relative '../application'
6
6
 
7
- # LangFactory#get
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
- Project.instance.locales.each { |i| @langs[i] = Lang.new(i) }
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
- def get(locale)
17
- @langs[locale]
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
- def text_for(pOption, pText1="",pText2="",pText3="",pText4="",pText5="",pText6="",pText7="")
6
- text1=pText1
7
- text2=pText2
8
- text3=pText3
9
- text4=pText4
10
- text5=pText5
11
- text6=pText6
12
- text7=pText7
13
-
14
- # TODO: check if exists pOption before use it
15
- renderer = ERB.new(@templates[pOption])
16
- output = renderer.result(binding)
17
- return output
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
- def text_filter_connectors(pText, pFilter)
21
- input_lines = pText.split(".")
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 pFilter) || (!flag and !pFilter and word.length>1)
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
- end
45
+ end
39
46
  end
40
47
  row << '.'
41
48
  output_lines << row
42
- end
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 (item[:word].include?(e)) }
55
+ exclude.each { |e| flag = false if item[:word].include? e }
49
56
  indexes << index if flag
50
57
  end
51
58
 
52
- result={ :lines => output_lines, :words => output_words, :indexes => indexes }
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
- text_filter_connectors(text, true)
71
+ text_filter_connectors(text, true)
62
72
  end
63
73
 
64
- def build_text_from_filtered(pStruct, pIndexes)
65
- lines = pStruct[:lines]
66
- lIndexes = pIndexes.sort
67
- counter = 1
68
- lText = ''
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
- lText += (' ' + value)
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 lIndexes.include? value
78
- lText += " [#{counter.to_s}]"
87
+ if indexes.include? value
88
+ text += " [#{counter}]"
79
89
  counter += 1
80
90
  else
81
- lword = pStruct[:words][value][:word]
82
- lText += (' ' + lword)
91
+ word = input_struct[:words][value][:word]
92
+ text += (' ' + word)
83
93
  end
84
94
  end
85
95
  end
86
96
  end
87
- lText.gsub!(' .', '.')
88
- lText.gsub!(' ,', ',')
89
- lText = lText[1, lText.size] if lText[0] == ' '
90
- lText
97
+ text.gsub!(' .', '.')
98
+ text.gsub!(' ,', ',')
99
+ text = text[1, text.size] if text[0] == ' '
100
+ text
91
101
  end
92
102
 
93
- def count_words(pInputText)
94
- return 0 if pInputText.nil?
95
-
96
- t = pInputText.clone
97
- t.gsub!("\n"," ")
98
- t.gsub!("/"," ")
99
- #t.gsub!("-"," ")
100
- t.gsub!("."," ")
101
- t.gsub!(","," ")
102
- t.gsub!(" "," ")
103
- t.gsub!(" "," ")
104
- t.split(" ").count
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
- def do_mistake_to(pText = '')
108
- lText = pText.dup
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
- if lText.include? key.to_s
115
- values = @mistakes[key].split(',')
116
- values.shuffle!
117
- lText = lText.sub(key.to_s,values[0].to_s)
118
- return lText
119
- end
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 lText.size > 1
124
- i = rand(lText.size - 2)
125
- aux = lText[i]
126
- lText[i] = lText[i + 1]
127
- lText[i + 1] = aux
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 lText if lText != pText
147
+ return text if text != input
130
148
 
131
- lText + 's'
149
+ text + 's'
132
150
  end
133
151
 
134
152
  def hide_text(input_text)