class SublimeDSL::TextMate::Snippet::XMLReader

Sublime Text format importer

Public Instance Methods

load() click to toggle source
# 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