|
17 | 17 | import re |
18 | 18 | from plover.steno import normalize_steno |
19 | 19 | from plover.steno_dictionary import StenoDictionary |
| 20 | +# TODO: Move dictionary format somewhere more caninical than formatting. |
| 21 | +from plover.formatting import META_RE |
20 | 22 |
|
21 | 23 | # A regular expression to capture an individual entry in the dictionary. |
22 | 24 | DICT_ENTRY_PATTERN = re.compile(r'(?s)(?<!\\){\\\*\\cxs (?P<steno>[^}]+)}' + |
@@ -81,19 +83,19 @@ def _re_handle_escaped_newline(self, m): |
81 | 83 | return '{#Return}{#Return}' |
82 | 84 |
|
83 | 85 | def _re_handle_infix(self, m): |
84 | | - r'\\cxds ([^{}\\\r\n]+)\\cxds' |
| 86 | + r'\\cxds ([^{}\\\r\n]+)\\cxds ?' |
85 | 87 | return '{^%s^}' % m.group(1) |
86 | 88 |
|
87 | 89 | def _re_handle_suffix(self, m): |
88 | 90 | r'\\cxds ([^{}\\\r\n ]+)' |
89 | 91 | return '{^%s}' % m.group(1) |
90 | 92 |
|
91 | 93 | def _re_handle_prefix(self, m): |
92 | | - r'([^{}\\\r\n ]+)\\cxds' |
| 94 | + r'([^{}\\\r\n ]+)\\cxds ?' |
93 | 95 | return '{%s^}' % m.group(1) |
94 | 96 |
|
95 | 97 | def _re_handle_commands(self, m): |
96 | | - r'(\\\*)?\\([a-z]+)(-?[0-9]+)?[ ]?' |
| 98 | + r'(\\\*)?\\([a-z]+)(-?[0-9]+)? ?' |
97 | 99 |
|
98 | 100 | ignore = bool(m.group(1)) |
99 | 101 | command = m.group(2) |
@@ -294,31 +296,37 @@ def load_dictionary(s): |
294 | 296 | HEADER = ("{\\rtf1\\ansi{\\*\\cxrev100}\\cxdict{\\*\\cxsystem Plover}" + |
295 | 297 | "{\\stylesheet{\\s0 Normal;}}\n") |
296 | 298 |
|
| 299 | +def format_translation(t): |
| 300 | + t = ' '.join([x.strip() for x in META_RE.findall(t) if x.strip()]) |
| 301 | + |
| 302 | + t = re.sub(r'{\.}', '{\\cxp. }', t) |
| 303 | + t = re.sub(r'{!}', '{\\cxp! }', t) |
| 304 | + t = re.sub(r'{\?}', '{\\cxp? }', t) |
| 305 | + t = re.sub(r'{\,}', '{\\cxp, }', t) |
| 306 | + t = re.sub(r'{:}', '{\\cxp: }', t) |
| 307 | + t = re.sub(r'{;}', '{\\cxp; }', t) |
| 308 | + t = re.sub(r'{\^}', '\\cxds ', t) |
| 309 | + t = re.sub(r'{\^([^^}]*)}', '\\cxds \\1', t) |
| 310 | + t = re.sub(r'{([^^}]*)\^}', '\\1\\cxds ', t) |
| 311 | + t = re.sub(r'{\^([^^}]*)\^}', '\\cxds \\1\\cxds ', t) |
| 312 | + t = re.sub(r'{-\|}', '\\cxfc ', t) |
| 313 | + t = re.sub(r'{>}', '\\cxfls ', t) |
| 314 | + t = re.sub(r'{ }', ' ', t) |
| 315 | + t = re.sub(r'{&([^}]+)}', '{\\cxfing \\1}', t) |
| 316 | + t = re.sub(r'{#([^}]+)}', '\\{#\\1\\}', t) |
| 317 | + t = re.sub(r'{PLOVER:([a-zA-Z]+)}', '\\{PLOVER:\\1\\}', t) |
| 318 | + t = re.sub(r'\\"', '"', t) |
| 319 | + |
| 320 | + return t |
| 321 | + |
| 322 | + |
297 | 323 | # TODO: test this |
298 | 324 | def save_dictionary(d, fp): |
299 | 325 | fp.write(HEADER) |
300 | 326 |
|
301 | 327 | for s, t in d.items(): |
302 | 328 | s = '/'.join(s) |
303 | | - |
304 | | - t = re.sub(r'{\.}', '{\\cxp. }', t) |
305 | | - t = re.sub(r'{!}', '{\\cxp! }', t) |
306 | | - t = re.sub(r'{\?}', '{\\cxp? }', t) |
307 | | - t = re.sub(r'{\,}', '{\\cxp, }', t) |
308 | | - t = re.sub(r'{:}', '{\\cxp: }', t) |
309 | | - t = re.sub(r'{;}', '{\\cxp; }', t) |
310 | | - t = re.sub(r'{\^}', '\\cxds ', t) |
311 | | - t = re.sub(r'{\^([^^}]*)}', '\\cxds \\1', t) |
312 | | - t = re.sub(r'{([^^}]*)\^}', '\\1\\cxds ', t) |
313 | | - t = re.sub(r'{\^([^^}]*)\^}', '\\cxds \\1\\cxds ', t) |
314 | | - t = re.sub(r'{-\|}', '\\cxfc ', t) |
315 | | - t = re.sub(r'{>}', '\\cxfls ', t) |
316 | | - t = re.sub(r'{ }', ' ', t) |
317 | | - t = re.sub(r'{&([^}]+)}', '{\\cxfing \\1}', t) |
318 | | - t = re.sub(r'{#([^}]+)}', '\\{#\\1\\}', t) |
319 | | - t = re.sub(r'{PLOVER:([a-zA-Z]+)}', '\\{PLOVER:\\1\\}', t) |
320 | | - t = re.sub(r'\\"', '"', t) |
321 | | - |
| 329 | + t = format_translation(t) |
322 | 330 | entry = "{\\*\\cxs %s}%s\r\n" % (s, t) |
323 | 331 | fp.write(entry) |
324 | 332 |
|
|
0 commit comments