sqm2json 0.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 191066f61562113d393a00ecaec6e6cb9c1932a8
4
+ data.tar.gz: 74e38e30f44c7680e2dde4b7810082c9868c54b7
5
+ SHA512:
6
+ metadata.gz: 1fe57226eec5ef80f57d71ef9e3a9cdfc41cee0cabbeac5ebac37ba183ea3881b02cd43f7d98e3969a0ca505e2804ede2ef0eb2a5f5349f1d81756f45bd81977
7
+ data.tar.gz: 7f340ffbd0a1dcd21785324d39d3b27d78e963d97d8c29c96882bdc3a1b5d30ad82b0c678b77dca3c39de6d84c6184e7dcacb67986b51da966b7cac7a91d4589
@@ -0,0 +1,6 @@
1
+ **/*~
2
+ doc/
3
+ .idea/
4
+ .yardoc/
5
+ Gemfile.lock
6
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sqm2json.gemspec
4
+ gemspec
@@ -0,0 +1,13 @@
1
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ Version 2, December 2004
3
+
4
+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
5
+
6
+ Everyone is permitted to copy and distribute verbatim or modified
7
+ copies of this license document, and changing it is allowed as long
8
+ as the name is changed.
9
+
10
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12
+
13
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
@@ -0,0 +1,36 @@
1
+ # Sqm2Json
2
+
3
+ This gem can convert SQM mission files from Arma3 to JSON documents. And vice versa. Missions content can be either parsed easily, and generated from standard JSON representation.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sqm2json'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install sqm2json
20
+
21
+ ## Usage
22
+
23
+ This library embeds a single module called **Sqm2Json**: include or extends it, then you can play with it:
24
+
25
+
26
+
27
+
28
+ ## Development
29
+
30
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
31
+
32
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/arma3. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
@@ -0,0 +1,14 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'yard'
4
+
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+
9
+ YARD::Rake::YardocTask.new(:yard) do |t|
10
+ t.files = ['lib/**/*.rb']
11
+ end
12
+
13
+
14
+ task :default => [:spec, :yard]
@@ -0,0 +1,4 @@
1
+ require 'json'
2
+ require 'sqm2json/json2sqm'
3
+ require 'sqm2json/sqm2json'
4
+ require 'sqm2json/version'
@@ -0,0 +1,92 @@
1
+ module Sqm2Json
2
+ module Reverse
3
+
4
+ # Generate a SQM formatted output from given JSON document
5
+ # @param [Hash] json valid JSON document
6
+ # @return [String] SQM document as string
7
+ def to_sqm(json)
8
+ content = ''
9
+ json.each { |k,v|
10
+ content += get_element(k, v, 0)
11
+ }
12
+ content
13
+ end
14
+
15
+
16
+ # Convert any JSON element to SQM equivalent
17
+ # @param [Object] key of JSON element
18
+ # @param [Object] value of JSON element
19
+ # @param [Integer] level of the element in the whole JSON tree. (root is level 0)
20
+ # @return [String] SQM formatted element
21
+ def get_element(key, value, level)
22
+ content = ''
23
+ level.times { content << "\t"}
24
+
25
+ if value.is_a?(::Numeric)
26
+ content << "#{key.to_s}=#{get_numeric(value)};\r\n"
27
+ elsif value.is_a?(::String)
28
+ content << "#{key.to_s}=\"#{value.gsub(/"/, '""').to_s}\";\r\n"
29
+ elsif value.is_a?(::Array)
30
+ content << get_array(key, value, level)
31
+ elsif value.is_a?(::Hash)
32
+ content << "class #{key.to_s}\r\n"
33
+ level.times { content << "\t"}
34
+ content << "{\r\n"
35
+ value.each{ |k,v|
36
+ content << get_element(k, v, level + 1)
37
+ }
38
+ level.times { content << "\t"}
39
+ content << "};\r\n"
40
+ end
41
+ content
42
+ end
43
+
44
+
45
+ # Convert a JSON value array in SQM equivalent
46
+ # @param [Object] key of JSON element
47
+ # @param [Array] values of JSON element
48
+ # @param [Integer] level of the element in the whole JSON tree. (root is level 0)
49
+ # @return [String] SQM formatted array
50
+ def get_array(key, values, level)
51
+ content = ''
52
+ content << "#{key.to_s}[]="
53
+ if values[0].is_a?(::Numeric)
54
+ content << '{'
55
+ values.each { |v|
56
+ content << "#{get_numeric(v)},"
57
+ }
58
+ content.chomp!(',')
59
+ content << "};\r\n"
60
+ else
61
+ content << "\r\n"
62
+ level.times { content << "\t"}
63
+ content << "{\r\n"
64
+ values.each { |v|
65
+ (level + 1).times {content << "\t"}
66
+ content << "\"#{v.to_s}\",\r\n"
67
+ }
68
+ content.chomp!(",\r\n")
69
+ content << "\r\n"
70
+ level.times { content << "\t"}
71
+ content << "};\r\n"
72
+ end
73
+ content
74
+ end
75
+
76
+
77
+ # Convert a JSON numeric value in SQM equivalent
78
+ # @param [Object] value of JSON element
79
+ # @return [String] SQM formatted numeric value
80
+ def get_numeric(value)
81
+ value.to_s.gsub(/(?<val>[0-9\.]+e-?)(?<exp>[0-9]+)/) { |m|
82
+ arr = m.split(/e/)
83
+ if arr[1][0] =~ /[0-9]/
84
+ "#{arr[0]}e#{arr[1].rjust(3,'0')}"
85
+ else
86
+ "#{arr[0]}e#{arr[1][0]}#{arr[1][1..-1].rjust(3,'0')}"
87
+ end
88
+ }.to_s
89
+ end
90
+
91
+ end
92
+ end
@@ -0,0 +1,33 @@
1
+ module Sqm2Json
2
+
3
+ # Parse a given SQM file to JSON document
4
+ # NOTICE: the implementation is far from being perfect but just works
5
+ # @param [String] sqm_document valid and readable mission.sqm file path
6
+ # @return [Hash] JSON document
7
+ def to_json(sqm_document)
8
+ content = sqm_document.delete("\r\n").delete("\t")
9
+
10
+ content.gsub!(/(?<key>\w*)(\s)*=(\s)*(?<val>"");/, '\k<key>="ʉ";') # replace empty string values
11
+ content.gsub!('""', '\"') # 2x" in init fields replaced by \"
12
+
13
+ content.gsub!(/(?<key>[\w]+)(\[\])?=(?<val>".{0,}?([^\\]\";))/) { |m|
14
+ pairs = m.split('=')
15
+ puts pairs[1].gsub(/;/,'ʊ').gsub(/,/,'ʎ').chomp('ʊ').gsub('""','\"') if pairs[1].include? '""'
16
+ "\"#{pairs[0]}\": #{pairs[1].gsub(/;/,'ʊ').gsub(/,/,'ʎ').chomp('ʊ').gsub('""','\"')},"
17
+ }
18
+
19
+ content.gsub!(/class (?<val>\w+)\s*\{/, '"\k<val>" : {')
20
+ content.gsub!(/(?<key>\w*)=(?<val>[\w#+\-0-9 .,]+);/, '"\k<key>" : \k<val>,')
21
+ content.gsub!(/(?<key>\w*)\[\]\s*=\s*\{(?<val>[\w#\+\-0-9 .,"]+)\};/, '"\k<key>" : [\k<val>],')
22
+ content.gsub!(/\};/, '},')
23
+ content.gsub!(/,\}/, '}')
24
+ content.gsub!(/\}[;,]\}/, '}}')
25
+ content.gsub!(/ʊ/, ';') if content.include? 'ʊ'
26
+ content.gsub!(/ʎ/, ',') if content.include? 'ʎ'
27
+ content.gsub!(/ʉ/, '') if content.include? 'ʉ'
28
+ content = "{#{content.chomp('"').chomp(',')}}"
29
+
30
+ ::JSON.parse(content, symbolize_names: true)
31
+ end
32
+
33
+ end
@@ -0,0 +1,3 @@
1
+ module Sqm2Json
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sqm2json/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sqm2json'
8
+ spec.version = Sqm2Json::VERSION
9
+ spec.authors = ['Nicolas FLINOIS']
10
+ spec.email = ['nicolas.flinois@gmail.com']
11
+ spec.license = 'WTFPL v2'
12
+
13
+ spec.summary = %q{This library aims at converting Arma's missions SQM format to JSON and vice-versa}
14
+ spec.description = %q{The conversion of SQM format to JSON document and vice versa makes missions parsing easier: you can either browse your mission content, or modify it and generate a new valid mission.sqm file}
15
+ spec.homepage = 'https://github.com/nicolasFlinois/sqm2json'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.11'
21
+ spec.add_development_dependency 'rake', '~> 10.0'
22
+ spec.add_development_dependency 'rspec', '~> 3.0'
23
+ spec.add_development_dependency 'yard', '~> 0.8.7.6'
24
+
25
+ end
26
+
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sqm2json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas FLINOIS
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.7.6
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.8.7.6
69
+ description: 'The conversion of SQM format to JSON document and vice versa makes missions
70
+ parsing easier: you can either browse your mission content, or modify it and generate
71
+ a new valid mission.sqm file'
72
+ email:
73
+ - nicolas.flinois@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - Gemfile
80
+ - LICENSE.md
81
+ - README.md
82
+ - Rakefile
83
+ - lib/sqm2json.rb
84
+ - lib/sqm2json/json2sqm.rb
85
+ - lib/sqm2json/sqm2json.rb
86
+ - lib/sqm2json/version.rb
87
+ - sqm2json.gemspec
88
+ homepage: https://github.com/nicolasFlinois/sqm2json
89
+ licenses:
90
+ - WTFPL v2
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.5.1
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: This library aims at converting Arma's missions SQM format to JSON and vice-versa
112
+ test_files: []
113
+ has_rdoc: