snippet_converter 0.1.8

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
+ SHA256:
3
+ metadata.gz: 8dc56d98a3f84090a5668ee6707a606f765adb28ad4fb53ccf54c144d46b571f
4
+ data.tar.gz: 93c626dd0f6be3e366606554502a33b5e53dafaf3105a42d3b2be6da862e6614
5
+ SHA512:
6
+ metadata.gz: 648fc64287f5de008ca97377c3e09aa25649e843e1ae6e668d23964ac5c9e05437956a34f1c1fcf631bc65ee177bf8278e53415c6553863d772e889726a4a9e4
7
+ data.tar.gz: 7e41bc607312eab66769d9d97acc433f0a9d6cffa7cbf0c95605f7e875268c9a25a9f97d0199909a843396e63541b49dd905b340e3b527b3ab23252c81e45262
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.vscode
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .ruby-version
14
+ .ruby-gemset
15
+
16
+ # Gems
17
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at jeremie@cubytz.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in snippet_converter.gemspec
6
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ snippet_converter (0.1.7)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.7.0)
12
+ rspec-core (~> 3.7.0)
13
+ rspec-expectations (~> 3.7.0)
14
+ rspec-mocks (~> 3.7.0)
15
+ rspec-core (3.7.1)
16
+ rspec-support (~> 3.7.0)
17
+ rspec-expectations (3.7.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.7.0)
20
+ rspec-mocks (3.7.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-support (3.7.1)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16)
30
+ rake (~> 10.0)
31
+ rspec (~> 3.0)
32
+ snippet_converter!
33
+
34
+ BUNDLED WITH
35
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Jeremie Henri
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,87 @@
1
+ # SnippetConverter
2
+
3
+ [![The MIT License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](http://opensource.org/licenses/MIT)
4
+
5
+ Simple command-line tool to convert Sublime Text Snippets to Visual Studio Code format
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'snippet_converter'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install snippet_converter
22
+
23
+ ## Usage
24
+
25
+ ### Simple conversion
26
+
27
+ Convert a snippet to json
28
+
29
+ ```
30
+ snippet_converter -f PATH_TO_SUBLIME_SNIPPET
31
+ ```
32
+
33
+ This will create a `snippets/snippets.json` file in the current working directory.
34
+
35
+ **Keep original filename**
36
+
37
+ Pass the split `-s` option to keep the original filename.
38
+
39
+ ### Bulk conversion
40
+
41
+ **Convert selected files**
42
+
43
+ Convert multiple files by passing a comma seperated list to the `-f` option
44
+
45
+ ```
46
+ snippet_converter -f 'PATH_TO_FILE,PATH_TO_FILE,...'
47
+ ```
48
+
49
+ **Convert content of directory**
50
+
51
+ Convert a folder by using the `-d` option
52
+
53
+ ```
54
+ snippet_converter -d PATH_TO_DIRECTORY
55
+ ```
56
+
57
+ **Split files**
58
+
59
+ Pass the split `-s` option to split the output in multiple files
60
+
61
+ ### Output directory
62
+
63
+ Set the output directory by passing the `-o` option
64
+
65
+ ```
66
+ snippet_converter -f PATH_TO_SNIPPET_FILE -o PATH_TO_OUTPUT_FOLDER
67
+ ```
68
+
69
+ For all available options, see `-h` or `--help` for details
70
+
71
+ ## Development
72
+
73
+ After checking out the repo, run `rake spec` to run the tests.
74
+
75
+ 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).
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cubytz/snippet_converter. 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.
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
84
+
85
+ ## Code of Conduct
86
+
87
+ Everyone interacting in the SnippetConverter project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/cubytz/snippet_converter/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/ruby -w
2
+ # frozen_string_literal: true
3
+
4
+ require 'snippet_converter'
5
+
6
+ # Parse the options to the converter
7
+ options = SnippetConverter.parse(ARGV)
8
+
9
+ # File or Directory required
10
+ unless options.file || options.directory
11
+ puts 'No file or directory passed'
12
+ exit
13
+ end
14
+
15
+ # Choose file over directory in case both are passed
16
+ if options.file
17
+ if File.directory?(options.file) || !File.file?(options.file)
18
+ puts "#{options.file} is not a snippet file"
19
+ exit
20
+ end
21
+
22
+ files = options.file.split(',')
23
+ elsif options.directory
24
+ if !File.directory?(options.directory) || File.file?(options.directory)
25
+ puts "#{options.directory} is not a directory"
26
+ exit
27
+ end
28
+
29
+ directory = options.directory
30
+ directory += '/' unless options.directory[-1] == '/'
31
+
32
+ files = Dir["#{directory}*.sublime-snippet"]
33
+ end
34
+
35
+ puts "#{files.count} files" if options.verbose
36
+
37
+ # Exit if no file found
38
+ exit unless files.size.positive?
39
+
40
+ # Create the directory
41
+ FileUtils.mkdir_p(options.destination) unless Dir.exist?(options.destination)
42
+
43
+ # Create a snippets hash
44
+ snippets = {}
45
+
46
+ # Loop through the files
47
+ files.each do |file|
48
+ # Convert the files
49
+ snippet = SnippetConverter.convert_to_vsc(file)
50
+ snippets[snippet[:prefix]] = snippet
51
+ if options.verbose
52
+ # puts "### #{snippet[:prefix]} ###"
53
+ # pp snippet.to_json
54
+ end
55
+ end
56
+
57
+ case options.split
58
+ when true
59
+ snippets.each do |key, value|
60
+ snippet = {}
61
+ snippet[key] = value
62
+ filename = "#{options.destination}/#{key}.#{options.output}"
63
+ SnippetConverter.write_file(filename, snippet)
64
+ end
65
+ when false
66
+ filename = "#{options.destination}/snippets.#{options.output}"
67
+ SnippetConverter.write_file(filename, snippets)
68
+ end
69
+
70
+ puts JSON.pretty_generate(snippets) if options.verbose
71
+
72
+ puts 'Done'
@@ -0,0 +1,192 @@
1
+ #!/usr/bin/ruby -w
2
+ # frozen_string_literal: true
3
+
4
+ require 'rexml/document'
5
+ require 'optparse'
6
+ require 'optparse/time'
7
+ require 'ostruct'
8
+ require 'pp'
9
+ require 'json'
10
+ require 'fileutils'
11
+ require 'snippet_converter/version'
12
+
13
+ # The main class
14
+ class SnippetConverter
15
+ include REXML
16
+
17
+ # variable referencing the corresponding
18
+ # keys {Sublime: 'VSC'}
19
+ KEYS = {
20
+ content: 'body',
21
+ tabTrigger: 'prefix',
22
+ scope: 'scope',
23
+ description: 'description'
24
+ }.freeze
25
+
26
+ #
27
+ # Outputs info to the command line
28
+ #
29
+ def self.info
30
+ name = Gem.loaded_specs['snippet_converter'].name
31
+ name = name.split('_').map(&:capitalize).join
32
+ license = Gem.loaded_specs['snippet_converter'].license
33
+ puts ''
34
+ puts "#{name}, version #{VERSION}"
35
+ puts "#{license} License"
36
+ puts 'Copyright (c) 2018 Jeremie Henri - Cubytz'
37
+ puts ''
38
+ end
39
+
40
+ #
41
+ # Parse arguments
42
+ #
43
+ # @param [Array] args Array of arguments passed in cli
44
+ #
45
+ # @return [OpenStruct] options as a ostruct
46
+ #
47
+ def self.parse(args)
48
+ options = OpenStruct.new
49
+ options.encoding = 'utf8'
50
+ options.verbose = false
51
+ options.split = false
52
+ options.output = 'json'
53
+ options.destination = 'snippets'
54
+
55
+ opts = OptionParser.new do |op|
56
+ op.banner = "Usage:\tsnippetconverter.rb [options]\n\tFile(s) or directory required"
57
+
58
+ op.separator ''
59
+ op.separator 'Specific options:'
60
+
61
+ # File
62
+ op.on('-f', '--file file', 'Snippet file(s) to convert') do |f|
63
+ options.file = f
64
+ end
65
+
66
+ # Directory
67
+ op.on('-d', '--directory directory', 'Directory of snippets file to convert') do |d|
68
+ options.directory = d
69
+ end
70
+
71
+ # Split
72
+ op.on('-s', '--[no-]split', 'Split output into multiple files') do |s|
73
+ options.split = s
74
+ end
75
+
76
+ # Output
77
+ op.on('-o', '--output output', 'Output format (default: json)') do |o|
78
+ options.output = o
79
+ end
80
+
81
+ # Destination folder
82
+ op.on('--destination destination', 'Destination directory (default: snippets)') do |dest|
83
+ options.destination = dest
84
+ end
85
+
86
+ # Verbose
87
+ op.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
88
+ options.verbose = v
89
+ end
90
+
91
+ op.separator ''
92
+ op.separator 'Common options:'
93
+
94
+ # Print an options summary.
95
+ op.on_tail('-h', '--help', 'Show this help message') do
96
+ info
97
+ puts opts
98
+ exit
99
+ end
100
+
101
+ # Print the version.
102
+ op.on_tail('--version', 'Show version') do
103
+ puts "SnippetConverter, version #{VERSION}"
104
+ exit
105
+ end
106
+ end
107
+
108
+ opts.parse!(args)
109
+ options
110
+ end
111
+
112
+ #
113
+ # Read snippet file and return the first snippet found
114
+ #
115
+ # @param [String] file Snipppet file path
116
+ #
117
+ # @return [REXML::Element] First snippet element found
118
+ #
119
+ def self.read_snippet(file)
120
+ xmlfile = File.new(file)
121
+ doc = Document.new(xmlfile)
122
+
123
+ # return the snippet element
124
+ XPath.first(doc.root, '//snippet')
125
+ end
126
+
127
+ #
128
+ # Creates a Hash element with VSC keys and
129
+ # formatted body
130
+ #
131
+ # @param [REXML::Element] snippet_element Snippet xml element
132
+ #
133
+ # @return [Hash] Snippet as a hash
134
+ #
135
+ def self.create_json_snippet(snippet_element)
136
+ snippet = {}
137
+ snippet_element.elements.each do |element|
138
+ key = KEYS[element.name.to_sym]
139
+ case key
140
+ when 'body'
141
+ value = SnippetConverter.format_body(element.text)
142
+ else
143
+ value = element.text
144
+ end
145
+
146
+ snippet[key.to_sym] = value
147
+ end
148
+ snippet
149
+ end
150
+
151
+ #
152
+ # Convert file content to VSC snippet format
153
+ #
154
+ # @param [file] file Snippet file path
155
+ #
156
+ # @return [Hash] VSC Json snippet
157
+ #
158
+ def self.convert_to_vsc(file)
159
+ # Get the first snippet element
160
+ snippet_element = read_snippet(file)
161
+ # Convert the snippet to VSC snippet format
162
+ create_json_snippet(snippet_element)
163
+ end
164
+
165
+ #
166
+ # Format the body element
167
+ #
168
+ # @param [String] body Sublime snippet content element
169
+ #
170
+ # @return [Array] Body as an array
171
+ #
172
+ def self.format_body(body)
173
+ # Split by \n
174
+ formatted_body = body.split("\n")
175
+ # Remove any empty element from the array
176
+ formatted_body.delete('')
177
+ formatted_body
178
+ end
179
+
180
+ #
181
+ # Save snippet content to disk
182
+ #
183
+ # @param [String] file filename
184
+ # @param [Has] snippet snippet hash
185
+ #
186
+ def self.write_file(file, snippet)
187
+ File.open(file, 'w') do |f|
188
+ # Pretty generate the JSON before saving
189
+ f.write(JSON.pretty_generate(snippet))
190
+ end
191
+ end
192
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The SnippetConverter main class
4
+ class SnippetConverter
5
+ VERSION = '0.1.8'
6
+ end
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path('lib', __dir__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'snippet_converter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'snippet_converter'
8
+ spec.version = SnippetConverter::VERSION
9
+ spec.authors = ['Jeremie Henri']
10
+ spec.email = ['jeremie@cubytz.com']
11
+
12
+ spec.summary = 'CLI Converter for Sublime Snippets to Visual Studio Code format'
13
+ spec.description = 'Simple command-line tool to convert Sublime Text Snippets to Visual Studio Code format'
14
+ spec.homepage = 'https://github.com/cubytz/SnippetConverter'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'bin'
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.16'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snippet_converter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.8
5
+ platform: ruby
6
+ authors:
7
+ - Jeremie Henri
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-07 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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
+ description: Simple command-line tool to convert Sublime Text Snippets to Visual Studio
56
+ Code format
57
+ email:
58
+ - jeremie@cubytz.com
59
+ executables:
60
+ - snippet_converter
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - bin/snippet_converter
74
+ - lib/snippet_converter.rb
75
+ - lib/snippet_converter/version.rb
76
+ - snippet_converter.gemspec
77
+ homepage: https://github.com/cubytz/SnippetConverter
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.7.6
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: CLI Converter for Sublime Snippets to Visual Studio Code format
101
+ test_files: []