-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmartyPants.py
76 lines (65 loc) · 1.72 KB
/
SmartyPants.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
import sublime, sublime_plugin
import re
def replacements():
return [
("\"", "“"),
("\"", "”"),
("'", "‘"),
("'", "’"),
("...", "…"),
("---", "—"),
("--", "–"),
("<<", "«"),
(">>", "»"),
("1/2", "½"),
("1/4", "¼"),
("3/4", "¾"),
("1/7", "⅐"),
("1/9", "⅑"),
("1/10", "⅒"),
("1/3", "⅓"),
("2/3", "⅔"),
("1/5", "⅕"),
("2/5", "⅖"),
("3/5", "⅗"),
("4/5", "⅘"),
("1/6", "⅙"),
("5/6", "⅚"),
("1/8", "⅛"),
("3/8", "⅜"),
("5/8", "⅝"),
("7/8", "⅞"),
("(c)", "©"),
("(r)", "®"),
("(p)", "℗"),
("(tm)", "™"),
("+-", "±"),
("!=", "≠"),
("?!", "‽"),
]
class SmartyPantsCommand(sublime_plugin.TextCommand):
def run(self, edit):
for sel in self.view.sel():
region = self.view.word(sel)
text = self.view.substr(region)
if text == "":
continue
# Transform these items manually because we need a regex.
text = re.sub(r"\"(.*?)\"", r"“\1”", text)
text = re.sub(r"'(.*?)'", r"‘\1’", text)
text = re.sub(r"([\w\d]+)'", r"\1’", text)
text = re.sub(r"([\d.,]+) x ([\d.,]+)", r"\1 × \2", text)
for pair in replacements():
text = text.replace(pair[0], pair[1])
self.view.replace(edit, region, text)
class UnsmartyPantsCommand(sublime_plugin.TextCommand):
def run(self, edit):
for sel in self.view.sel():
region = self.view.word(sel)
text = self.view.substr(region)
if text == "":
continue
text = text.replace("×", "x")
for pair in replacements():
text = text.replace(pair[1], pair[0])
self.view.replace(edit, region, text)