Module: Sqm2Json

Defined in:
lib/sqm2json/sqm2json.rb,
lib/sqm2json/version.rb,
lib/sqm2json/json2sqm.rb

Overview

Provide SQM =>> JSON conversion

Defined Under Namespace

Modules: Reverse

Constant Summary collapse

VERSION =
'0.1.1'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_supported_versionsObject



4
5
6
# File 'lib/sqm2json/version.rb', line 4

def self.get_supported_versions
  [12, 51, 52, 54]
end

.version_supported?(version) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/sqm2json/version.rb', line 8

def self.version_supported?(version)
  get_supported_versions.include?(version)
end

Instance Method Details

#escape_quotes(content) ⇒ Object



42
43
44
# File 'lib/sqm2json/sqm2json.rb', line 42

def escape_quotes(content)
  content.gsub(/\\/){"\\\\\\\\"}.gsub(/"/, '\"')
end

#process_line(line) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sqm2json/sqm2json.rb', line 22

def process_line(line)
  case line
  when /^\s*\w*\s*=\s*".*";\s*$/
    elem = line.split('=', 2).map! { |x| x.lstrip.rstrip.delete_prefix('"').delete_suffix('";') }
    line.gsub!(/(?<key>\w*)\s*=\s*"(?<val>.*)";\s*$/, "\"#{elem[0]}\": \"#{escape_quotes(elem.drop(1).join('='))}\",")
  when /^\s*\w*\s*=\s*.*;\s*$/
    line.gsub!(/(?<key>\w*)\s*=\s*(?<val>\S*);\s*$/, '"\k<key>": \k<val>,')
  when /^\s*[cC]lass\s*\w*/
    line.gsub!(/([cC])lass\s*(\w*)/, '"\1lass_\2":')
  when /^\s*\w*\[\]\s*=\s*\{.*\}[;,]/
    line.gsub!(/(?<key>\w*)\[\]\s*=\s*\{(?<val>[\w#\+\-0-9 .,"]+)\}[;,]/, '"\k<key>": [\k<val>],')

  when /^\s*\w*\[\]\s*=\s*$/
    line.gsub!(/(?<key>\w*)\[\]\s*=/, '"\k<key>": [')
  end

  line.gsub!(/\};\s*$/, '},')
  line
end

#to_json(sqm_document) ⇒ Hash

Parse a given SQM file to JSON document NOTICE: the implementation is far from being perfect but just works

Parameters:

  • sqm_document (String)

    valid and readable mission.sqm file path

Returns:

  • (Hash)

    JSON document



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sqm2json/sqm2json.rb', line 8

def to_json(sqm_document)
  content = sqm_document.gsub(/^[\s\S]*version = (\d\d);/, 'version=\1;')
  content = content.lines.each { |line| process_line(line) }.join

  content = content.delete("\r\n").delete("\t")
  # content.gsub!(/(\w+)\s*\[\]\s*=\s*\{([^}]*)\}[;,]/, '"\1": [\2],')
  content.gsub!(/"(\w+)":\s*\[\{([^\}]*)\}[;,]/, '"\1": [\2],')
  content.gsub!(/,\}/, '}')
  content.gsub!(/\}[;,]\}/, '}}')

  content = "{#{content.chomp('"').chomp(',')}}"
  ::JSON.parse(content, symbolize_names: true)
end