166 lines
8.5 KiB
Python
166 lines
8.5 KiB
Python
# coding: utf8
|
||
|
||
import sublime_plugin
|
||
import urllib
|
||
import base64
|
||
import re
|
||
import json
|
||
|
||
|
||
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 get_end(region):
|
||
return region.end()
|
||
regions.sort(key=get_end, reverse=True)
|
||
|
||
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">": ">"
|
||
}
|
||
html_reserved_list = (u"\"", u"'", u"<", u">", u"&")
|
||
|
||
|
||
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 SafeHtmlEntitizeCommand(StringEncode):
|
||
def encode(self, text):
|
||
for k in html_escape_table:
|
||
# skip HTML reserved characters
|
||
if k in html_reserved_list:
|
||
continue
|
||
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 SafeHtmlDeentitizeCommand(StringEncode):
|
||
def encode(self, text):
|
||
for k in html_escape_table:
|
||
# skip HTML reserved characters
|
||
if k in html_reserved_list:
|
||
continue
|
||
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 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 JsonEscapeCommand(StringEncode):
|
||
def encode(self, text):
|
||
return json.dumps(text)
|
||
|
||
|
||
class JsonUnescapeCommand(StringEncode):
|
||
def encode(self, text):
|
||
return json.loads(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))
|