encode_html_entities.py (764B)
1 import sublime 2 import sublime_plugin 3 4 from html.entities import codepoint2name as cp2n 5 6 class EncodeHtmlEntities(sublime_plugin.TextCommand): 7 def run(self, edit, **args): 8 view = self.view 9 10 for sel in view.sel(): 11 buf = [] 12 13 for pt in range(sel.begin(), sel.end()): 14 ch = view.substr(pt) 15 ch_ord = ord(ch) 16 17 if (not view.match_selector(pt, ('meta.tag - string, constant.character.entity')) 18 and ch_ord in cp2n 19 and not (ch in ('"', "'") 20 and view.match_selector(pt, 'string'))): 21 ch = '&%s;' % cp2n[ch_ord] 22 23 buf.append(ch) 24 25 view.replace(edit, sel, ''.join(buf))