templater.firstline should handle empty strings
authorBrendan Cully <brendan@kublai.com>
Tue, 04 Jul 2006 15:36:05 -0700
changeset 2559 bf67d0f6531c
parent 2558 1120302009d7
child 2567 2748253b49c2
templater.firstline should handle empty strings
mercurial/templater.py
--- a/mercurial/templater.py	Mon Jul 03 00:33:19 2006 -0300
+++ b/mercurial/templater.py	Tue Jul 04 15:36:05 2006 -0700
@@ -221,6 +221,13 @@
         fp.write(rest)
     return fp.getvalue()
 
+def firstline(text):
+    '''return the first line of text'''
+    try:
+        return text.splitlines(1)[0].rstrip('\r\n')
+    except IndexError:
+        return ''
+
 def isodate(date):
     '''turn a (timestamp, tzoff) tuple into an iso 8631 date and time.'''
     return util.datestr(date, format='%Y-%m-%d %H:%M')
@@ -284,7 +291,7 @@
     "escape": lambda x: cgi.escape(x, True),
     "fill68": lambda x: fill(x, width=68),
     "fill76": lambda x: fill(x, width=76),
-    "firstline": lambda x: x.splitlines(1)[0].rstrip('\r\n'),
+    "firstline": firstline,
     "tabindent": lambda x: indent(x, '\t'),
     "hgdate": hgdate,
     "isodate": isodate,