-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSublimeChineseConvert.py
executable file
·134 lines (107 loc) · 3.74 KB
/
SublimeChineseConvert.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
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
'''
@name SublimeChineseConvert
@package sublime_plugin
@author Rexdf
This Sublime Text 3 plugin adds Traditional/Simplified Chinese convert
features to the right click context menu.
Usage: Make a selection (or not), Choose menu
from the context menu.
'''
import sublime
import sublime_plugin
import os
OpenCC = None
BASE_PATH = os.path.abspath(os.path.dirname(__file__))
OPENCC_PATH = os.path.join(BASE_PATH, "OpenCC")
def get_opencc_path():
"""Return zipfile path according to platform."""
platform = sublime.platform()
arch = sublime.arch()
version = sublime.version()
if int(version) < 3000:
sublime.message_dialog("Don't support Sublime Text 2")
path = None
if platform == "windows":
if arch == "x32":
path = "OpenCC_Win32.zip"
elif arch == "x64":
path = "OpenCC_Win64.zip"
elif platform == "linux":
if arch == "x32":
path = "OpenCC_Linux32.zip"
elif arch == "x64":
path = "OpenCC_Linux64.zip"
elif platform == "osx":
path = "OpenCC_OSX.zip"
return os.path.join(BASE_PATH, path)
def get_dict_arch_path():
"""Return Dict_arch.zip path."""
arch = sublime.arch()
if arch == "x32":
return os.path.join(BASE_PATH, "Dict32.zip")
elif arch == "x64":
return os.path.join(BASE_PATH, "Dict64.zip")
def get_dict_path():
"""Return Dict.zip path."""
return os.path.join(BASE_PATH, "Dict.zip")
def init():
"""Load or unzip OpenCC."""
# PACKAGES_PATH = sublime.packages_path()
# PACKAGE_NAME = __name__.split('.')[0]
try:
from .OpenCC import OpenCC
except ImportError:
import zipfile
# mkdir if OPENCC_PATH not exist
if not os.path.isdir(OPENCC_PATH):
os.mkdir(OPENCC_PATH)
# unzip Dict.zip
DICT_PATH = get_dict_path()
if os.path.isfile(DICT_PATH):
with zipfile.ZipFile(DICT_PATH, "r") as f:
f.extractall(OPENCC_PATH)
# unzip Dict_arch.zip
DICT_ARCH_PATH = get_dict_arch_path()
if os.path.isfile(DICT_ARCH_PATH):
with zipfile.ZipFile(DICT_ARCH_PATH, "r") as f:
f.extractall(OPENCC_PATH)
# unzip OpenCC pre-build binary
ZIP_FILE_PATH = get_opencc_path()
if os.path.isfile(ZIP_FILE_PATH):
with zipfile.ZipFile(ZIP_FILE_PATH, "r") as f:
f.extractall(OPENCC_PATH)
try:
from .OpenCC import OpenCC
except ImportError:
raise Exception("Can not load OpenCC, return")
return
globals()['OpenCC'] = OpenCC
class ChineseConvertCommand(sublime_plugin.TextCommand):
def run(self, edit, to):
view_size = self.view.size()
regions = self.view.sel()
num = len(regions)
x = len(self.view.substr(regions[0]))
if num <= 1 and x == 0:
regions.clear()
regions.add(sublime.Region(0, view_size))
# support multi selection
cc = Tradsim(to)
for region in regions:
input_string = self.view.substr(region)
output_string = cc.convert(input_string)
self.view.replace(edit, region, output_string)
# self.view.end_edit(edit)
class Tradsim():
def __init__(self, config="s2t.json"):
config = os.path.join(OPENCC_PATH, config)
self._handle = OpenCC.opencc_open(config)
def convert(self, text):
result = OpenCC.opencc_convert_utf8(self._handle, text,
len(text.encode("utf-8")))
return result
def __del__(self):
OpenCC.opencc_close(self._handle)
def plugin_loaded():
"""Load and unzip the pre-build binary files."""
sublime.set_timeout(init, 200)