Class: SnippetConverter

Inherits:
Object
  • Object
show all
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

Class Method Details

.convert_to_vsc(file) ⇒ Hash

Convert file content to VSC snippet format

Parameters:

  • file (file)

    Snippet file path

Returns:

  • (Hash)

    VSC Json snippet



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

Parameters:

  • snippet_element (REXML::Element)

    Snippet xml element

Returns:

  • (Hash)

    Snippet as a hash



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

Parameters:

  • body (String)

    Sublime snippet content element

Returns:

  • (Array)

    Body as an array



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

.infoObject

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

Parameters:

  • args (Array)

    Array of arguments passed in cli

Returns:

  • (OpenStruct)

    options as a ostruct



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)
  options = OpenStruct.new
  options.encoding = 'utf8'
  options.verbose = false
  options.split = false
  options.output = 'json'
  options.destination = 'snippets'

  opts = OptionParser.new do |op|
    op.banner = "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|
      options.file = f
    end

    # Directory
    op.on('-d', '--directory directory', 'Directory of snippets file to convert') do |d|
      options.directory = d
    end

    # Split
    op.on('-s', '--[no-]split', 'Split output into multiple files') do |s|
      options.split = s
    end

    # Output
    op.on('-o', '--output output', 'Output format (default: json)') do |o|
      options.output = o
    end

    # Destination folder
    op.on('--destination destination', 'Destination directory (default: snippets)') do |dest|
      options.destination = dest
    end

    # Verbose
    op.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
      options.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)
  options
end

.read_snippet(file) ⇒ REXML::Element

Read snippet file and return the first snippet found

Parameters:

  • file (String)

    Snipppet file path

Returns:

  • (REXML::Element)

    First snippet element 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

Parameters:

  • file (String)

    filename

  • snippet (Has)

    snippet hash



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