=== modified file '__init__.py' --- __init__.py 2009-04-06 22:40:14 +0000 +++ __init__.py 2010-10-01 17:31:51 +0000 @@ -262,10 +262,13 @@ return "" -def compress_keywords(s): +def compress_keywords(s, keyword_dicts): """Replace cooked style keywords with raw style in a string. + Note: If the keyword is not known, the text is not modified. + :param s: the string + :param keyword_dicts: an iterable of keyword dictionaries. :return: the string with keywords compressed """ _raw_style = _keyword_style_registry.get('raw') @@ -277,6 +280,11 @@ break result += rest[:match.start()] keyword = match.group(1) + expansion = _get_from_dicts(keyword_dicts, keyword) + if expansion is None: + # Unknown expansion - leave as is + result += match.group(0) + else: result += _raw_style % {'name': keyword} rest = rest[match.end():] return result + rest @@ -351,7 +359,7 @@ def _kw_compressor(chunks, context=None): """Filter that replaces keywords with their compressed form.""" text = ''.join(chunks) - return [compress_keywords(text)] + return [compress_keywords(text, [keyword_registry])] def _kw_expander(chunks, context, encoder=None): === modified file 'tests/test_conversion.py' --- tests/test_conversion.py 2008-07-27 13:04:54 +0000 +++ tests/test_conversion.py 2010-10-01 17:31:51 +0000 @@ -34,6 +34,7 @@ ('$Foo$$Bar$', '$Foo: FOO! $$Bar: bar $'), ('abc $Foo$ xyz $Bar$ qwe', 'abc $Foo: FOO! $ xyz $Bar: bar $ qwe'), ('$Unknown$$Bar$', '$Unknown$$Bar: bar $'), + ('$Unknown: unkn $$Bar$', '$Unknown: unkn $$Bar: bar $'), ('$Foo$$Unknown$', '$Foo: FOO! $$Unknown$'), ('$CallMe$', '$CallMe: now! $'), ] @@ -44,7 +45,7 @@ def test_compression(self): # Test keyword expansion for raw, cooked in _samples: - self.assertEqual(raw, compress_keywords(cooked)) + self.assertEqual(raw, compress_keywords(cooked, [_keywords])) def test_expansion(self): # Test keyword expansion