summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Hatch <tim@timhatch.com>2014-10-03 22:41:54 -0700
committerTim Hatch <tim@timhatch.com>2014-10-03 22:41:54 -0700
commit24ee4c42972ee33e6321f79f1f5f72631cebb795 (patch)
tree212c650e59391bf1a9f9aa037e56721869c9030b
parent1c26f1a864cbc8deee3fef0cb292e3e67c084200 (diff)
downloadpygments-24ee4c42972ee33e6321f79f1f5f72631cebb795.tar.gz
Add util function for keyword list formatting
-rw-r--r--pygments/util.py20
-rw-r--r--tests/test_util.py10
2 files changed, 30 insertions, 0 deletions
diff --git a/pygments/util.py b/pygments/util.py
index 5dc6981f..65efe175 100644
--- a/pygments/util.py
+++ b/pygments/util.py
@@ -255,6 +255,26 @@ def unirange(a, b):
return u'(?:' + u'|'.join(buf) + u')'
+
+def format_lines(var_name, seq, raw=False):
+ """
+ Formats a sequence of strings for output.
+ """
+ lines = []
+ indent = ' ' * 4
+ lines.append(var_name + ' = (')
+ if raw:
+ # These should be preformatted reprs of, say, tuples.
+ for i in seq:
+ lines.append(indent + i + ',')
+ else:
+ for i in seq:
+ # Force use of single quotes
+ r = repr(i + '"')
+ lines.append(indent + r[:-2] + r[-1] + ',')
+ lines.append(')')
+ return '\n'.join(lines)
+
# Python 2/3 compatibility
if sys.version_info < (3, 0):
diff --git a/tests/test_util.py b/tests/test_util.py
index 59ecf14f..2581be0b 100644
--- a/tests/test_util.py
+++ b/tests/test_util.py
@@ -133,3 +133,13 @@ class UtilTest(unittest.TestCase):
m = r.match(first_non_bmp * 2)
self.assertTrue(m)
self.assertEquals(m.end(), len(first_non_bmp) * 2)
+
+ def test_format_lines(self):
+ lst = ['cat', 'dog']
+ output = util.format_lines('var', lst)
+ print output
+ d = {}
+ exec output in d
+ print d['var']
+ self.assertTrue(isinstance(d['var'], tuple))
+ self.assertEquals(('cat', 'dog'), d['var'])