-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanki-tools.py
224 lines (178 loc) · 7.28 KB
/
anki-tools.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import json
import re
import bs4
import requests
import sublime
import sublime_plugin
def print_in_panel(view, edit, text):
panel = view.window().find_output_panel("dutch_tools_panel")
if not panel:
panel = view.window().create_output_panel("dutch_tools_panel")
view.window().run_command('show_panel', {"panel": 'output.dutch_tools_panel'})
panel.insert(edit, panel.size(), text + "\n")
def get_transcription(word):
# html = urlopen("https://www.mijnwoordenboek.nl/vertaal/NL/EN/%s" % word)
# content = html.read()
r = requests.get("https://www.mijnwoordenboek.nl/vertaal/NL/EN/%s" % word, verify=False)
# print("Enconding: " + r.encoding)
soup = bs4.BeautifulSoup(r.text, 'html.parser')
tag = soup.find("td", class_="smallcaps", string=re.compile("Uitspraak.*"))
# print("### TAG FOUND: %s" % tag)
# print("### " + tag.next_sibling.contents[0])
if tag and tag.next_sibling and tag.next_sibling.contents[0]:
return tag.next_sibling.contents[0]
else:
return None
def insert_transcription(view, edit):
selection = view.sel()
if selection:
for sel in selection:
word = view.substr(sel)
transcription = get_transcription(word)
if transcription:
print_in_panel(view, edit, "Word: '%s' Transcription: %s" % (word, transcription))
line = view.line(sel)
ending = view.substr(line.end() - 1)
if ending != ';':
transcription = ";" + transcription
view.insert(edit, line.end(), transcription)
else:
print_in_panel(view, edit, "No transcription found for: %s" % word)
transcription = "N/A"
line = view.line(sel)
ending = view.substr(line.end() - 1)
if ending != ';':
transcription = ";"
view.insert(edit, line.end(), transcription)
def request(action, **params):
return {'action': action, 'params': params, 'version': 6}
def invoke(action, **params):
request_json = json.dumps(request(action, **params)).encode('utf-8')
r = requests.post("http://localhost:8765", request_json)
response = json.loads(r.text)
if len(response) != 2:
raise Exception('response has an unexpected number of fields')
if 'error' not in response:
raise Exception('response is missing required error field')
if 'result' not in response:
raise Exception('response is missing required result field')
if response['error'] is not None:
raise Exception(response['error'])
return response['result']
def get_config(view, edit):
def extract_config(s):
m = re.match("^\s*#\s*(\w+)\s*[:=]\s*(.*)$", s)
if m:
return m.group(1), m.group(2)
lines = view.find_all("^\s*#\s*\w+\s*[:=]\s*.*$")
return dict([extract_config(view.substr(line)) for line in lines])
def prepare_note(fields, config):
def get_field(idx, name):
if idx < len(fields):
return name, fields[idx]
else:
return name, ''
def get_tags_list():
if tags_field_name in fields_dict:
tags_value = fields_dict.get(tags_field_name)
if tags_value:
return re.split("[, ]", tags_value)
else:
return []
if 'deck' not in config:
raise Exception("No 'deck' configuration found")
if 'model' not in config:
raise Exception("No 'model' configuration found")
if 'fields' not in config:
raise Exception("No 'fields' configuration found")
fields_order = re.split("[;, :]", config['fields'])
fields_dict = dict([get_field(idx, name) for idx, name in enumerate(fields_order)])
tags_field_name = config.get('tags', 'Tags')
tags_list = get_tags_list()
if tags_field_name in fields_dict:
del fields_dict[tags_field_name]
return {
"deckName": config['deck'],
"modelName": config['model'],
"fields": fields_dict,
"options": {
"allowDuplicate": False,
"duplicateScope": "deck",
"duplicateScopeOptions": {
"deckName": config['deck'],
"checkChildren": False
}
},
"tags": tags_list
}
class GetTranscriptionCommand(sublime_plugin.TextCommand):
def run(self, edit):
insert_transcription(self.view, edit)
class SendToAnkiCommand(sublime_plugin.TextCommand):
def run(self, edit):
config = get_config(self.view, edit)
try:
panel = self.view.window().find_output_panel("local_vars")
selection = self.view.sel()
if selection:
for sel in selection:
line = self.view.line(sel)
# view.insert(edit, line.b, ";" + transcription)
fields = [s for s in self.view.substr(line).split(';') if s != '']
min_fields = int(config.get('min-fields', 2))
if len(fields) < min_fields:
print_in_panel(self.view, edit, "Not enough fields (%d) for %s" % (len(fields), fields))
else:
note = prepare_note(fields, config)
print("Sending note: %s" % note)
result = invoke('addNote', note=note)
print_in_panel(self.view, edit, "Created note %s for %s" % (result, fields))
selection.clear()
selection.add(sublime.Region(line.end() + 1, line.end() + 1))
except Exception as e:
print_in_panel(self.view, edit, "*** Error:" + str(e))
class SelectTextForTranslationCommand(sublime_plugin.TextCommand):
def run(self, edit):
selection = self.view.sel()
if selection:
regions = self.view.find_all(pattern="^(.*?)[\(\/;,]")
regions_in_selection = [r for r in regions if selection.contains(r)]
selection.clear()
for region in regions_in_selection:
region.b -= 1
selection.add(region)
class AddTrailingSeparatorCommand(sublime_plugin.TextCommand):
def run(self, edit):
selection = self.view.sel()
if selection and len(selection) > 0:
sel = selection[0]
lines = self.view.lines(sel)
offset = 0
for line in lines:
if line.begin() < line.end():
ending = self.view.substr(line.end() - 1 + offset)
if ending != ';':
self.view.insert(edit, line.end() + offset, ";")
offset += 1
class ShowHelpCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.show_popup("""
<div style="color: gray">
<h3>Help</h3>
----------------------------------<br>
<b>F5</b>: get transcription<br>
<b>F6</b>: select words for translation <br>
(works with selection only)<br>
<b>F7</b>: add trailing semi-columns <br>
(works with selection only)<br>
<b>F11</b>: send line to Anki<br>
----------------------------------<br>
Fields order: <ul>
<li>Nederlands
<li>Tags (space separated)
<li>Transcription
<li>English
<li>Russian
</ul>
</div>
""", 0, -1, 500, 300)