immosquare-yaml 0.1.9 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/immosquare-yaml/configuration.rb +1 -1
- data/lib/immosquare-yaml/railtie.rb +1 -1
- data/lib/immosquare-yaml/shared_methods.rb +3 -3
- data/lib/immosquare-yaml/translate.rb +23 -25
- data/lib/immosquare-yaml/version.rb +1 -1
- data/lib/immosquare-yaml.rb +72 -77
- data/lib/tasks/immosquare-yaml.rake +4 -6
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4fd55f9ae0f4d9e13ef667a868df1670aa3a038860a8f2cb4db0e2f29dc08fa9
|
4
|
+
data.tar.gz: bdf032155c8a4c9db6c0697c249c459378caaac8c46f00cf3d803d194a294dfb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b8ed1227862217b4d7dca2011e4b8df8ca5ed20e8762d462ac2d4fbc062f295aaecf2f782d09405687f8d7ad09844b2ef3734ca8565814838db5739660674bb
|
7
|
+
data.tar.gz: a2266c2e2b51e57715ae21ffd25c9391c9af2de021f702805271abad716261ad0ce6ece6cdd85d942725a627beeb610837c497a31bc3464f63111e5e754b445b
|
@@ -15,7 +15,7 @@ module ImmosquareYaml
|
|
15
15
|
"YES", "NO", "ON", "OFF", "TRUE", "FALSE"
|
16
16
|
].freeze
|
17
17
|
|
18
|
-
|
18
|
+
|
19
19
|
##============================================================##
|
20
20
|
## Deep transform values resursively
|
21
21
|
##============================================================##
|
@@ -31,7 +31,7 @@ module ImmosquareYaml
|
|
31
31
|
|
32
32
|
##============================================================##
|
33
33
|
## sort_by_key Function
|
34
|
-
## Purpose: Sort a hash by its keys, optionally recursively, with
|
34
|
+
## Purpose: Sort a hash by its keys, optionally recursively, with
|
35
35
|
## case-insensitive comparison and stripping of double quotes.
|
36
36
|
## ============================================================ #
|
37
37
|
def sort_by_key(hash, recursive = false, &block)
|
@@ -42,4 +42,4 @@ module ImmosquareYaml
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
45
|
-
end
|
45
|
+
end
|
@@ -3,7 +3,6 @@ require "httparty"
|
|
3
3
|
|
4
4
|
|
5
5
|
module ImmosquareYaml
|
6
|
-
|
7
6
|
module Translate
|
8
7
|
extend SharedMethods
|
9
8
|
|
@@ -23,10 +22,10 @@ module ImmosquareYaml
|
|
23
22
|
##=============================================================##
|
24
23
|
## Load config keys from config_dev.yml
|
25
24
|
##=============================================================##
|
26
|
-
raise("Error: openai_api_key not found in config_dev.yml") if ImmosquareYaml.configuration.openai_api_key.nil?
|
25
|
+
raise("Error: openai_api_key not found in config_dev.yml") if ImmosquareYaml.configuration.openai_api_key.nil?
|
27
26
|
raise("Error: File #{file_path} not found") if !File.exist?(file_path)
|
28
27
|
raise("Error: locale is not a locale") if !locale_to.is_a?(String) || locale_to.size != 2
|
29
|
-
|
28
|
+
|
30
29
|
##============================================================##
|
31
30
|
## We clean the file before translation
|
32
31
|
##============================================================##
|
@@ -63,7 +62,7 @@ module ImmosquareYaml
|
|
63
62
|
##============================================================##
|
64
63
|
array_to = translatable_array(hash_to)
|
65
64
|
array_to = array_to.map {|k, v| [k, v, nil] }
|
66
|
-
|
65
|
+
|
67
66
|
##============================================================##
|
68
67
|
## If we already have a translation file for the language
|
69
68
|
## we get the values in it and put it in our
|
@@ -121,7 +120,7 @@ module ImmosquareYaml
|
|
121
120
|
## format = "string" and keys_only = true => ["fr.demo1", "fr.demo2.demo2-1"]
|
122
121
|
## format = "array" and keys_only = false => [[["fr", "demo1"], "demo1"], [["fr", "demo2", "demo2-1"], "demo2-1"]]
|
123
122
|
## format = "array" and keys_only = true => [["fr", "demo1"], ["fr", "demo2", "demo2-1"]]
|
124
|
-
## ============================================================
|
123
|
+
## ============================================================
|
125
124
|
def translatable_array(hash, key = nil, result = [], **options)
|
126
125
|
options = {
|
127
126
|
:format => "string",
|
@@ -140,7 +139,6 @@ module ImmosquareYaml
|
|
140
139
|
result << (options[:keys_only] ? r2 : [r2, hash])
|
141
140
|
end
|
142
141
|
result
|
143
|
-
|
144
142
|
end
|
145
143
|
|
146
144
|
##============================================================##
|
@@ -156,13 +154,13 @@ module ImmosquareYaml
|
|
156
154
|
parent[leaf] = value
|
157
155
|
end
|
158
156
|
final
|
159
|
-
end
|
157
|
+
end
|
160
158
|
|
161
159
|
##============================================================##
|
162
160
|
## Translate with OpenAI
|
163
|
-
##
|
161
|
+
##
|
164
162
|
## [
|
165
|
-
## ["en.mlsconnect.contact_us", "Nous contacter", "Contact us"],
|
163
|
+
## ["en.mlsconnect.contact_us", "Nous contacter", "Contact us"],
|
166
164
|
## ["en.mlsconnect.description", "Description", nil],
|
167
165
|
## ...
|
168
166
|
## ]
|
@@ -190,8 +188,8 @@ module ImmosquareYaml
|
|
190
188
|
blank_values = [NOTHING, SPACE, "\"\"", "\"#{SPACE}\""]
|
191
189
|
cant_be_translated = "CANNOT-BE-TRANSLATED"
|
192
190
|
array = array.map do |key, from, to|
|
193
|
-
[key, from, blank_values.include?(from) ? from : to]
|
194
|
-
end
|
191
|
+
[key, from, blank_values.include?(from) ? from : to]
|
192
|
+
end
|
195
193
|
|
196
194
|
|
197
195
|
##============================================================##
|
@@ -211,14 +209,14 @@ module ImmosquareYaml
|
|
211
209
|
## Remove quotes surrounding the value if they are present.
|
212
210
|
## and remove to to avoid error in translation
|
213
211
|
##============================================================##
|
214
|
-
data_open_ai = data_open_ai.map do |index, from, _to|
|
212
|
+
data_open_ai = data_open_ai.map do |index, from, _to|
|
215
213
|
from = from.to_s
|
216
214
|
from = from[1..-2] while (from.start_with?(DOUBLE_QUOTE) && from.end_with?(DOUBLE_QUOTE)) || (from.start_with?(SIMPLE_QUOTE) && from.end_with?(SIMPLE_QUOTE))
|
217
|
-
[index, from]
|
215
|
+
[index, from]
|
218
216
|
end
|
219
217
|
|
220
218
|
return array if data_open_ai.empty?
|
221
|
-
|
219
|
+
|
222
220
|
##============================================================##
|
223
221
|
## Call OpenAI API
|
224
222
|
##============================================================##
|
@@ -243,8 +241,8 @@ module ImmosquareYaml
|
|
243
241
|
"Content-Type" => "application/json",
|
244
242
|
"Authorization" => "Bearer #{ImmosquareYaml.configuration.openai_api_key}"
|
245
243
|
}
|
246
|
-
|
247
|
-
|
244
|
+
|
245
|
+
|
248
246
|
##============================================================##
|
249
247
|
## Loop
|
250
248
|
##============================================================##
|
@@ -254,7 +252,7 @@ module ImmosquareYaml
|
|
254
252
|
|
255
253
|
|
256
254
|
begin
|
257
|
-
puts("call OPENAI Api (with model #{model[:name]}) #{" for #{data_group.size} fields (#{index}-#{index+data_group.size})" if data_open_ai.size > group_size}")
|
255
|
+
puts("call OPENAI Api (with model #{model[:name]}) #{" for #{data_group.size} fields (#{index}-#{index + data_group.size})" if data_open_ai.size > group_size}")
|
258
256
|
prompt = "#{prompt_init}:\n\n#{data_group.inspect}\n\n"
|
259
257
|
body = {
|
260
258
|
:model => model[:name],
|
@@ -266,11 +264,11 @@ module ImmosquareYaml
|
|
266
264
|
}
|
267
265
|
t0 = Time.now
|
268
266
|
call = HTTParty.post("https://api.openai.com/v1/chat/completions", :body => body.to_json, :headers => headers, :timeout => 500)
|
269
|
-
|
267
|
+
|
270
268
|
puts("responded in #{(Time.now - t0).round(2)} seconds")
|
271
|
-
raise(call["error"]["message"]) if call.code != 200
|
269
|
+
raise(call["error"]["message"]) if call.code != 200
|
270
|
+
|
272
271
|
|
273
|
-
|
274
272
|
##============================================================##
|
275
273
|
## We check that the result is complete
|
276
274
|
##============================================================##
|
@@ -278,7 +276,7 @@ module ImmosquareYaml
|
|
278
276
|
choice = response["choices"][0]
|
279
277
|
raise("Result is not complete") if choice["finish_reason"] != "stop"
|
280
278
|
|
281
|
-
|
279
|
+
|
282
280
|
##============================================================##
|
283
281
|
## We calculate the estimate price of the call
|
284
282
|
##============================================================##
|
@@ -313,7 +311,7 @@ module ImmosquareYaml
|
|
313
311
|
##============================================================##
|
314
312
|
ai_resuslts.each do |index, translation|
|
315
313
|
begin
|
316
|
-
array[index.to_i][2] = translation
|
314
|
+
array[index.to_i][2] = translation
|
317
315
|
rescue StandardError => e
|
318
316
|
puts(e.message)
|
319
317
|
end
|
@@ -322,14 +320,14 @@ module ImmosquareYaml
|
|
322
320
|
##============================================================##
|
323
321
|
## We return the modified array
|
324
322
|
##============================================================##
|
325
|
-
array.map.with_index do |(k, from, to), index|
|
323
|
+
array.map.with_index do |(k, from, to), index|
|
326
324
|
from = from.to_s
|
327
325
|
to = "#{DOUBLE_QUOTE}#{to}#{DOUBLE_QUOTE}" if ai_resuslts.find {|i, _t| i == index } && ((from.start_with?(DOUBLE_QUOTE) && from.end_with?(DOUBLE_QUOTE)) || (from.start_with?(SIMPLE_QUOTE) && from.end_with?(SIMPLE_QUOTE)))
|
328
|
-
[k, from, to]
|
326
|
+
[k, from, to]
|
329
327
|
end
|
330
328
|
end
|
331
329
|
|
332
330
|
|
333
331
|
end
|
334
332
|
end
|
335
|
-
end
|
333
|
+
end
|
data/lib/immosquare-yaml.rb
CHANGED
@@ -6,12 +6,12 @@ require_relative "immosquare-yaml/railtie" if defined?(Rails)
|
|
6
6
|
|
7
7
|
module ImmosquareYaml
|
8
8
|
extend SharedMethods
|
9
|
-
|
9
|
+
|
10
10
|
class << self
|
11
11
|
|
12
|
-
|
13
12
|
|
14
|
-
|
13
|
+
|
14
|
+
|
15
15
|
##===========================================================================##
|
16
16
|
## Gem configuration
|
17
17
|
##===========================================================================##
|
@@ -46,10 +46,10 @@ module ImmosquareYaml
|
|
46
46
|
:sort => true,
|
47
47
|
:output => file_path
|
48
48
|
}.merge(options)
|
49
|
-
|
49
|
+
|
50
50
|
begin
|
51
51
|
raise("File not found") if !File.exist?(file_path)
|
52
|
-
|
52
|
+
|
53
53
|
##===========================================================================##
|
54
54
|
## Setup variables
|
55
55
|
##===========================================================================##
|
@@ -61,8 +61,8 @@ module ImmosquareYaml
|
|
61
61
|
original_content = File.read(file_path) if output_file_path != file_path
|
62
62
|
|
63
63
|
##===========================================================================##
|
64
|
-
## The cleaning procedure is initialized with a comprehensive clean, transforming
|
65
|
-
## the YAML content to a hash to facilitate optional sorting, before
|
64
|
+
## The cleaning procedure is initialized with a comprehensive clean, transforming
|
65
|
+
## the YAML content to a hash to facilitate optional sorting, before
|
66
66
|
## rewriting it to the YAML file in its cleaned and optionally sorted state.
|
67
67
|
##===========================================================================##
|
68
68
|
clean_yml(file_path)
|
@@ -74,12 +74,12 @@ module ImmosquareYaml
|
|
74
74
|
## Restore original content if necessary
|
75
75
|
##===========================================================================##
|
76
76
|
File.write(file_path, original_content) if output_file_path != file_path
|
77
|
-
|
77
|
+
|
78
78
|
##===========================================================================##
|
79
79
|
## Write the cleaned YAML content to the specified output file
|
80
80
|
##===========================================================================##
|
81
81
|
FileUtils.mkdir_p(File.dirname(output_file_path))
|
82
|
-
File.write(output_file_path, parsed_yml)
|
82
|
+
File.write(output_file_path, parsed_yml)
|
83
83
|
true
|
84
84
|
rescue StandardError => e
|
85
85
|
puts(e.message)
|
@@ -88,8 +88,8 @@ module ImmosquareYaml
|
|
88
88
|
end
|
89
89
|
|
90
90
|
##==========================================================================##
|
91
|
-
## This method parses a specified YAML file, carrying out a preliminary
|
92
|
-
## cleaning operation to ensure a smooth parsing process. Following this,
|
91
|
+
## This method parses a specified YAML file, carrying out a preliminary
|
92
|
+
## cleaning operation to ensure a smooth parsing process. Following this,
|
93
93
|
## the cleaned file is transformed into a hash, which can optionally be sorted.
|
94
94
|
## It operates under the assumption that the file is properly structured.
|
95
95
|
##
|
@@ -102,7 +102,7 @@ module ImmosquareYaml
|
|
102
102
|
##==========================================================================##
|
103
103
|
def parse(file_path, **options)
|
104
104
|
options = {:sort => true}.merge(options)
|
105
|
-
|
105
|
+
|
106
106
|
begin
|
107
107
|
raise("File not found") if !File.exist?(file_path)
|
108
108
|
|
@@ -132,12 +132,12 @@ module ImmosquareYaml
|
|
132
132
|
false
|
133
133
|
end
|
134
134
|
end
|
135
|
-
|
135
|
+
|
136
136
|
##===========================================================================##
|
137
|
-
## This method performs a dump operation to obtain a well-structured
|
138
|
-
## YAML file from a hash input. It iterates through each key-value pair in the
|
139
|
-
## hash and constructs a series of lines representing the YAML file, with
|
140
|
-
## appropriate indentations and handling of various value types including
|
137
|
+
## This method performs a dump operation to obtain a well-structured
|
138
|
+
## YAML file from a hash input. It iterates through each key-value pair in the
|
139
|
+
## hash and constructs a series of lines representing the YAML file, with
|
140
|
+
## appropriate indentations and handling of various value types including
|
141
141
|
## strings with newline characters.
|
142
142
|
##
|
143
143
|
## Params:
|
@@ -151,18 +151,18 @@ module ImmosquareYaml
|
|
151
151
|
def dump(hash, lines = [], indent = 0)
|
152
152
|
hash.each do |key, value|
|
153
153
|
##===========================================================================##
|
154
|
-
## Preparing the key with the proper indentation before identifying
|
154
|
+
## Preparing the key with the proper indentation before identifying
|
155
155
|
## the type of the value to handle it appropriately in the YAML representation.
|
156
156
|
##===========================================================================##
|
157
157
|
line = "#{SPACE * indent}#{clean_key(key)}:"
|
158
|
-
|
158
|
+
|
159
159
|
case value
|
160
160
|
when nil
|
161
161
|
lines << "#{line} null"
|
162
162
|
when String
|
163
163
|
if value.include?(NEWLINE) || value.include?('\n')
|
164
164
|
##=============================================================##
|
165
|
-
## We display the line with the key
|
165
|
+
## We display the line with the key
|
166
166
|
## then the indentation if necessary
|
167
167
|
## then - if necessary (the + is not displayed because it is
|
168
168
|
## the default behavior)
|
@@ -197,7 +197,7 @@ module ImmosquareYaml
|
|
197
197
|
end
|
198
198
|
|
199
199
|
##===========================================================================##
|
200
|
-
## Finalizing the construction by adding a newline at the end and
|
200
|
+
## Finalizing the construction by adding a newline at the end and
|
201
201
|
## removing whitespace from empty lines.
|
202
202
|
##===========================================================================##
|
203
203
|
lines += [NOTHING]
|
@@ -211,8 +211,8 @@ module ImmosquareYaml
|
|
211
211
|
##===========================================================================##
|
212
212
|
## This method ensures the file ends with a single newline, facilitating
|
213
213
|
## cleaner multi-line blocks. It operates by reading all lines of the file,
|
214
|
-
## removing any empty lines at the end, and then appending a newline.
|
215
|
-
## This guarantees the presence of a newline at the end, and also prevents
|
214
|
+
## removing any empty lines at the end, and then appending a newline.
|
215
|
+
## This guarantees the presence of a newline at the end, and also prevents
|
216
216
|
## multiple newlines from being present at the end.
|
217
217
|
##
|
218
218
|
## Params:
|
@@ -226,32 +226,27 @@ module ImmosquareYaml
|
|
226
226
|
## Read all lines from the file
|
227
227
|
## https://gist.github.com/guilhermesimoes/d69e547884e556c3dc95
|
228
228
|
##============================================================##
|
229
|
-
|
229
|
+
content = File.read(file_path)
|
230
230
|
|
231
|
-
##============================================================##
|
232
|
-
## Ensure the last line ends with a newline character
|
233
|
-
##============================================================##
|
234
|
-
lines[-1] = "#{lines[-1]}#{NEWLINE}" if !lines[-1].end_with?(NEWLINE)
|
235
|
-
|
236
231
|
##===========================================================================##
|
237
232
|
## Remove all trailing empty lines at the end of the file
|
233
|
+
content.gsub!(/#{Regexp.escape($INPUT_RECORD_SEPARATOR)}+\z/, "")
|
238
234
|
##===========================================================================##
|
239
|
-
|
240
|
-
|
235
|
+
|
241
236
|
##===========================================================================##
|
242
237
|
## Append a newline at the end to maintain the file structure
|
243
238
|
###===========================================================================##
|
244
|
-
|
245
|
-
|
239
|
+
content += $INPUT_RECORD_SEPARATOR
|
240
|
+
|
246
241
|
##===========================================================================##
|
247
242
|
## Write the modified lines back to the file
|
248
243
|
##===========================================================================##
|
249
|
-
File.write(file_path,
|
244
|
+
File.write(file_path, content)
|
250
245
|
|
251
246
|
##===========================================================================##
|
252
247
|
## Return the total number of lines in the modified file
|
253
248
|
##===========================================================================##
|
254
|
-
lines.size
|
249
|
+
content.lines.size
|
255
250
|
end
|
256
251
|
|
257
252
|
##============================================================##
|
@@ -267,15 +262,15 @@ module ImmosquareYaml
|
|
267
262
|
|
268
263
|
##===================================================================================#
|
269
264
|
## First, we normalize the file by ensuring it always ends with an empty line
|
270
|
-
## This also allows us to get the total number of lines in the file,
|
265
|
+
## This also allows us to get the total number of lines in the file,
|
271
266
|
## helping us to determine when we are processing the last line
|
272
267
|
###===================================================================================#
|
273
268
|
line_count = normalize_last_line(file_path)
|
274
|
-
|
275
|
-
|
269
|
+
|
270
|
+
|
276
271
|
File.foreach(file_path) do |current_line|
|
277
272
|
last_line = line_index == line_count
|
278
|
-
|
273
|
+
|
279
274
|
##===================================================================================#
|
280
275
|
## Cleaning the current line by removing multiple spaces occurring after a non-space character
|
281
276
|
##===================================================================================#
|
@@ -285,10 +280,10 @@ module ImmosquareYaml
|
|
285
280
|
## Trimming potential whitespace characters from the end of the line
|
286
281
|
##============================================================##
|
287
282
|
current_line = current_line.rstrip
|
288
|
-
|
283
|
+
|
289
284
|
|
290
285
|
##===================================================================================#
|
291
|
-
## Detecting blank lines to specially handle the last line within a block;
|
286
|
+
## Detecting blank lines to specially handle the last line within a block;
|
292
287
|
## if we are inside a block or it's the last line, we avoid skipping
|
293
288
|
##===================================================================================#
|
294
289
|
blank_line = current_line.gsub(NEWLINE, NOTHING).empty?
|
@@ -301,9 +296,9 @@ module ImmosquareYaml
|
|
301
296
|
indent_level = current_line[/\A */].size
|
302
297
|
need_to_clean_prev_inblock = inblock == true && ((!blank_line && indent_level <= inblock_indent) || last_line)
|
303
298
|
need_to_clen_prev_weirdblock = weirdblock == true && (indent_level <= weirdblock_indent || last_line)
|
304
|
-
|
299
|
+
|
305
300
|
##===================================================================================#
|
306
|
-
## Handling the exit from a block:
|
301
|
+
## Handling the exit from a block:
|
307
302
|
## if we are exiting a block, we clean the entire block
|
308
303
|
##===================================================================================#
|
309
304
|
if need_to_clean_prev_inblock
|
@@ -323,7 +318,7 @@ module ImmosquareYaml
|
|
323
318
|
##============================================================##
|
324
319
|
## Handling different types of blocks (literal blocks "|",
|
325
320
|
## folded blocks ">", etc.)
|
326
|
-
## and applying the respective formatting strategies based on
|
321
|
+
## and applying the respective formatting strategies based on
|
327
322
|
## block type and additional indent specified
|
328
323
|
##
|
329
324
|
## | => Literal blocks: It keeps line breaks as
|
@@ -359,25 +354,25 @@ module ImmosquareYaml
|
|
359
354
|
end
|
360
355
|
|
361
356
|
##===================================================================================#
|
362
|
-
## Handling 'weirdblocks': cases where multi-line values are enclosed in quotes,
|
357
|
+
## Handling 'weirdblocks': cases where multi-line values are enclosed in quotes,
|
363
358
|
## which should actually be single-line values
|
364
|
-
## key: "
|
359
|
+
## key: "
|
365
360
|
## line1
|
366
361
|
## line2
|
367
362
|
## line3"
|
368
|
-
## key: '
|
363
|
+
## key: '
|
369
364
|
## line1
|
370
365
|
## line2
|
371
366
|
## line3'
|
372
367
|
##============================================================##
|
373
368
|
if need_to_clen_prev_weirdblock
|
374
|
-
weirdblock = false
|
369
|
+
weirdblock = false
|
375
370
|
key, value = lines[-1].split(":", 2)
|
376
371
|
lines[-1] = "#{key}: #{clean_value(value)}"
|
377
372
|
end
|
378
373
|
|
379
374
|
##===================================================================================#
|
380
|
-
## Handling keys without values: if the previous line ends with a colon (:) and is not
|
375
|
+
## Handling keys without values: if the previous line ends with a colon (:) and is not
|
381
376
|
## followed by a value, we assign 'null' as the value
|
382
377
|
##===================================================================================#
|
383
378
|
if inblock == false && weirdblock == false && lines[-1] && lines[-1].end_with?(":") && last_inblock == false
|
@@ -395,8 +390,8 @@ module ImmosquareYaml
|
|
395
390
|
key = inblock || weirdblock ? nil : split[0].to_s.strip
|
396
391
|
|
397
392
|
##===================================================================================#
|
398
|
-
## Line processing based on various conditions such as being inside a block,
|
399
|
-
## starting with a comment symbol (#), or being a part of a 'weirdblock'
|
393
|
+
## Line processing based on various conditions such as being inside a block,
|
394
|
+
## starting with a comment symbol (#), or being a part of a 'weirdblock'
|
400
395
|
## Each case has its specific line cleaning strategy
|
401
396
|
## ----
|
402
397
|
## If the line is commented out, we keep and we remove newlines
|
@@ -450,7 +445,7 @@ module ImmosquareYaml
|
|
450
445
|
|
451
446
|
if !split[1].empty?
|
452
447
|
value = split[1].to_s.strip
|
453
|
-
|
448
|
+
|
454
449
|
##============================================================##
|
455
450
|
## We are in a multiline block which should be an inline
|
456
451
|
## if the value starts with a " and the number of " is odd
|
@@ -484,7 +479,7 @@ module ImmosquareYaml
|
|
484
479
|
lines += [NOTHING]
|
485
480
|
lines = lines.map {|l| (l.strip.empty? ? NOTHING : l).to_s.gsub(/(?<=\S)\s+/, SPACE) }
|
486
481
|
File.write(file_path, lines.join(NEWLINE))
|
487
|
-
end
|
482
|
+
end
|
488
483
|
|
489
484
|
##============================================================##
|
490
485
|
## clean_key Function
|
@@ -510,33 +505,33 @@ module ImmosquareYaml
|
|
510
505
|
key = key[1..-2] if (key.start_with?(DOUBLE_QUOTE) && key.end_with?(DOUBLE_QUOTE)) || (key.start_with?(SIMPLE_QUOTE) && key.end_with?(SIMPLE_QUOTE))
|
511
506
|
|
512
507
|
##============================================================##
|
513
|
-
## Check if the key is an integer
|
508
|
+
## Check if the key is an integer
|
514
509
|
##============================================================##
|
515
510
|
is_int = key =~ /\A[-+]?\d+\z/
|
516
511
|
|
517
512
|
##============================================================##
|
518
|
-
##
|
513
|
+
##
|
519
514
|
## Re-add quotes if the key is in the list of reserved keys or is an integer
|
520
515
|
##============================================================##
|
521
516
|
key = "\"#{key}\"" if RESERVED_KEYS.include?(key) || is_int
|
522
517
|
key
|
523
518
|
end
|
524
|
-
|
519
|
+
|
525
520
|
##============================================================##
|
526
521
|
## clean_value Function
|
527
522
|
## Purpose: Sanitize and standardize YAML values
|
528
523
|
## In YAML "inblock" scenarios, there's no need to add quotes
|
529
524
|
## around values as it's inherently handled.
|
530
525
|
## ============================================================ ##
|
531
|
-
def clean_value(value, with_quotes_verif
|
526
|
+
def clean_value(value, with_quotes_verif: true)
|
532
527
|
##============================================================##
|
533
528
|
## Convert value to string to prevent issues in subsequent operations
|
534
529
|
##============================================================##
|
535
530
|
value = value.to_s
|
536
|
-
|
531
|
+
|
537
532
|
##============================================================##
|
538
533
|
## Remove newline characters at the end of the value if present.
|
539
|
-
## This should be done prior to strip operation to handle scenarios
|
534
|
+
## This should be done prior to strip operation to handle scenarios
|
540
535
|
## where the value ends with a space followed by a newline.
|
541
536
|
###============================================================##
|
542
537
|
value = value[0..-2] if value.end_with?(NEWLINE)
|
@@ -544,7 +539,7 @@ module ImmosquareYaml
|
|
544
539
|
|
545
540
|
##============================================================##
|
546
541
|
## Clean up the value:
|
547
|
-
## - Remove tabs, carriage returns, form feeds, and vertical tabs.
|
542
|
+
## - Remove tabs, carriage returns, form feeds, and vertical tabs.
|
548
543
|
## \t: corresponds to a tab
|
549
544
|
## \r: corresponds to a carriage return
|
550
545
|
## \f: corresponds to a form feed
|
@@ -552,17 +547,17 @@ module ImmosquareYaml
|
|
552
547
|
## We keep the \n
|
553
548
|
##============================================================##
|
554
549
|
value = value.gsub(/[\t\r\f\v]+/, NOTHING)
|
555
|
-
|
550
|
+
|
556
551
|
##============================================================##
|
557
552
|
## Replace multiple spaces with a single space.
|
558
553
|
##============================================================##
|
559
554
|
value = value.gsub(/ {2,}/, SPACE)
|
560
|
-
|
555
|
+
|
561
556
|
##============================================================##
|
562
557
|
## Trim leading and trailing spaces.
|
563
558
|
##============================================================##
|
564
559
|
value = value.strip
|
565
|
-
|
560
|
+
|
566
561
|
##============================================================##
|
567
562
|
## Replace special quotes with standard single quotes.
|
568
563
|
##============================================================##
|
@@ -599,15 +594,15 @@ module ImmosquareYaml
|
|
599
594
|
if value.empty?
|
600
595
|
value = "\"#{value}\""
|
601
596
|
elsif with_quotes_verif == true
|
602
|
-
value = "\"#{value}\"" if value.include?(": ") ||
|
597
|
+
value = "\"#{value}\"" if value.include?(": ") ||
|
603
598
|
value.include?(" #") ||
|
604
|
-
value.include?(NEWLINE) ||
|
605
|
-
value.include?('\n') ||
|
599
|
+
value.include?(NEWLINE) ||
|
600
|
+
value.include?('\n') ||
|
606
601
|
value.start_with?(*YML_SPECIAL_CHARS) ||
|
607
602
|
value.end_with?(":") ||
|
608
603
|
RESERVED_KEYS.include?(value) ||
|
609
|
-
value.start_with?(SPACE) ||
|
610
|
-
value.end_with?(SPACE)
|
604
|
+
value.start_with?(SPACE) ||
|
605
|
+
value.end_with?(SPACE)
|
611
606
|
end
|
612
607
|
value
|
613
608
|
end
|
@@ -616,14 +611,14 @@ module ImmosquareYaml
|
|
616
611
|
## parse_xml Function
|
617
612
|
## Purpose: Parse an XML file into a nested hash representation.
|
618
613
|
##
|
619
|
-
## This method reads through the XML file line by line and creates a
|
614
|
+
## This method reads through the XML file line by line and creates a
|
620
615
|
## nested hash representation based on the structure and content of the XML.
|
621
616
|
##============================================================##
|
622
617
|
def parse_xml(file_path)
|
623
618
|
nested_hash = {}
|
624
619
|
inblock = nil
|
625
620
|
last_keys = []
|
626
|
-
|
621
|
+
|
627
622
|
##============================================================##
|
628
623
|
## We go over each line of the file to create a hash.
|
629
624
|
## We put the multiline blocks in an array to recover
|
@@ -641,20 +636,20 @@ module ImmosquareYaml
|
|
641
636
|
## Check for blank lines (which can be present within multi-line blocks)
|
642
637
|
##============================================================##
|
643
638
|
blank_line = line.gsub(NEWLINE, NOTHING).empty?
|
644
|
-
|
639
|
+
|
645
640
|
##============================================================##
|
646
641
|
## Split the line into key and value.
|
647
642
|
##============================================================##
|
648
643
|
split = line.strip.split(":", 2)
|
649
644
|
key = split[0].to_s.strip
|
650
645
|
inblock = nil if !inblock.nil? && !blank_line && indent_level <= inblock
|
651
|
-
|
652
|
-
|
646
|
+
|
647
|
+
|
653
648
|
##============================================================##
|
654
649
|
## Set the key level based on indentation
|
655
650
|
##============================================================##
|
656
651
|
last_keys = last_keys[0, (blank_line ? inblock + INDENT_SIZE : indent_level) / INDENT_SIZE]
|
657
|
-
|
652
|
+
|
658
653
|
##============================================================##
|
659
654
|
## If inside a multi-line block, append the line to the current key's value
|
660
655
|
##============================================================##
|
@@ -666,7 +661,7 @@ module ImmosquareYaml
|
|
666
661
|
##============================================================##
|
667
662
|
## Handle multi-line key declarations.
|
668
663
|
## We no longer have the >
|
669
|
-
## because it is transformed in the clean_xml into |
|
664
|
+
## because it is transformed in the clean_xml into |
|
670
665
|
##============================================================##
|
671
666
|
elsif line.gsub("#{key}:", NOTHING).strip.start_with?("|")
|
672
667
|
inblock = indent_level
|
@@ -697,7 +692,7 @@ module ImmosquareYaml
|
|
697
692
|
## |4- without newline and indentation of 4
|
698
693
|
##============================================================##
|
699
694
|
deep_transform_values(nested_hash) do |value|
|
700
|
-
if value.is_a?(Array)
|
695
|
+
if value.is_a?(Array)
|
701
696
|
style_type = value[0]
|
702
697
|
indent_supp = style_type.scan(/\d+/).first&.to_i || 0
|
703
698
|
indent_supp = [indent_supp - INDENT_SIZE, 0].max
|
@@ -722,4 +717,4 @@ module ImmosquareYaml
|
|
722
717
|
|
723
718
|
|
724
719
|
end
|
725
|
-
end
|
720
|
+
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
namespace :immosquare_yaml do
|
2
|
-
|
3
2
|
##============================================================##
|
4
3
|
## Function to translate translation files in rails app
|
5
4
|
## rake immosquare_yaml:translate SOURCE_LOCALE=fr
|
@@ -10,15 +9,15 @@ namespace :immosquare_yaml do
|
|
10
9
|
source_locale = ENV.fetch("SOURCE_LOCALE", nil) || "fr"
|
11
10
|
reset_translations = ENV.fetch("RESET_TRANSLATIONS", nil) || false
|
12
11
|
reset_translations = reset_translations == "true"
|
13
|
-
|
12
|
+
|
14
13
|
raise("Please provide a valid locale") if !I18n.available_locales.map(&:to_s).include?(source_locale)
|
15
14
|
raise("Please provide a valid boolean for reset_translations") if ![true, false].include?(reset_translations)
|
16
|
-
|
15
|
+
|
17
16
|
locales = I18n.available_locales.map(&:to_s).reject {|l| l == source_locale }
|
18
17
|
puts("Translating from #{source_locale} to #{locales.join(", ")} with reset_translations=#{reset_translations}")
|
19
18
|
Dir.glob("#{Rails.root}/config/locales/**/*#{source_locale}.yml").each do |file|
|
20
19
|
locales.each do |locale|
|
21
|
-
ImmosquareYaml::Translate.translate(file, locale, :reset_translations => reset_translations)
|
20
|
+
ImmosquareYaml::Translate.translate(file, locale, :reset_translations => reset_translations)
|
22
21
|
end
|
23
22
|
end
|
24
23
|
rescue StandardError => e
|
@@ -35,5 +34,4 @@ namespace :immosquare_yaml do
|
|
35
34
|
ImmosquareYaml.clean(file)
|
36
35
|
end
|
37
36
|
end
|
38
|
-
|
39
|
-
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: immosquare-yaml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- IMMO SQUARE
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-09-
|
11
|
+
date: 2023-09-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: iso-639
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0
|
19
|
+
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: httparty
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0
|
33
|
+
version: '0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0
|
40
|
+
version: '0'
|
41
41
|
description: IMMOSQUARE-YAML is a specialized Ruby gem tailored primarily for parsing
|
42
42
|
and dumping YML translation files, addressing challenges faced with other parsers
|
43
43
|
like interpreting translation keys as booleans, multi-line strings, and more.
|