TextMate片段(.tmSnippet)通常看起来像这样,而一些键/字符串对是可选的,可以在任何位置。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>content</key>
<string>${1:the actual snippet}</string>
<key>tabTrigger</key>
<string>my_trigger</string>
<key>name</key>
<string>This is my Snippet's name</string>
<key>scope</key>
<string>source.js</string>
<key>uuid</key>
<string>6C2985F1-9BB8-43D7-A85C-1006B2932A0D</string>
</dict>
</plist>
我尝试使用Nokogiri解析此问题,但由于标记全部为<key>
和<string>
,并且每个键/字符串对的位置都可以改变,因此我可以使用不知道该怎么做。我在scope
,tabTrigger
,content
和name
之后。
答案 0 :(得分:1)
Assuming the sub-nodes of a dict
node are just key
-string
pairs, this:
require 'nokogiri'
kws = %w{ scope tabTrigger content name }
doc = Nokogiri::XML(File.read('a.tmsnippet'))
doc.xpath('//dict').each do | dict_node |
dict_node.element_children.map(&:content).each_slice(2) do | k, v |
next unless kws.include? k
puts "#{k} -> #{v}"
end
end
produces
"content -> ${1:the actual snippet}
tabTrigger -> my_trigger
name -> This is my Snippet's name
scope -> source.js"
Otherwise you need some more logic on the node types before looking at their content.