Skip to content

Commit

Permalink
Merge pull request #102 from balshetzer/master
Browse files Browse the repository at this point in the history
Fix how prefixes, suffixes and infixes are loaded from RTF dictionaries
  • Loading branch information
balshetzer committed Jul 12, 2013
2 parents ab37753 + b27e817 commit c774cf7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
19 changes: 18 additions & 1 deletion plover/dictionary/rtfcre_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ class TranslationConverter(object):

def __init__(self, styles={}):
self.styles = styles

def linenumber(f):
return f[1].im_func.func_code.co_firstlineno

handler_funcs = inspect.getmembers(self, inspect.ismethod)
handler_funcs.sort(key=linenumber)
handlers = [self._make_re_handler(f.__doc__, f)
for name, f in handler_funcs
if name.startswith('_re_handle_')]
Expand Down Expand Up @@ -73,6 +78,18 @@ def _re_handle_escaped_newline(self, m):
r'\\\r\n'
return '{#Return}{#Return}'

def _re_handle_infix(self, m):
r'\\cxds ([^{}\\\r\n]+)\\cxds'
return '{^%s^}' % m.group(1)

def _re_handle_suffix(self, m):
r'\\cxds ([^{}\\\r\n ]+)'
return '{^%s}' % m.group(1)

def _re_handle_prefix(self, m):
r'([^{}\\\r\n ]+)\\cxds'
return '{%s^}' % m.group(1)

def _re_handle_commands(self, m):
r'(\\\*)?\\([a-z]+)(-?[0-9]+)?[ ]?'

Expand Down Expand Up @@ -283,7 +300,7 @@ def save_dictionary(d, fp):
t = re.sub(r'{([^^}]*)\^}', '\\1\\cxds ', t)
t = re.sub(r'{\^([^^}]*)\^}', '\\cxds \\1\\cxds ', t)
t = re.sub(r'{-\|}', '\\cxfc ', t)
t = re.sub(r'{>}', '\\cxfl ', t)
t = re.sub(r'{>}', '\\cxfls ', t)
t = re.sub(r'{ }', ' ', t)
t = re.sub(r'{&([^}]+)}', '{\\cxfing \\1}', t)
t = re.sub(r'{#([^}]+)}', '\\{#\\1\\}', t)
Expand Down
15 changes: 11 additions & 4 deletions plover/dictionary/test_rtfcre_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def test_converter(self):
(r'\_', '-'),
('\\\r\n', '{#Return}{#Return}'),
(r'\cxds', '{^}'),
(r'pre\cxds', 'pre{^}'),
(r'\cxds post', '{^}post'),
(r'\cxds in\cxds', '{^}in{^}'),
(r'pre\cxds', '{pre^}'),
(r'\cxds post', '{^post}'),
(r'\cxds in\cxds', '{^in^}'),
(r'\cxfc', '{-|}'),
(r'\cxfl', '{>}'),
(r'pre\cxfl', 'pre{>}'),
Expand Down Expand Up @@ -72,8 +72,15 @@ def test_converter(self):
(r'{\*\nonexistant {\cxp .}}', ''),
)

failed = []
for before, after in cases:
self.assertEqual(convert(before), after)
if convert(before) != after:
failed.append((before, after))

for before, after in failed:
print 'convert(%s) != %s: %s' % (before, after, convert(before))

self.assertEqual(len(failed), 0)

def test_load_dict(self):
"""Test the load_dict function.
Expand Down

0 comments on commit c774cf7

Please sign in to comment.