-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatex_replace_quotes.py
286 lines (212 loc) · 8.46 KB
/
latex_replace_quotes.py
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# -*- coding: utf-8 -*-
#
# Plugin to replace ``blah'' with \enquote{blah}
# and `blah' with \squote{blah}
#
# Original Author: Joshua Leung
# Date: April 2017
import sublime
import sublime_plugin
######################################
# Highlight Region Identifiers (for LatexQuotesToEnquotePreview)
REGIONID_DOUBLE_QUOTES = "DoubleQuotes"
REGIONID_SINGLE_QUOTES = "SingleQuotes"
REGIONID_UNICODE_DOPEN_QUOTES = "UnicodeDOpenQuotes"
REGIONID_UNICODE_DCLOSE_QUOTES = "UnicodeDCloseQuotes"
REGIONID_UNICODE_SOPEN_QUOTES = "UnicodeSOpenQuotes"
REGIONID_UNICODE_SCLOSE_QUOTES = "UnicodeSCloseQuotes"
######################################
# General Utilities
# Find all the double quotes regions
def find_double_quotes(view):
# Regex expression to find each quote pair + their contents + an extra character
# Note: This is "v4" below
matcher = r"([`]{2})([^`])(\\?.)*?([^'])([']{2})([)}\n\]\s,.;?])"
return view.find_all(matcher, 0)
# Find all the single quotes regions
def find_single_quotes(view):
matcher = r"([({\[\s])`([^`])(\\?.)*?([^'])'([)}\n\]\s,.;?])"
return view.find_all(matcher, 0)
# ------------------------------------
# Clear all the regions associated with the highlights operator
def clear_quote_highlights(view):
view.erase_regions(REGIONID_DOUBLE_QUOTES)
view.erase_regions(REGIONID_SINGLE_QUOTES)
view.erase_regions(REGIONID_UNICODE_DOPEN_QUOTES)
view.erase_regions(REGIONID_UNICODE_DCLOSE_QUOTES)
view.erase_regions(REGIONID_UNICODE_SOPEN_QUOTES)
view.erase_regions(REGIONID_UNICODE_SCLOSE_QUOTES)
######################################
# Commands/Operators
# Run as "latex_quotes_to_enquote"
# TODO: Only apply within selected region - perhaps by intersecting regions?
class LatexQuotesToEnquoteCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
# Clear highlighting - It won't be valid anymore
clear_quote_highlights(self.view)
# Fix double quotes first, so that it is less likely to get confused
# with the triple-quoted stuff
self.fix_double_quotes(edit)
self.fix_single_quotes(edit)
# Also fix all unicode quotes (for the few files that have them...)
self.fix_unicode_quotes(edit)
# Fix the double quotes -> \enquote{}
def fix_double_quotes(self, edit, **args):
# NOTE: We must do this in reverse order, order else the offsets are all wrong
for match in reversed(find_double_quotes(self.view)):
# Get the matched string (it includes the paired quotes, and one extra character)
matched_str = self.view.substr(match)
# Get the "contents" only
clean_str = matched_str[2:-3]
# Also, grab the last character, since we'll need to tack
# it on to the end of the cleaned up tags
last_ch = matched_str[-1]
# Construct the result
result = "\\enquote{%s}%s" % (clean_str, last_ch)
# Replace away!
self.view.replace(edit, match, result)
#print "Match: %d - %d" % (match.begin(), match.end())
#print " -> M \"%s\"" % (matched_str)
#print " -> C \"%s\"" % (clean_str)
# Fix the single quotes -> \squote{}
def fix_single_quotes(self, edit, **args):
# NOTE: We must do this in reverse order, order else the offsets are all wrong
for match in reversed(find_single_quotes(self.view)):
# Get the matched string (it includes the paired quotes, and one extra on each side)
matched_str = self.view.substr(match)
# Get the "contents" only
clean_str = matched_str[2:-2]
# Also, grab the first+last characters, since we'll need to tack
# those on to the ends of the final string
s_ch = matched_str[0]
e_ch = matched_str[-1]
# Construct the result
result = "%s\\squote{%s}%s" % (s_ch, clean_str, e_ch)
# Replace away!
self.view.replace(edit, match, result)
#print "Match: %d - %d" % (match.begin(), match.end())
#print " -> M \"%s\"" % (matched_str)
#print " -> C \"%s\"" % (clean_str)
# Replace unicode curly quotes
def fix_unicode_quotes(self, edit, **args):
# Double Open/Close
for match in reversed(self.view.find_all(u"“", 0)):
self.view.replace(edit, match, "\\enquote{")
for match in reversed(self.view.find_all(u"”", 0)):
self.view.replace(edit, match, "}")
# Single Pair
# Note: Do the pair before the singles, so that we can catch the apostrophe's
# properly too afterwards
for match in reversed(self.view.find_all(ur"‘(\\?.)*?’", 0)):
# Get the matched string
matched_str = self.view.substr(match)
# Clean up the quotes
clean_str = matched_str[1:-1]
# Replace
result = "\\squote{%s}" % (clean_str)
self.view.replace(edit, match, result)
# Left-Over Singles - This should just be for apostrophes
for match in reversed(self.view.find_all(u"’", 0)):
self.view.replace(edit, match, "'")
# Run as "preview_latex_quote_fixes"
# TODO: Only apply within selected region - perhaps by intersecting regions?
class PreviewLatexQuoteFixesCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
clear_quote_highlights(self.view)
dcount = self.highlight_double_quotes()
scount = self.highlight_single_quotes()
ucount = self.highlight_unicode_quotes()
msg = "Identified: %d Double, %d Single Pairs, %d Unicode..." % (dcount, scount, ucount)
sublime.status_message(msg)
print msg
# Highlight the selected quotes
def highlight_double_quotes(self):
regions = find_double_quotes(self.view)
scope = "string" # XXX
icon = "circle"
flags = sublime.DRAW_OUTLINED
self.view.add_regions(REGIONID_DOUBLE_QUOTES, regions, scope, icon, flags)
return len(regions)
# Highlight the selected quotes
def highlight_single_quotes(self):
regions = find_single_quotes(self.view)
scope = "constant.numeric" # XXX
icon = "dot"
flags = sublime.DRAW_OUTLINED
self.view.add_regions(REGIONID_SINGLE_QUOTES, regions, scope, icon, flags)
return len(regions)
# Highlight the unicode quotes
def highlight_unicode_quotes(self):
# Common drawing options -> Basically, we want to mark these as errors that we're trying to fix
scope = "invalid.deprecated"
icon = "cross"
flags = 0
count = 0
# Double-Quotes - Open
regions = self.view.find_all(u"“", 0)
count += len(regions)
self.view.add_regions(REGIONID_UNICODE_DOPEN_QUOTES, regions, scope, icon, flags)
# Double Quotes - Close
regions = self.view.find_all(u"”", 0)
count += len(regions)
self.view.add_regions(REGIONID_UNICODE_DCLOSE_QUOTES, regions, scope, icon, flags)
# Single-Quotes - Open
regions = self.view.find_all(u"‘", 0)
count += len(regions)
self.view.add_regions(REGIONID_UNICODE_SOPEN_QUOTES, regions, scope, icon, flags)
# Single-Quotes - Close
regions = self.view.find_all(u"’", 0)
count += len(regions)
self.view.add_regions(REGIONID_UNICODE_SCLOSE_QUOTES, regions, scope, icon, flags)
# Return the number of errors identified
return count
# Run as "clear_latex_quote_preview"
class ClearLatexQuotePreviewCommand(sublime_plugin.TextCommand):
def run(self, edit):
clear_quote_highlights(self.view)
######################################
tips = """
# Get cursor position
pos = self.view.sel()[0].begin()
# Get range of first selection
self.view.sel()[0].begin()/.end()
"""
double_match_notes = """
Test Suite -------------------------------------
1) Capture ``blah''
2) Capture ``blah blah''
3) Capture ``blah blagh bleh''
4) Don't capture ```blah'''
5) Should capture ``blah `bleh' blargh''
6) Capture (``blah'')
7) Capture {``blah''}
8) Capture ``blah'' blah
9) Capture ``active'',
Attempts --------------------------------------
V1: Correct (1,2,3,6,7), Incorrect (4), Missed (5)
V1: "``([^`']+)''"
i.e. `` (everything that isn't start/end quotes) ''
V2: Correct(1,2,3,5,6,7), Incorrect (4 - All but last quote)
V2: ([`]{2})(\\?.)*?([']{2})
V3: Captures all, with excess
V3: ([`]{2})(\\?.)*?([']{2})([)}\n\]\s])
V4: All except 4
V4: ([`]{2})([^`])(\\?.)*?([^'])([']{2})([)}\n\]\s])
i.e. ([`]{2}) ([^`]) (\\?.)*? ([^']) ([']{2}) ([)}\n\]\s])
Start pair | Skip 3rd | Match Contents as #3 | Skip 3rd | End pair | Excess "Word End"
"""
single_match_notes = """
Test Suite ------------------------------------------
1) Capture `blah'
2) Capture `blah blah blah'
3) Capture (`blah')
4) Capture {`blah'}
5) Don't capture ```noob'''
6) Don't capture ``nada''
7) Don't capture don't worry''
8) Don't capture ``blah `bleh' blargh''
-----------------------------------------------------
V1: Incorrect (8)
V1: `([^`])(\\?.)*?([^'])'([)}\n\]\s])
V2: ([({\[\s])`([^`])(\\?.)*?([^'])'([)}\n\]\s])
"""