Class: SnippetConverter
- Inherits:
-
Object
- Object
- SnippetConverter
- Includes:
- REXML
- Defined in:
- lib/snippet_converter.rb,
lib/snippet_converter/version.rb
Overview
The SnippetConverter main class
Constant Summary collapse
- KEYS =
variable referencing the corresponding keys ‘VSC’
{ content: 'body', tabTrigger: 'prefix', scope: 'scope', description: 'description' }.freeze
- VERSION =
'0.1.8'
Class Method Summary collapse
-
.convert_to_vsc(file) ⇒ Hash
Convert file content to VSC snippet format.
-
.create_json_snippet(snippet_element) ⇒ Hash
Creates a Hash element with VSC keys and formatted body.
-
.format_body(body) ⇒ Array
Format the body element.
-
.info ⇒ Object
Outputs info to the command line.
-
.parse(args) ⇒ OpenStruct
Parse arguments.
-
.read_snippet(file) ⇒ REXML::Element
Read snippet file and return the first snippet found.
-
.write_file(file, snippet) ⇒ Object
Save snippet content to disk.
Class Method Details
.convert_to_vsc(file) ⇒ Hash
Convert file content to VSC snippet format
158 159 160 161 162 163 |
# File 'lib/snippet_converter.rb', line 158 def self.convert_to_vsc(file) # Get the first snippet element snippet_element = read_snippet(file) # Convert the snippet to VSC snippet format create_json_snippet(snippet_element) end |
.create_json_snippet(snippet_element) ⇒ Hash
Creates a Hash element with VSC keys and formatted body
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/snippet_converter.rb', line 135 def self.create_json_snippet(snippet_element) snippet = {} snippet_element.elements.each do |element| key = KEYS[element.name.to_sym] case key when 'body' value = SnippetConverter.format_body(element.text) else value = element.text end snippet[key.to_sym] = value end snippet end |
.format_body(body) ⇒ Array
Format the body element
172 173 174 175 176 177 178 |
# File 'lib/snippet_converter.rb', line 172 def self.format_body(body) # Split by \n formatted_body = body.split("\n") # Remove any empty element from the array formatted_body.delete('') formatted_body end |
.info ⇒ Object
Outputs info to the command line
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/snippet_converter.rb', line 29 def self.info name = Gem.loaded_specs['snippet_converter'].name name = name.split('_').map(&:capitalize).join license = Gem.loaded_specs['snippet_converter'].license puts '' puts "#{name}, version #{VERSION}" puts "#{license} License" puts 'Copyright (c) 2018 Jeremie Henri - Cubytz' puts '' end |
.parse(args) ⇒ OpenStruct
Parse arguments
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/snippet_converter.rb', line 47 def self.parse(args) = OpenStruct.new .encoding = 'utf8' .verbose = false .split = false .output = 'json' .destination = 'snippets' opts = OptionParser.new do |op| op. = "Usage:\tsnippetconverter.rb [options]\n\tFile(s) or directory required" op.separator '' op.separator 'Specific options:' # File op.on('-f', '--file file', 'Snippet file(s) to convert') do |f| .file = f end # Directory op.on('-d', '--directory directory', 'Directory of snippets file to convert') do |d| .directory = d end # Split op.on('-s', '--[no-]split', 'Split output into multiple files') do |s| .split = s end # Output op.on('-o', '--output output', 'Output format (default: json)') do |o| .output = o end # Destination folder op.on('--destination destination', 'Destination directory (default: snippets)') do |dest| .destination = dest end # Verbose op.on('-v', '--[no-]verbose', 'Run verbosely') do |v| .verbose = v end op.separator '' op.separator 'Common options:' # Print an options summary. op.on_tail('-h', '--help', 'Show this help message') do info puts opts exit end # Print the version. op.on_tail('--version', 'Show version') do puts "SnippetConverter, version #{VERSION}" exit end end opts.parse!(args) end |
.read_snippet(file) ⇒ REXML::Element
Read snippet file and return the first snippet found
119 120 121 122 123 124 125 |
# File 'lib/snippet_converter.rb', line 119 def self.read_snippet(file) xmlfile = File.new(file) doc = Document.new(xmlfile) # return the snippet element XPath.first(doc.root, '//snippet') end |
.write_file(file, snippet) ⇒ Object
Save snippet content to disk
186 187 188 189 190 191 |
# File 'lib/snippet_converter.rb', line 186 def self.write_file(file, snippet) File.open(file, 'w') do |f| # Pretty generate the JSON before saving f.write(JSON.pretty_generate(snippet)) end end |