-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkroman.py
135 lines (125 loc) · 2.5 KB
/
kroman.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
135
import sublime, sublime_plugin
import sys, math
head_jamos = [
'g',
'gg',
'n',
'd',
'dd',
'r',
'm',
'b',
'bb',
's',
'ss',
'',
'j',
'jj',
'c',
'k',
't',
'p',
'h'
]
body_jamos = [
'a',
'ae',
'ya',
'yae',
'eo',
'e',
'yeo',
'ye',
'o',
'wa',
'wae',
'oe',
'yo',
'u',
'weo',
'we',
'wi',
'yu',
'eu',
'eui',
'i'
]
tail_jamos = [
'',
'g',
'gg',
'gs',
'n',
'nj',
'nh',
'd',
'r',
'rk',
'rm',
'rb',
'rs',
'rt',
'rp',
'rh',
'm',
'b',
'bs',
's',
'ss',
'ng',
'j',
'c',
'k',
't',
'p',
'h'
]
def parse(text):
if sys.version_info[0] == 2:
text = unicode(text, 'utf-8')
retval = u''
ga = 0xac00
hih = 0xd7a3
interval_head = 588
interval_body = 28
last_char_is_hangul = False
for c in text:
cint = ord(c)
if ga <= cint <= hih:
head = int(math.floor((cint - ga) / interval_head))
headl = int(math.floor((cint - ga) % interval_head))
body = int(math.floor(headl / interval_body))
tail = int(math.floor(headl % interval_body))
if last_char_is_hangul:
retval += '-'
retval += head_jamos[head]
retval += body_jamos[body]
retval += tail_jamos[tail]
last_char_is_hangul = True
else:
last_char_is_hangul = False
retval += c
return retval
class KromanReplaceRegionCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
for region in view.sel():
if not region.empty():
s = view.substr(region)
s = parse(s)
view.replace(edit, region, s)
class KromanReplaceFileCommand(sublime_plugin.TextCommand):
def run(self, edit):
allcontent = sublime.Region(0, self.view.size())
s = self.view.substr(allcontent)
s = parse(s)
self.view.replace(edit, allcontent, s)
class KromanCompareFileCommand(sublime_plugin.TextCommand):
def run(self, edit):
whole_region = sublime.Region(0, self.view.size())
s = self.view.substr(whole_region)
s = parse(s)
self.view.window().run_command("set_layout", {"cells": [[0, 0, 1, 1], [1, 0, 2, 1]], "cols": [0.0, 0.5, 1.0], "rows": [0.0, 1.0]})
# self.view.window().run_command("focus_neighboring_group")
v = self.view.window().active_view()
v.insert(edit, 0, s)