This repository was archived by the owner on Jul 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhtml_encoder.py
More file actions
90 lines (72 loc) · 2.71 KB
/
html_encoder.py
File metadata and controls
90 lines (72 loc) · 2.71 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
# -*- coding: utf-8 -*-
import sys
import sublime
import sublime_plugin
PY_MAJOR_VER = sys.version_info[0]
PY_MINOR_VER = sys.version_info[1]
def selections(view, default_to_all=True):
regions = [r for r in view.sel() if not r.empty()]
if not regions and default_to_all:
regions = [sublime.Region(0, view.size())]
return regions
def encode_html(source):
if PY_MAJOR_VER >= 3:
from html import escape
return escape(source)
else:
# use saxutils escape for python 2
from xml.sax.saxutils import escape
html_escape_table = {'"': """, "'": "'"}
source = escape(source, html_escape_table)
source = source.decode('utf-8')
source = source.encode('ascii', 'xmlcharrefreplace')
return source
def decode_html(source):
# Use unescape from py standard lib
# see: http://stackoverflow.com/questions/2087370
if PY_MAJOR_VER == 2:
# python 2
from HTMLParser import HTMLParser
return HTMLParser().unescape(source)
elif PY_MAJOR_VER == 3 and PY_MINOR_VER <= 3:
# python 3
from html.parser import HTMLParser
return HTMLParser().unescape(source)
else:
# python 3.4+
from html import unescape
return unescape(source)
class HtmlDecodeCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
file_changed = False
for region in selections(view):
source = view.substr(region)
transformed = decode_html(source)
if source != transformed:
view.replace(edit, region, transformed)
file_changed = True
if file_changed is True:
sublime.set_timeout(lambda: sublime.status_message(
'HTML Encoder: HTML Decoded.'), 0)
else:
sublime.set_timeout(lambda: sublime.status_message(
'HTML Encoder: Nothing to decode.'), 0)
class HtmlEncodeCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
file_changed = False
for region in selections(view):
source = view.substr(region)
# decode first to prevent double-encoding:
source_decoded = decode_html(source)
transformed = encode_html(source_decoded)
if source != transformed:
view.replace(edit, region, transformed)
file_changed = True
if file_changed is True:
sublime.set_timeout(lambda: sublime.status_message(
'HTML Encoder: HTML Encoded.'), 0)
else:
sublime.set_timeout(lambda: sublime.status_message(
'HTML Encoder: Nothing to encode.'), 0)