immosquare-yaml 0.1.0 → 0.1.1
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.rb +65 -21
- data/lib/version.rb +1 -1
- 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: aef05b6cde6a978367813b253fe4dcc378674aa2b6e0192efe967caaf5babd21
|
4
|
+
data.tar.gz: 1bea9842a643d06132b023cf10c8e1f3624c94d83fdff9d62509017fc6873064
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b067dc205dd3e76680c7b76cc851a7676244fcbfb224865f8fdd7368ea185b7fc7612e282c621144b44c7edde5edb13e0dd7db43baa4d6a62ec9a5aa58bfbd1d
|
7
|
+
data.tar.gz: 226772ab6581607c3d41ba8e3fe0bdb360d12e900a4440eba6554d0eb646d7b518f91794dae055f6ff234a798f74b1f0678e5e908dd36e41170441e28f43884a
|
data/lib/immosquare-yaml.rb
CHANGED
@@ -32,10 +32,23 @@ module ImmosquareYaml
|
|
32
32
|
##============================================================##
|
33
33
|
## Default options
|
34
34
|
##============================================================##
|
35
|
-
options = {
|
35
|
+
options = {
|
36
|
+
:sort => true,
|
37
|
+
:output => file_path
|
38
|
+
}.merge(options)
|
36
39
|
|
37
40
|
begin
|
38
41
|
raise("File not found") if !File.exist?(file_path)
|
42
|
+
|
43
|
+
##===========================================================================##
|
44
|
+
## Setup variables
|
45
|
+
##===========================================================================##
|
46
|
+
output_file_path = options[:output]
|
47
|
+
|
48
|
+
##===========================================================================##
|
49
|
+
## Backup original content for restoration after parsing if necessary
|
50
|
+
##===========================================================================##
|
51
|
+
original_content = File.read(file_path) if output_file_path != file_path
|
39
52
|
|
40
53
|
##===========================================================================##
|
41
54
|
## The cleaning procedure is initialized with a comprehensive clean, transforming
|
@@ -43,10 +56,20 @@ module ImmosquareYaml
|
|
43
56
|
## rewriting it to the YAML file in its cleaned and optionally sorted state.
|
44
57
|
##===========================================================================##
|
45
58
|
clean_yml(file_path)
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
59
|
+
parsed_yml = parse(file_path)
|
60
|
+
parsed_yml = sort_by_key(parsed_yml, options[:sort])
|
61
|
+
parsed_yml = dump(parsed_yml)
|
62
|
+
|
63
|
+
##===========================================================================##
|
64
|
+
## Restore original content if necessary
|
65
|
+
##===========================================================================##
|
66
|
+
File.write(file_path, original_content) if output_file_path != file_path
|
67
|
+
|
68
|
+
##===========================================================================##
|
69
|
+
## Write the cleaned YAML content to the specified output file
|
70
|
+
##===========================================================================##
|
71
|
+
FileUtils.mkdir_p(File.dirname(output_file_path))
|
72
|
+
File.write(output_file_path, parsed_yml)
|
50
73
|
true
|
51
74
|
rescue StandardError => e
|
52
75
|
puts(e.message)
|
@@ -73,10 +96,27 @@ module ImmosquareYaml
|
|
73
96
|
begin
|
74
97
|
raise("File not found") if !File.exist?(file_path)
|
75
98
|
|
99
|
+
##===========================================================================##
|
100
|
+
## Backup original content for restoration after parsing
|
101
|
+
##===========================================================================##
|
102
|
+
original_content = File.read(file_path)
|
103
|
+
|
104
|
+
##===========================================================================##
|
105
|
+
## clean, parse & Sort
|
106
|
+
##===========================================================================##
|
76
107
|
clean_yml(file_path)
|
77
|
-
|
78
|
-
|
79
|
-
|
108
|
+
parsed_xml = parse_xml(file_path)
|
109
|
+
parsed_xml = sort_by_key(parsed_xml, options[:sort]) if options[:sort]
|
110
|
+
|
111
|
+
##===========================================================================##
|
112
|
+
## Restore original content
|
113
|
+
##===========================================================================##
|
114
|
+
File.write(file_path, original_content)
|
115
|
+
|
116
|
+
##===========================================================================##
|
117
|
+
## Return the parsed YAML file
|
118
|
+
##===========================================================================##
|
119
|
+
parsed_xml
|
80
120
|
rescue StandardError => e
|
81
121
|
puts(e.message)
|
82
122
|
false
|
@@ -123,6 +163,12 @@ module ImmosquareYaml
|
|
123
163
|
line += "-" if !value.end_with?(NEWLINE)
|
124
164
|
lines << line
|
125
165
|
|
166
|
+
##============================================================##
|
167
|
+
## Remove quotes surrounding the value if they are present.
|
168
|
+
## They are not necessary in this case after | or |-
|
169
|
+
##============================================================##
|
170
|
+
value = value[1..-2] if (value.start_with?(DOUBLE_QUOTE) && value.end_with?(DOUBLE_QUOTE)) || (value.start_with?(SIMPLE_QUOTE) && value.end_with?(SIMPLE_QUOTE))
|
171
|
+
|
126
172
|
##=============================================================##
|
127
173
|
## We parse on the 2 types of line breaks
|
128
174
|
##=============================================================##
|
@@ -464,7 +510,7 @@ module ImmosquareYaml
|
|
464
510
|
## Re-add quotes if the key is in the list of reserved keys or is an integer
|
465
511
|
##============================================================##
|
466
512
|
key = key.gsub(/\A(['“”‘’"]?)(.*)\1\z/, '\2')
|
467
|
-
key = "\"#{key}\"" if
|
513
|
+
key = "\"#{key}\"" if RESERVED_KEYS.include?(key) || is_int
|
468
514
|
key
|
469
515
|
end
|
470
516
|
|
@@ -528,33 +574,31 @@ module ImmosquareYaml
|
|
528
574
|
##=============================================================##
|
529
575
|
## Handling cases where the value must be surrounded by quotes
|
530
576
|
## if:
|
577
|
+
## management of "" and " ". Not possible to have more spaces
|
578
|
+
## because we have already removed the double spaces
|
579
|
+
## else
|
531
580
|
## value.include?(": ") => key: text with: here
|
532
581
|
## value.include?(" #") => key: text with # here
|
533
582
|
## value.include?(NEWLINE) => key: Line 1\nLine 2\nLine 3
|
534
583
|
## value.include?('\n') => key: Line 1"\n"Line 2"\n"Line 3
|
535
584
|
## value.start_with?(*YML_SPECIAL_CHARS) => key: @text
|
536
585
|
## value.end_with?(":") => key: text:
|
537
|
-
##
|
586
|
+
## RESERVED_KEYS.include?(value) => key: YES
|
538
587
|
## value.start_with?(SPACE) => key: 'text'
|
539
588
|
## value.end_with?(SPACE) => key: text '
|
540
|
-
## else:
|
541
|
-
## management of "" and " ". Not possible to have more spaces
|
542
|
-
## because we have already removed the double spaces
|
543
589
|
##=============================================================##
|
544
|
-
if value.
|
545
|
-
value = "\"#{value}\""
|
590
|
+
if value.empty?
|
591
|
+
value = "\"#{value}\""
|
592
|
+
elsif with_quotes_verif == true
|
593
|
+
value = "\"#{value}\"" if value.include?(": ") ||
|
546
594
|
value.include?(" #") ||
|
547
595
|
value.include?(NEWLINE) ||
|
548
596
|
value.include?('\n') ||
|
549
597
|
value.start_with?(*YML_SPECIAL_CHARS) ||
|
550
598
|
value.end_with?(":") ||
|
551
|
-
|
599
|
+
RESERVED_KEYS.include?(value) ||
|
552
600
|
value.start_with?(SPACE) ||
|
553
|
-
value.end_with?(SPACE)
|
554
|
-
with_quotes_verif == true
|
555
|
-
|
556
|
-
else
|
557
|
-
value = "\"#{value}\""
|
601
|
+
value.end_with?(SPACE)
|
558
602
|
end
|
559
603
|
value
|
560
604
|
end
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- IMMO SQUARE
|
@@ -24,9 +24,9 @@ dependencies:
|
|
24
24
|
- - ">"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 0.2.5
|
27
|
-
description: IMMOSQUARE-YAML is a
|
28
|
-
|
29
|
-
|
27
|
+
description: IMMOSQUARE-YAML is a specialized Ruby gem tailored primarily for parsing
|
28
|
+
and dumping YML translation files, addressing challenges faced with other parsers
|
29
|
+
like interpreting translation keys as booleans, multi-line strings, and more.
|
30
30
|
email:
|
31
31
|
- jules@immosquare.com
|
32
32
|
executables: []
|
@@ -54,8 +54,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '0'
|
56
56
|
requirements: []
|
57
|
-
rubygems_version: 3.
|
57
|
+
rubygems_version: 3.4.13
|
58
58
|
signing_key:
|
59
59
|
specification_version: 4
|
60
|
-
summary: A YAML parser
|
60
|
+
summary: A YAML parser optimized for translation files.
|
61
61
|
test_files: []
|