module Sqm2Json

Constants

VERSION

Public Class Methods

get_supported_versions() click to toggle source
# File lib/sqm2json/version.rb, line 4
def self.get_supported_versions
  [12,51,52]
end
is_version_supported?(version) click to toggle source
# File lib/sqm2json/version.rb, line 8
def self.is_version_supported?(version)
  get_supported_versions.include?(version)
end

Public Instance Methods

to_json(sqm_document) click to toggle source

Parse a given SQM file to JSON document NOTICE: the implementation is far from being perfect but just works @param [String] sqm_document valid and readable mission.sqm file path @return [Hash] JSON document

# File lib/sqm2json/sqm2json.rb, line 7
def to_json(sqm_document)
  content = sqm_document.delete("\r\n").delete("\t")

  content.gsub!(/(?<key>\w*)(\s)*=(\s)*(?<val>"");/, '\k<key>="ʉ";') # replace empty string values
  content.gsub!('""', '\"') # 2x" in init fields replaced by \"

  content.gsub!(/(?<key>[\w]+)(\[\])?=(?<val>".{0,}?([^\\]\";))/) { |m|
    pairs = m.split('=', 2)
    "\"#{pairs[0]}\": #{pairs[1].gsub(/;/,'ʊ').gsub(/,/,'ʎ').chomp('ʊ').gsub('""','\"').gsub(/\\([^"])/, '\\\\\\\\\1')},"
  }

  content.gsub!(/class (?<val>\w+)\s*\{/, '"\k<val>" : {')
  content.gsub!(/(?<key>\w*)=(?<val>[\w#+\-0-9 .,]+);/, '"\k<key>" : \k<val>,')
  content.gsub!(/(?<key>\w*)\[\]\s*=\s*\{(?<val>[\w#\+\-0-9 .,"]+)\};/, '"\k<key>" : [\k<val>],')
  content.gsub!(/\};/, '},')
  content.gsub!(/,\}/, '}')
  content.gsub!(/\}[;,]\}/, '}}')
  content.gsub!(/ʊ/, ';') if content.include? 'ʊ'
  content.gsub!(/ʎ/, ',') if content.include? 'ʎ'
  content.gsub!(/ʉ/, '') if content.include? 'ʉ'
  content.gsub!(/ɣ/, '\'') if content.include? 'ɣ'
  content = "{#{content.chomp('"').chomp(',')}}"

  ::JSON.parse(content, symbolize_names: true)
end