immosquare-yaml 0.1.16 → 0.1.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/immosquare-yaml/shared_methods.rb +1 -0
- data/lib/immosquare-yaml/version.rb +1 -1
- data/lib/immosquare-yaml.rb +165 -82
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9afa19ff5189bcdfaddb188ecc98ef79a37de47d53ba42946eb0fbcbdd37ddc
|
4
|
+
data.tar.gz: 9daa5ffd7cf997c57d65bc19a067a8d73d9fca89cc43c34a1db91d10d3f6550b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90f7bba1a8418feaad8fa81dc958db7f891ea0d5279f4c0e282b8cc1521fed2935a2b9e92feca9aa961cb0f0ba27918366ae6c6d5f3448bdc6ec3dc9ac466dfb
|
7
|
+
data.tar.gz: f8b61971635c0626e2f3ef0d7e9d2311a2b6c518e5fd0e20e21e69a1a3587b86c0bbb4124c2b337614c5cdcbbfa4c318566d055eae85a4ca8c1b48e9ead42acb
|
@@ -7,6 +7,7 @@ module ImmosquareYaml
|
|
7
7
|
SIMPLE_QUOTE = "'".freeze
|
8
8
|
DOUBLE_QUOTE = '"'.freeze
|
9
9
|
DOUBLE_SIMPLE_QUOTE = "''".freeze
|
10
|
+
CUSTOM_SEPARATOR = "_#_#_".freeze
|
10
11
|
WEIRD_QUOTES_REGEX = /‘|’|“|”|‛|‚|„|‟|#{Regexp.quote(DOUBLE_SIMPLE_QUOTE)}/.freeze
|
11
12
|
YML_SPECIAL_CHARS = ["-", "`", "{", "}", "|", "[", "]", ">", ":", "\"", "'", "*", "=", "%", ",", "!", "?", "&", "#", "@"].freeze
|
12
13
|
RESERVED_KEYS = [
|
data/lib/immosquare-yaml.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "English"
|
2
|
+
require "psych"
|
2
3
|
require "immosquare-extensions"
|
3
4
|
require_relative "immosquare-yaml/configuration"
|
4
5
|
require_relative "immosquare-yaml/shared_methods"
|
@@ -16,9 +17,6 @@ module ImmosquareYaml
|
|
16
17
|
|
17
18
|
class << self
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
20
|
##===========================================================================##
|
23
21
|
## Gem configuration
|
24
22
|
##===========================================================================##
|
@@ -151,6 +149,7 @@ module ImmosquareYaml
|
|
151
149
|
##===========================================================================##
|
152
150
|
File.write(file_path, original_content) if !original_content.nil?
|
153
151
|
puts(e.message)
|
152
|
+
puts(e.backtrace)
|
154
153
|
false
|
155
154
|
end
|
156
155
|
end
|
@@ -215,6 +214,17 @@ module ImmosquareYaml
|
|
215
214
|
when Hash
|
216
215
|
lines << line
|
217
216
|
dump(value, lines, indent + INDENT_SIZE)
|
217
|
+
when Array
|
218
|
+
formated_value = Psych.dump(value)
|
219
|
+
if formated_value == "--- []\n"
|
220
|
+
lines << "#{line} []"
|
221
|
+
else
|
222
|
+
formated_value = formated_value.gsub("---#{NEWLINE}", NOTHING)
|
223
|
+
.split(NEWLINE).map {|l| "#{SPACE * (INDENT_SIZE + indent)}#{l}" }
|
224
|
+
.join(NEWLINE)
|
225
|
+
lines << line
|
226
|
+
lines << formated_value
|
227
|
+
end
|
218
228
|
end
|
219
229
|
end
|
220
230
|
|
@@ -305,7 +315,6 @@ module ImmosquareYaml
|
|
305
315
|
##============================================================##
|
306
316
|
current_line = current_line.rstrip
|
307
317
|
|
308
|
-
|
309
318
|
##===================================================================================#
|
310
319
|
## Detecting blank lines to specially handle the last line within a block;
|
311
320
|
## if we are inside a block or it's the last line, we avoid skipping
|
@@ -457,10 +466,18 @@ module ImmosquareYaml
|
|
457
466
|
## my key: line1 line2 line3
|
458
467
|
##============================================================##
|
459
468
|
elsif split.size < 2
|
460
|
-
|
469
|
+
if current_line.lstrip.start_with?("-")
|
470
|
+
lines << current_line
|
471
|
+
else
|
472
|
+
lines[-1] = (lines[-1] + " #{current_line.lstrip}").gsub(NEWLINE, NOTHING)
|
473
|
+
end
|
461
474
|
##============================================================##
|
462
475
|
## Otherwise we are in the case of a classic line
|
463
|
-
## key: value
|
476
|
+
## key: value
|
477
|
+
## or
|
478
|
+
## key: without value
|
479
|
+
## - key: value (list)
|
480
|
+
## - key: without value (list)
|
464
481
|
##============================================================##
|
465
482
|
else
|
466
483
|
key = clean_key(key)
|
@@ -541,94 +558,129 @@ module ImmosquareYaml
|
|
541
558
|
key
|
542
559
|
end
|
543
560
|
|
561
|
+
##============================================================##
|
562
|
+
## " [apple, orange, 'banana']" => [apple, orange, 'banana']
|
563
|
+
##============================================================##
|
564
|
+
def string_in_array(string)
|
565
|
+
begin
|
566
|
+
string_striped = string.strip
|
567
|
+
string_striped.match(/^\[.*\]$/) ? string_striped[1..-2].split(/,\s?/) : string
|
568
|
+
rescue StandardError
|
569
|
+
string
|
570
|
+
end
|
571
|
+
end
|
572
|
+
|
544
573
|
##============================================================##
|
545
574
|
## clean_value Function
|
546
575
|
## Purpose: Sanitize and standardize YAML values
|
547
576
|
## In YAML "inblock" scenarios, there's no need to add quotes
|
548
577
|
## around values as it's inherently handled.
|
549
578
|
## ============================================================ ##
|
550
|
-
def clean_value(
|
579
|
+
def clean_value(values, with_quotes_verif = true)
|
551
580
|
##============================================================##
|
552
|
-
## Convert
|
581
|
+
## Convert key to array if not
|
582
|
+
## fruits: [apple, orange, 'banana']
|
583
|
+
## demo: "demo"
|
553
584
|
##============================================================##
|
554
|
-
|
585
|
+
is_array = string_in_array(values)
|
586
|
+
values = is_array.instance_of?(String) ? [values] : is_array
|
555
587
|
|
556
|
-
##============================================================##
|
557
|
-
## Remove newline characters at the end of the value if present.
|
558
|
-
## This should be done prior to strip operation to handle scenarios
|
559
|
-
## where the value ends with a space followed by a newline.
|
560
|
-
###============================================================##
|
561
|
-
value = value[0..-2] if value.end_with?(NEWLINE)
|
562
588
|
|
589
|
+
values = values.map do |value|
|
590
|
+
##============================================================##
|
591
|
+
## Convert value to string to prevent issues in subsequent operations
|
592
|
+
##============================================================##
|
593
|
+
value = value.to_s
|
563
594
|
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
## \v: corresponds to a vertical tab
|
571
|
-
## We keep the \n
|
572
|
-
##============================================================##
|
573
|
-
value = value.gsub(/[\t\r\f\v]+/, NOTHING)
|
595
|
+
##============================================================##
|
596
|
+
## Remove newline characters at the end of the value if present.
|
597
|
+
## This should be done prior to strip operation to handle scenarios
|
598
|
+
## where the value ends with a space followed by a newline.
|
599
|
+
###============================================================##
|
600
|
+
value = value[0..-2] if value.end_with?(NEWLINE)
|
574
601
|
|
575
|
-
##============================================================##
|
576
|
-
## Replace multiple spaces with a single space.
|
577
|
-
##============================================================##
|
578
|
-
value = value.gsub(/ {2,}/, SPACE)
|
579
602
|
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
603
|
+
##============================================================##
|
604
|
+
## Clean up the value:
|
605
|
+
## - Remove tabs, carriage returns, form feeds, and vertical tabs.
|
606
|
+
## \t: corresponds to a tab
|
607
|
+
## \r: corresponds to a carriage return
|
608
|
+
## \f: corresponds to a form feed
|
609
|
+
## \v: corresponds to a vertical tab
|
610
|
+
## We keep the \n
|
611
|
+
##============================================================##
|
612
|
+
value = value.gsub(/[\t\r\f\v]+/, NOTHING)
|
584
613
|
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
614
|
+
##============================================================##
|
615
|
+
## Replace multiple spaces with a single space.
|
616
|
+
##============================================================##
|
617
|
+
value = value.gsub(/ {2,}/, SPACE)
|
589
618
|
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
##============================================================##
|
595
|
-
value = value[1..-2] while (value.start_with?(DOUBLE_QUOTE) && value.end_with?(DOUBLE_QUOTE)) || (value.start_with?(SIMPLE_QUOTE) && value.end_with?(SIMPLE_QUOTE))
|
619
|
+
##============================================================##
|
620
|
+
## Trim leading and trailing spaces.
|
621
|
+
##============================================================##
|
622
|
+
value = value.strip
|
596
623
|
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
624
|
+
##============================================================##
|
625
|
+
## Replace special quotes with standard single quotes.
|
626
|
+
##============================================================##
|
627
|
+
value = value.gsub(WEIRD_QUOTES_REGEX, SIMPLE_QUOTE)
|
628
|
+
|
629
|
+
##============================================================##
|
630
|
+
## Remove all quotes surrounding the value if they are present.
|
631
|
+
## They will be re-added later if necessary.
|
632
|
+
## """"value"""" => value
|
633
|
+
##============================================================##
|
634
|
+
value = value[1..-2] while (value.start_with?(DOUBLE_QUOTE) && value.end_with?(DOUBLE_QUOTE)) || (value.start_with?(SIMPLE_QUOTE) && value.end_with?(SIMPLE_QUOTE))
|
635
|
+
|
636
|
+
##============================================================##
|
637
|
+
## Convert emoji representations such as \U0001F600 to their respective emojis.
|
638
|
+
##============================================================##
|
639
|
+
value = value.gsub(/\\U([0-9A-Fa-f]{8})/) { [::Regexp.last_match(1).to_i(16)].pack("U*") }
|
640
|
+
|
641
|
+
##=============================================================##
|
642
|
+
## Handling cases where the value must be surrounded by quotes
|
643
|
+
## if:
|
644
|
+
## management of "" and " ". Not possible to have more spaces
|
645
|
+
## because we have already removed the double spaces
|
646
|
+
## else
|
647
|
+
## value.include?(": ") => key: text with: here
|
648
|
+
## value.include?(" #") => key: text with # here
|
649
|
+
## value.include?(NEWLINE) => key: Line 1\nLine 2\nLine 3
|
650
|
+
## value.include?('\n') => key: Line 1"\n"Line 2"\n"Line 3
|
651
|
+
## value.start_with?(*YML_SPECIAL_CHARS) => key: @text
|
652
|
+
## value.end_with?(":") => key: text:
|
653
|
+
## RESERVED_KEYS.include?(value) => key: YES
|
654
|
+
## value.start_with?(SPACE) => key: 'text'
|
655
|
+
## value.end_with?(SPACE) => key: text '
|
656
|
+
##=============================================================##
|
657
|
+
if value.empty?
|
658
|
+
value = "\"#{value}\""
|
659
|
+
elsif with_quotes_verif == true
|
660
|
+
value = "\"#{value}\"" if value.include?(": ") ||
|
661
|
+
value.include?(" #") ||
|
662
|
+
value.include?(NEWLINE) ||
|
663
|
+
value.include?('\n') ||
|
664
|
+
value.start_with?(*YML_SPECIAL_CHARS) ||
|
665
|
+
value.end_with?(":") ||
|
666
|
+
(is_array ? false : RESERVED_KEYS.include?(value)) ||
|
667
|
+
value.start_with?(SPACE) ||
|
668
|
+
value.end_with?(SPACE)
|
669
|
+
end
|
670
|
+
value
|
671
|
+
end
|
672
|
+
is_array.instance_of?(String) ? values.first : "[#{values.join(", ")}]"
|
673
|
+
end
|
674
|
+
|
675
|
+
##============================================================##
|
676
|
+
## Normalize indentation for array values without intent
|
677
|
+
## for the first level.
|
678
|
+
##============================================================##
|
679
|
+
def normalize_indentation(lines)
|
680
|
+
initial_indentation = lines.first.match(/^(\s*)/)[1].length
|
681
|
+
lines.map do |line|
|
682
|
+
line[initial_indentation..(line.end_with?(NEWLINE) ? -2 : -1)]
|
630
683
|
end
|
631
|
-
value
|
632
684
|
end
|
633
685
|
|
634
686
|
##============================================================##
|
@@ -641,8 +693,11 @@ module ImmosquareYaml
|
|
641
693
|
def parse_xml(file_path)
|
642
694
|
nested_hash = {}
|
643
695
|
inblock = nil
|
696
|
+
inlist = nil
|
697
|
+
inlist_data = nil
|
644
698
|
last_keys = []
|
645
699
|
|
700
|
+
|
646
701
|
##============================================================##
|
647
702
|
## We go over each line of the file to create a hash.
|
648
703
|
## We put the multiline blocks in an array to recover
|
@@ -669,11 +724,34 @@ module ImmosquareYaml
|
|
669
724
|
inblock = nil if !inblock.nil? && !blank_line && indent_level <= inblock
|
670
725
|
|
671
726
|
|
727
|
+
##============================================================##
|
728
|
+
## inlist Enter
|
729
|
+
##============================================================##
|
730
|
+
if inlist.nil? && !blank_line && line.strip.start_with?("-")
|
731
|
+
inlist = indent_level
|
732
|
+
inlist_data = []
|
733
|
+
end
|
734
|
+
|
735
|
+
##============================================================##
|
736
|
+
## inlist Exit
|
737
|
+
## We use Pscyh to parse the yaml of the list content
|
738
|
+
##============================================================##
|
739
|
+
if !inlist.nil? && !blank_line && indent_level < inlist
|
740
|
+
yaml = normalize_indentation(inlist_data).join(NEWLINE)
|
741
|
+
current_key = last_keys.last
|
742
|
+
parent_keys = last_keys[0..-2]
|
743
|
+
result = parent_keys.reduce(nested_hash) {|hash, k| hash[k] }
|
744
|
+
result[current_key] = Psych.safe_load(yaml, :permitted_classes => [Date])
|
745
|
+
inlist = nil
|
746
|
+
inlist_data = []
|
747
|
+
end
|
748
|
+
|
672
749
|
##============================================================##
|
673
750
|
## Set the key level based on indentation
|
674
751
|
##============================================================##
|
675
752
|
last_keys = last_keys[0, (blank_line ? inblock + INDENT_SIZE : indent_level) / INDENT_SIZE]
|
676
753
|
|
754
|
+
|
677
755
|
##============================================================##
|
678
756
|
## If inside a multi-line block, append the line to the current key's value
|
679
757
|
##============================================================##
|
@@ -683,6 +761,11 @@ module ImmosquareYaml
|
|
683
761
|
result = parent_keys.reduce(nested_hash) {|hash, k| hash[k] }
|
684
762
|
result[current_key][1] << line.strip
|
685
763
|
##============================================================##
|
764
|
+
## Handle list declarations.
|
765
|
+
##============================================================##
|
766
|
+
elsif !inlist.nil?
|
767
|
+
inlist_data << line
|
768
|
+
##============================================================##
|
686
769
|
## Handle multi-line key declarations.
|
687
770
|
## We no longer have the >
|
688
771
|
## because it is transformed in the clean_xml into |
|
@@ -691,7 +774,7 @@ module ImmosquareYaml
|
|
691
774
|
inblock = indent_level
|
692
775
|
block_type = line.gsub("#{key}:", NOTHING).strip
|
693
776
|
result = last_keys.reduce(nested_hash) {|hash, k| hash[k] }
|
694
|
-
result[key] = [block_type, []]
|
777
|
+
result[key] = ["#{CUSTOM_SEPARATOR}#{block_type}#{CUSTOM_SEPARATOR}", []]
|
695
778
|
last_keys << key
|
696
779
|
##============================================================##
|
697
780
|
## Handle regular key-value pair declarations
|
@@ -703,11 +786,12 @@ module ImmosquareYaml
|
|
703
786
|
result[key] = {}
|
704
787
|
last_keys << key
|
705
788
|
else
|
706
|
-
result[key] = value.strip == "null" ? nil : value
|
789
|
+
result[key] = value.strip == "null" ? nil : string_in_array(value)
|
707
790
|
end
|
708
791
|
end
|
709
792
|
end
|
710
793
|
|
794
|
+
|
711
795
|
##============================================================##
|
712
796
|
## We go over each value then we process if it is a has
|
713
797
|
## | with final newline
|
@@ -716,14 +800,13 @@ module ImmosquareYaml
|
|
716
800
|
## |4- without newline and indentation of 4
|
717
801
|
##============================================================##
|
718
802
|
deep_transform_values(nested_hash) do |value|
|
719
|
-
if value.is_a?(Array)
|
720
|
-
style_type = value[0]
|
803
|
+
if value.is_a?(Array) && !value[0].nil? && value[0].instance_of?(String) && value[0].start_with?(CUSTOM_SEPARATOR) && value[0].end_with?(CUSTOM_SEPARATOR)
|
804
|
+
style_type = value[0].gsub(CUSTOM_SEPARATOR, NOTHING)
|
721
805
|
indent_supp = style_type.scan(/\d+/).first&.to_i || 0
|
722
806
|
indent_supp = [indent_supp - INDENT_SIZE, 0].max
|
723
807
|
value[1] = value[1].map {|l| "#{SPACE * indent_supp}#{l}" }
|
724
808
|
text = value[1].join(NEWLINE)
|
725
809
|
modifier = style_type[-1]
|
726
|
-
|
727
810
|
case modifier
|
728
811
|
when "+"
|
729
812
|
text << NEWLINE unless text.end_with?(NEWLINE)
|
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.17
|
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-10-
|
11
|
+
date: 2023-10-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -33,7 +33,7 @@ dependencies:
|
|
33
33
|
version: '0'
|
34
34
|
- - ">="
|
35
35
|
- !ruby/object:Gem::Version
|
36
|
-
version: 0.1.
|
36
|
+
version: 0.1.11
|
37
37
|
type: :runtime
|
38
38
|
prerelease: false
|
39
39
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: 0.1.
|
46
|
+
version: 0.1.11
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: iso-639
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|