11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
|
# File 'lib/clips2snippets/cli.rb', line 11
def gen(srcfile)
unless srcfile
puts "Usage: clips2snippets [filename.clips]"
exit
end
Dir.mktmpdir do |tmpdir|
clipsfile = File.join(tmpdir, 'clips')
system "plutil -convert xml1 '#{File.expand_path(srcfile)}' -o '#{clipsfile}'"
plist = Plist::parse_xml(clipsfile)
objects = plist["$objects"]
clips_title = objects[2]
indexes = []
objects.each_with_index do |e, n|
if e.class == Hash && e.keys.include?("title")
indexes << n
end
end
indexes.each_with_index do |index, n|
eor = (indexes[n+1])? indexes[n+1].to_i - 3 : objects.size
title = objects[index+1]
code = objects[index+2]
trigger = objects[index+3]
puts "Found Clip: #{title}"
ranges = []
placeholders = objects[index+4, eor]
placeholders.each_with_index do |ph, i|
if ph.class == Hash && ph.has_key?("range")
range = placeholders[i+1].scan(/\{(\d+), (\d+)\}/)[0].map{|s| s.to_i}
ranges << [range, ph["type"]]
end
end
curr = ranges.size
ranges.reverse.each do |range, type|
case type
when 2
code[range[0], range[1]] = "${#{curr}:$TM_FILENAME}"
when 7
code[range[0], range[1]] = "${#{curr}:$TM_FULLNAME}"
when 10
code[range[0], range[1]] = "${#{curr}:$SELECTION}"
when 12
code[range[0], range[1]] = "${#{curr}:#{code[range[0], range[1]]}}"
else
code[range[0], range[1]] = "${#{curr}}"
end
curr -= 1
end
save_dir = "Snippets - #{clips_title}"
Dir.mkdir save_dir unless FileTest.exists? save_dir
st2_snippet = "<snippet>\n <content><![CDATA[\n%s\n]]></content>\n <tabTrigger>%s</tabTrigger>\n <description>%s</description>\n <!-- <scope>[add here]</scope> -->\n</snippet>"
snippet = st2_snippet % [code, trigger, title]
open(File.join(save_dir, "#{title}.sublime-snippet"), "w").puts snippet
end
end
say "Finished converting all of Clips!", :green
end
|