-
Notifications
You must be signed in to change notification settings - Fork 3
/
Blogsmith Typography Preview.tmCommand
196 lines (179 loc) · 6.07 KB
/
Blogsmith Typography Preview.tmCommand
1
2
3
4
5
6
7
8
9
10
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?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>beforeRunningCommand</key>
<string>nop</string>
<key>command</key>
<string>#!/usr/bin/env ruby -wKU
require 'erb'
SUPPORT = ENV['TM_SUPPORT_PATH']
require SUPPORT + "/lib/web_preview.rb"
# CHANGE: 3/27/09 search for $32.25 with no trailing US, accounts for possible space ($32 US)
# CHANGE: 3/28/09 more robust monetary unit handling, uses ERB (set format in Markdown properties)
def e_sh(str)
str.to_s.gsub(/(?=[^a-zA-Z0-9_.\/\-\x7F-\xFF\n])/, '\\').gsub(/\n/, "'\n'").sub(/^$/, "''")
end
def ts( st )
st.gsub!(/,/,'')
mynum = st.to_s.reverse.scan(/(?:\d*\.)?\d{1,3}-?/).join(',').reverse
dec = mynum.split('.')
mynum = dec[0].to_s + '.' + dec[1].to_s.ljust(2, '0')[0,2] if dec[1]
mynum
end
def strip_html(string,removals)
iterations = string.scan(/<[^>]+>/).length
while iterations > 0
subnum = removals.length
subnum = "0" + subnum.to_s if subnum.to_s.length == 1
string.sub!(/(<[^>]+>)/,"%#{subnum}%")
removals += $1.to_a
iterations -= 1
end
[string,removals]
end
def strip_markdown(string,removals)
iterations = string.scan(/^[A-Za-z][^ \:]+\: .+$/).length
while iterations > 0
subnum = removals.length
subnum = "0" + subnum.to_s if subnum.to_s.length == 1
string.sub!(/(^[A-Za-z][^ \:]+\: .+$)/,"%#{subnum}%")
removals += $1.to_a
iterations -= 1
end
iterations = string.scan(/^\[[^\]]+\]\:.+$/).length
while iterations > 0
subnum = removals.length
subnum = "0" + subnum.to_s if subnum.to_s.length == 1
string.sub!(/(^\[[^\]]+\]\:.+$)/,"%#{subnum}%")
removals += $1.to_a
iterations -= 1
end
iterations = string.scan(/\[[^\]]+\][\[\(][^\]\)]+[\]\)]/).length
while iterations > 0
subnum = removals.length
subnum = "0" + subnum.to_s if subnum.to_s.length == 1
string.sub!(/(\[([^\]]+)\][\[\(][^\]\)]+[\]\)])/,"%#{subnum}\\2%")
removals += $1.to_a
iterations -= 1
end
[string,removals]
end
def restore_replacements(string,removals)
removals.each_with_index {|removal,x|
x = "0" + x.to_s if x.to_s.length == 1
m = string.match(/%#{x}([^%]+)?%/)
next if m.nil?
if m[1].nil?
string.sub!(/%#{x}%/,removals[x.to_i])
else
if removals[x.to_i] =~ /\[[^\]]+\]([\[\(][^\]\)]+[\]\)])/
string.sub!(/%#{x}([^%]+)?%/,"[\\1]#$1")
end
end
}
string
end
string = STDIN.read
original = string.clone
num = ""
removals = []
string,removals = strip_markdown(string,removals)
string,removals = strip_html(string,removals)
string.gsub!(/…/,'...')
string.gsub!(/(\w)(\.{3,}) ?/,"\\1<span class=\"correction\">\s...\s</span>")
string.gsub!(/—/,'<span class=\"correction\">--</span>')
string.gsub!(/(\w)(\-\-)\s?(\w)?/,"\\1<span class=\"correction\">\s--\s</span>\\3")
string.gsub!(/[“”]/,'<span class=\"correction\">"</span>')
string.gsub!(/[‘’]/,'<span class=\"correction\">\'</span>')
string.gsub!(/(['"])([,\.])/,"<span class=\"correction\">\\2\\1</span>")
count = string.scan(/\$([\d\.\,]+)(\s?US)?/).length
newstring = ""
while count > 0
punct = nil
res = string.match(/\$([\d\.\,]+)(\s?US)?/)
amount = res[1]
if amount.match(/\d([^A-Za-z0-9])$/)
punct = $1
end
num = ts(amount.sub(/#{punct}$/,''))
output = ERB.new(ENV['TM_MONEY_FORMAT']).result
output = "<span class=\"corrected\">#{res[0]}</span><span class=\"correction\">" + output + "</span>" unless output == res[0].to_s
output += punct unless punct.nil?
newstring += string[0,res.begin(0)] + output
string = string[res.end(0),string.length]
count -= 1
end
string = newstring + string
newstring = ""
count = string.scan(/(\d[\d\.\,]+)/).length
while count > 0
punct = nil
res = string.match(/(\d[\d\.\,]{3,})/)
break if res.nil?
amount = res[1]
if amount.match(/\d([^A-Za-z0-9])$/)
punct = $1
amount.sub!(/#{punct}$/,'')
end
output = ts(amount)
output = "<span class=\"corrected\">#{res[0]}</span><span class=\"correction\">" + output + "</span>" unless output == res[0].to_s
output += punct unless punct.nil?
newstring += string[0,res.begin(0)] + output
string = string[res.end(0),string.length]
count -= 1
end
string = restore_replacements(newstring + string,removals)
string.gsub!(/^[A-Za-z]+\: ([^\n]+)?\n+/,"")
html = %x{echo #{e_sh string}|"#{ENV['TM_SUPPORT_PATH']}/bin/Markdown.pl"}
html.gsub!(/(<div class="removeme"[^>]+>)?(.*)?(<p([^>]+)?>(.*)?)?<img[^>]+>(<\/p>)?(<\/div>)?/,'')
html_header("Automatic Typography Corrections")
puts <<-HTML
<script src="file://#{ENV['TM_BUNDLE_SUPPORT']}/lib/jquery.js" type="text/javascript" charset="utf-8"></script>
<script src="file://#{ENV['TM_BUNDLE_SUPPORT']}/lib/jqueryui.js" type="text/javascript" charset="utf-8"></script>
<style>
body {color:#333}
a {color:#333}
.corrected {color:#777;text-decoration:line-through;background:#ccc}
.correction {color:red;border-bottom:dotted 1px red;font-weight:bold;background:#fdc}
span {cursor:pointer}
</style>
HTML
print html
puts <<-HTML
<script type="text/javascript" charset="utf-8">
$('a').click(function () {
return false;
});
$('span').click(function () {
if ($(this).hasClass('corrected')) {
$(this).css({'display' : 'none'});
if ($(this).next().hasClass('correction')) {
$(this).next().removeClass('correction');
}
}
if ($(this).hasClass('correction')) {
$(this).removeClass('correction');
if ($(this).prev().hasClass('corrected')) {
$(this).prev().css({'display' : 'none'});
}
}
});
</script>
HTML
html_footer
</string>
<key>fallbackInput</key>
<string>document</string>
<key>input</key>
<string>selection</string>
<key>name</key>
<string>Blogsmith Typography Preview</string>
<key>output</key>
<string>showAsHTML</string>
<key>scope</key>
<string>text.html.markdown, text.html.markdown.multimarkdown</string>
<key>uuid</key>
<string>04F43BDC-92DA-4178-A018-4AFE787F30FC</string>
</dict>
</plist>