diff --git a/IPython/utils/tests/test_text.py b/IPython/utils/tests/test_text.py index 1d0b70a..58b6044 100644 --- a/IPython/utils/tests/test_text.py +++ b/IPython/utils/tests/test_text.py @@ -107,3 +107,5 @@ def test_dollar_formatter(): nt.assert_equals(s, "12") s = f.format("$n/{stuff[:5]}", **ns) nt.assert_equals(s, "12/hello") + s = f.format("$n $$HOME", **ns) + nt.assert_equals(s, "12 $HOME") diff --git a/IPython/utils/text.py b/IPython/utils/text.py index ba16e05..6ec0d7b 100644 --- a/IPython/utils/text.py +++ b/IPython/utils/text.py @@ -663,20 +663,26 @@ class DollarFormatter(FullEvalFormatter): In [4]: f.format('$a or {b}', a=1, b=2) Out[4]: u'1 or 2' """ - _dollar_pattern = re.compile("(.*)\$([\w\.]+)") + _dollar_pattern = re.compile("(.*?)\$(\$?[\w\.]+)") def parse(self, fmt_string): for literal_txt, field_name, format_spec, conversion \ in Formatter.parse(self, fmt_string): # Find $foo patterns in the literal text. continue_from = 0 + txt = "" for m in self._dollar_pattern.finditer(literal_txt): new_txt, new_field = m.group(1,2) - yield (new_txt, new_field, "", None) + # $$foo --> $foo + if new_field.startswith("$"): + txt += new_txt + new_field + else: + yield (txt + new_txt, new_field, "", None) + txt = "" continue_from = m.end() # Re-yield the {foo} style pattern - yield (literal_txt[continue_from:], field_name, format_spec, conversion) + yield (txt + literal_txt[continue_from:], field_name, format_spec, conversion) def columnize(items, separator=' ', displaywidth=80):