-
Notifications
You must be signed in to change notification settings - Fork 1
/
chign.py
61 lines (47 loc) · 1.32 KB
/
chign.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sublime, sublime_plugin
from string import maketrans
import sys, re
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
reload(sys)
sys.setdefaultencoding(default_encoding)
def multi_replace(aDict, subject):
for k, kv in aDict.iteritems():
r = re.compile(k)
subject = r.sub(kv, subject)
return subject
def process(string):
# mask `something`
maskWithoutBlank = re.compile("`[a-zA-Z0-9/=\-+*%<>!&|\^.~@]+`")
mask = re.compile(" *`[a-zA-Z0-9/=\-+*%<>!&|\^.~]+@` *")
mtexts = []
for m in re.finditer(maskWithoutBlank, string):
mtexts.append(m.group())
mtexts = tuple(mtexts)
string = mask.sub('%s', string.replace('%', '%%'))
print 'string', string
print 'mtexts', mtexts
# replace
aDict = {
r' *\, *':',',
r' *\. *':'。',
r' *\! *':'!',
r' *\? *':'?',
r' *\: *':':',
r' *\[ *':'【',
r' *\] *':'】',
r' *\( *':'(',
r' *\) *':')',
r' *\{ *':'{',
r' *\} *':'}'
}
return multi_replace(aDict, string) % mtexts
class ChignCommand(sublime_plugin.TextCommand):
def run(self, edit):
regions = self.view.sel()
print regions
for region in regions:
string = self.view.substr(region)
self.view.replace(edit, region, process(string))