Class: SublimeDSL::TextMate::Snippet::XMLReader

Inherits:
Importer
  • Object
show all
Defined in:
lib/sublime_dsl/textmate/snippet.rb

Overview

Sublime Text format importer

Instance Attribute Summary

Attributes inherited from Importer

#file, #snippet

Instance Method Summary collapse

Methods inherited from Importer

for, #initialize

Constructor Details

This class inherits a constructor from SublimeDSL::TextMate::Snippet::Importer

Instance Method Details

#loadObject



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/sublime_dsl/textmate/snippet.rb', line 231

def load
  snippet.file_format = :sublime_text

  doc = File.open(file, 'r:utf-8') { |f| Tools::XML.load(f) }
  # <snippet>
  #     <content><![CDATA[all? { |${1:e}| $0 }]]></content>
  #     <tabTrigger>all</tabTrigger>
  #     <scope>source.ruby</scope>
  #     <description>all? { |e| .. }</description>
  # </snippet>
  nodes = doc.children.reject { |n| n.comment? }
  root = nodes.first
  nodes.length == 1 && root.name == 'snippet' or
    raise Error, "#{file}: invalid root, expected <snippet>"

  root.children.each do |node|
    node.attributes.empty? or
      raise Error, "#{file}: unexpected attributes for #{node.name}: #{node.attributes.inspect}"
    next if node.children.empty?
    node.children.length == 1 && (node.children.first.text? || node.children.first.cdata?) or
      raise Error, "#{file}: unexpected children for #{node.name}: #{node.children.inspect}"
    if node.name == 'description'
      snippet.name = node.text
    else
      method = Snippet.to_snake_map[node.name]
      if method
        snippet.send "#{method}=", node.text
      else
        warn "snippet attribute '#{node.name}' ignored: " << file
      end
    end
  end

end