[英]Parse TextMate snippet with Nokogiri
A TextMate snippet (.tmSnippet) usually looks something like this, whereas some key/string-pairs are optional and can be at any position. 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>
I'm trying to parse this using Nokogiri, but since the tags are all <key>
and <string>
and the position of each key/string-pair can alter, I'm not sure how to do that. 我正在尝试使用Nokogiri来解析它,但是由于标签都是<key>
和<string>
并且每个键/字符串对的位置都可以改变,所以我不确定该怎么做。 I'm after the scope
, the tabTrigger
, content
and name
. 我关注scope
, tabTrigger
, content
和name
。
Assuming the sub-nodes of a dict
node are just key
- string
pairs, this: 假设dict
节点的子节点只是key
- string
对,则:
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. 否则,在查看节点类型之前,您需要有关节点类型的更多逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.