Class: VdfToYAML
- Inherits:
-
Object
- Object
- VdfToYAML
- Defined in:
- lib/vdf-converter/vdf-to-yaml.rb
Constant Summary collapse
- @@indentation_count =
Holds the indentation level
0
Instance Method Summary collapse
-
#parseToFile(input_stream, output_file) ⇒ Object
Takes a stream in (string) and puts out YAML to a file.
-
#sanitizeInput(key) ⇒ Object
deals with inconsistencies from VDF to YAM, issues like characters in the key segment, looks like [:key, “junk”, :value].
Instance Method Details
#parseToFile(input_stream, output_file) ⇒ Object
Takes a stream in (string) and puts out YAML to a file
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/vdf-converter/vdf-to-yaml.rb', line 9 def parseToFile(input_stream, output_file) # Stats for coolness t0 = Time.now # STL scanner for the input stream scanner = StringScanner.new(input_stream); 0 # output file as specified in parameters yaml_file = File.open(output_file, 'w'); 0 while scanner.eos? == false #search for a top level element current_segment = scanner.scan(/\"[a-zA-Z0-9'_ .\-\/#\\,:!]*\"[ \r\n\t]*{/) if current_segment != nil current_segment = current_segment.tr("}{\n\r\t\"", "") yaml_file.write(" "*@@indentation_count + current_segment + ":\n") @@indentation_count = @@indentation_count + 1 end # Search for a Key-Value pair current_segment = scanner.scan(/[\s]*\"[a-zA-Z0-9'_ .\-\/#\\,:!]+\"[\s]*\"[a-zA-Z0-9'_ .\-\/#\\,:!]+\"/) if current_segment != nil current_segment = current_segment.rstrip.lstrip key = current_segment.split(/\"/) key = sanitizeInput(key) yaml_file.write(" "*@@indentation_count + key[0] + ":\t" + key[2] + "\n") end # Search for an end segment current_segment = scanner.scan(/[\s]}/) if current_segment != nil @@indentation_count = @@indentation_count -1 if @@indentation_count <= 0 puts "Reached Root Indentation" end end current_segment = scanner.scan_until(/[\s]/) end puts "Conversion Complete, Time Taken: #{Time.now - t0} Seconds" end |
#sanitizeInput(key) ⇒ Object
deals with inconsistencies from VDF to YAM, issues like characters in the key segment, looks like [:key, “junk”, :value]
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/vdf-converter/vdf-to-yaml.rb', line 57 def sanitizeInput(key) key = key.compact key = key.reject{|element| element.empty?} key[0] = key[0].tr('"', "") key[2] = key[2].tr('"', "") if key[0].include? ":" key[0] = '"' + key[0] + '"' elsif key[0].include? "#" key[0] = '"' + key[0] + '"' end if key[2].include? ":" key[2] = '"' + key[2] + '"' end if key[2][0] == "'" key[2] = '"' + key[2] + '"' end return key end |