forked from colinta/SublimeStringEncode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_encode.py
More file actions
180 lines (143 loc) · 9.44 KB
/
string_encode.py
File metadata and controls
180 lines (143 loc) · 9.44 KB
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
# coding: utf8
import sublime_plugin
import urllib
import base64
import re
class StringEncode(sublime_plugin.TextCommand):
def run(self, edit):
e = self.view.begin_edit('encode')
regions = [region for region in self.view.sel()]
# sort by region.end() DESC
def compare(region_a, region_b):
return cmp(region_b.end(), region_a.end())
regions.sort(compare)
for region in regions:
if region.empty():
continue
text = self.view.substr(region)
replacement = self.encode(text)
self.view.replace(edit, region, replacement)
self.view.end_edit(e)
html_escape_table = {
u"\"": """, u"'": "'", u"<": "<", u">": ">", u"¡": "¡", u"¢": "¢", u"£": "£", u"¤": "¤", u"¥": "¥", u"¦": "¦", u"§": "§", u"¨": "¨", u"©": "©", u"ª": "ª", u"«": "«", u"¬": "¬", u"®": "®", u"¯": "¯", u"°": "°", u"±": "±", u"²": "²", u"³": "³", u"´": "´", u"µ": "µ", u"¶": "¶", u"·": "·", u"¸": "¸", u"¹": "¹", u"º": "º", u"»": "»", u"¼": "¼", u"½": "½", u"¾": "¾", u"¿": "¿", u"À": "À", u"Á": "Á", u"Â": "Â", u"Ã": "Ã", u"Ä": "Ä", u"Å": "Å", u"Æ": "Æ", u"Ç": "Ç", u"È": "È", u"É": "É", u"Ê": "Ê", u"Ë": "Ë", u"Ì": "Ì", u"Í": "Í", u"Î": "Î", u"Ï": "Ï", u"Ð": "Ð", u"Ñ": "Ñ", u"Ò": "Ò", u"Ó": "Ó", u"Ô": "Ô", u"Õ": "Õ", u"Ö": "Ö", u"×": "×", u"Ø": "Ø", u"Ù": "Ù", u"Ú": "Ú", u"Û": "Û", u"Ü": "Ü", u"Ý": "Ý", u"Þ": "Þ", u"ß": "ß", u"à": "à", u"á": "á", u"â": "â", u"ã": "ã", u"ä": "ä", u"å": "å", u"æ": "æ", u"ç": "ç", u"è": "è", u"é": "é", u"ê": "ê", u"ë": "ë", u"ì": "ì", u"í": "í", u"î": "î", u"ï": "ï", u"ð": "ð", u"ñ": "ñ", u"ò": "ò", u"ó": "ó", u"ô": "ô", u"õ": "õ", u"ö": "ö", u"÷": "÷", u"ø": "ø", u"ù": "ù", u"ú": "ú", u"û": "û", u"ü": "ü", u"ý": "ý", u"þ": "þ", u"ÿ": "ÿ", u"Œ": "Œ", u"œ": "œ", u"Š": "Š", u"š": "š", u"Ÿ": "Ÿ", u"ƒ": "ƒ", u"ˆ": "ˆ", u"˜": "˜", u"Α": "Α", u"Β": "Β", u"Γ": "Γ", u"Δ": "Δ", u"Ε": "Ε", u"Ζ": "Ζ", u"Η": "Η", u"Θ": "Θ", u"Ι": "Ι", u"Κ": "Κ", u"Λ": "Λ", u"Μ": "Μ", u"Ν": "Ν", u"Ξ": "Ξ", u"Ο": "Ο", u"Π": "Π", u"Ρ": "Ρ", u"Σ": "Σ", u"Τ": "Τ", u"Υ": "Υ", u"Φ": "Φ", u"Χ": "Χ", u"Ψ": "Ψ", u"Ω": "Ω", u"α": "α", u"β": "β", u"γ": "γ", u"δ": "δ", u"ε": "ε", u"ζ": "ζ", u"η": "η", u"θ": "θ", u"ι": "ι", u"κ": "κ", u"λ": "λ", u"μ": "μ", u"ν": "ν", u"ξ": "ξ", u"ο": "ο", u"π": "π", u"ρ": "ρ", u"ς": "ς", u"σ": "σ", u"τ": "τ", u"υ": "υ", u"φ": "φ", u"χ": "χ", u"ψ": "ψ", u"ω": "ω", u"ϑ": "ϑ", u"ϒ": "ϒ", u"ϖ": "ϖ", u"–": "–", u"—": "—", u"‘": "‘", u"’": "’", u"‚": "‚", u"“": "“", u"”": "”", u"„": "„", u"†": "†", u"‡": "‡", u"•": "•", u"…": "…", u"‰": "‰", u"′": "′", u"″": "″", u"‹": "‹", u"›": "›", u"‾": "‾", u"⁄": "⁄", u"€": "€", u"ℑ": "ℑ", u"℘": "℘", u"ℜ": "ℜ", u"™": "™", u"ℵ": "ℵ", u"←": "←", u"↑": "↑", u"→": "→", u"↓": "↓", u"↔": "↔", u"↵": "↵", u"⇐": "⇐", u"⇑": "⇑", u"⇒": "⇒", u"⇓": "⇓", u"⇔": "⇔", u"∀": "∀", u"∂": "∂", u"∃": "∃", u"∅": "∅", u"∇": "∇", u"∈": "∈", u"∉": "∉", u"∋": "∋", u"∏": "∏", u"∑": "∑", u"−": "−", u"∗": "∗", u"√": "√", u"∝": "∝", u"∞": "∞", u"∠": "∠", u"∧": "∧", u"∨": "∨", u"∩": "∩", u"∪": "∪", u"∫": "∫", u"∴": "∴", u"∼": "∼", u"≅": "≅", u"≈": "≈", u"≠": "≠", u"≡": "≡", u"≤": "≤", u"≥": "≥", u"⊂": "⊂", u"⊃": "⊃", u"⊄": "⊄", u"⊆": "⊆", u"⊇": "⊇", u"⊕": "⊕", u"⊗": "⊗", u"⊥": "⊥", u"⋅": "⋅", u"⌈": "⌈", u"⌉": "⌉", u"⌊": "⌊", u"⌋": "⌋", u"〈": "⟨", u"〉": "⟩", u"◊": "◊", u"♠": "♠", u"♣": "♣", u"♥": "♥", u"♦": "♦",
}
xml_escape_table = {
u"\"": """, u"'": "'", u"<": "<", u">": ">"
}
french_escape_table = {
u"À": "À", # Capital A-grave
u"à": "à", # Lowercase a-grave
u"Â": "Â", # Capital A-circumflex
u"â": "â", # Lowercase a-circumflex
u"Æ": "Æ", # Capital AE Ligature
u"æ": "æ", # Lowercase AE Ligature
u"Ç": "Ç", # Capital C-cedilla
u"ç": "ç", # Lowercase c-cedilla
u"È": "È", # Capital E-grave
u"è": "è", # Lowercase e-grave
u"É": "É", # Capital E-acute
u"é": "é", # Lowercase e-acute
u"Ê": "Ê", # Capital E-circumflex
u"ê": "ê", # Lowercase e-circumflex
u"Ë": "Ë", # Capital E-umlaut
u"ë": "ë", # Lowercase e-umlaut
u"Î": "Î", # Capital I-circumflex
u"î": "î", # Lowercase i-circumflex
u"Ï": "Ï", # Capital I-umlaut
u"ï": "ï", # Lowercase i-umlaut
u"Ô": "Ô", # Capital O-circumflex
u"ô": "ô", # Lowercase o-circumflex
u"Œ": "Œ", # Capital OE ligature
u"œ": "œ", # Lowercase oe ligature
u"Ù": "Ù", # Capital U-grave
u"ù": "ù", # Lowercase u-grave
u"Û": "Û", # Capital U-circumflex
u"û": "û", # Lowercase U-circumflex
u"Ü": "Ü", # Capital U-umlaut
u"ü": "ü", # Lowercase U-umlaut
u"«": "«", # Left angle quotes
u"»": "»", # Right angle quotes
u"€": "€" # Euro
}
class HtmlEntitizeCommand(StringEncode):
def encode(self, text):
text = text.replace('&', '&')
for k in html_escape_table:
v = html_escape_table[k]
text = text.replace(k, v)
ret = ''
for i, c in enumerate(text):
if ord(c) > 127:
ret += hex(ord(c)).replace('0x', '&#x') + ';'
else:
ret += c
return ret
class HtmlDeentitizeCommand(StringEncode):
def encode(self, text):
for k in html_escape_table:
v = html_escape_table[k]
text = text.replace(v, k)
while re.search('&#[xX][a-fA-F0-9]+;', text):
match = re.search('&#[xX]([a-fA-F0-9]+);', text)
text = text.replace(match.group(0), unichr(int('0x' + match.group(1), 16)))
text = text.replace('&', '&')
return text
class FrenchEntitizeCommand(StringEncode):
def encode(self, text):
for k in french_escape_table:
v = french_escape_table[k]
text = text.replace(k, v)
while re.search('&#[xX][a-fA-F0-9]+;', text):
match = re.search('&#[xX]([a-fA-F0-9]+);', text)
text = text.replace(match.group(0), unichr(int('0x' + match.group(1), 16)))
text = text.replace('&', '&')
return text
class FrenchDeentitizeCommand(StringEncode):
def encode(self, text):
for k in french_escape_table:
v = french_escape_table[k]
text = text.replace(v, k)
while re.search('&#[xX][a-fA-F0-9]+;', text):
match = re.search('&#[xX]([a-fA-F0-9]+);', text)
text = text.replace(match.group(0), unichr(int('0x' + match.group(1), 16)))
text = text.replace('&', '&')
return text
class XmlEntitizeCommand(StringEncode):
def encode(self, text):
text = text.replace('&', '&')
for k in xml_escape_table:
v = xml_escape_table[k]
text = text.replace(k, v)
return text
class XmlDeentitizeCommand(StringEncode):
def encode(self, text):
for k in xml_escape_table:
v = xml_escape_table[k]
text = text.replace(v, k)
text = text.replace('&', '&')
return text
class UrlEncodeCommand(StringEncode):
def encode(self, text):
return urllib.quote(text)
class UrlDecodeCommand(StringEncode):
def encode(self, text):
return urllib.unquote(text)
class Base64EncodeCommand(StringEncode):
def encode(self, text):
return base64.b64encode(text)
class Base64DecodeCommand(StringEncode):
def encode(self, text):
return base64.b64decode(text)
class Escaper(StringEncode):
def encode(self, text):
return re.sub(r'(?<!\\)(%s)' % self.meta, r'\\\1', text)
class EscapeRegexCommand(Escaper):
meta = r'[\\*.+^$()\[\]\{\}]'
class EscapeLikeCommand(Escaper):
meta = r'[%_]'
class HexDecCommand(StringEncode):
def encode(self, text):
return str(int(text, 16))
class DecHexCommand(StringEncode):
def encode(self, text):
return hex(int(text))