Patchwork [3,of,3] templater: add indentation arguments to the fill function

login
register
mail settings
Submitter Sean Farley
Date April 18, 2013, 9:01 p.m.
Message ID <70658c4039a143f9e6d6.1366318892@laptop.local>
Download mbox | patch
Permalink /patch/1441/
State Superseded, archived
Commit 889807c793846ddad2a1814db920b99d2c6dec57
Headers show

Comments

Sean Farley - April 18, 2013, 9:01 p.m.
# HG changeset patch
# User Sean Farley <sean.michael.farley@gmail.com>
# Date 1366318102 18000
#      Thu Apr 18 15:48:22 2013 -0500
# Node ID 70658c4039a143f9e6d69a5fa11a60780c85c345
# Parent  f4484e9bf540799f60922ab191d88e489b1f7adc
templater: add indentation arguments to the fill function

Patch

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -97,12 +97,12 @@ 
     return cgi.escape(text.replace('\0', ''), True)
 
 para_re = None
 space_re = None
 
-def fill(text, width):
-    '''fill many paragraphs.'''
+def fill(text, width, initindent, hangindent):
+    '''fill many paragraphs with optional indentation.'''
     global para_re, space_re
     if para_re is None:
         para_re = re.compile('(\n\n|\n\\s*[-*]\\s*)', re.M)
         space_re = re.compile(r'  +')
 
@@ -119,20 +119,21 @@ 
                        uctext[w:].encode(encoding.encoding))
                 break
             yield text[start:m.start(0)], m.group(1)
             start = m.end(1)
 
-    return "".join([space_re.sub(' ', util.wrap(para, width=width)) + rest
+    return "".join([util.wrap(space_re.sub(' ', util.wrap(para, width)),
+                              width, initindent, hangindent) + rest
                     for para, rest in findparas()])
 
 def fill68(text):
     """:fill68: Any text. Wraps the text to fit in 68 columns."""
-    return fill(text, 68)
+    return fill(text, 68, '', '')
 
 def fill76(text):
     """:fill76: Any text. Wraps the text to fit in 76 columns."""
-    return fill(text, 76)
+    return fill(text, 76, '', '')
 
 def firstline(text):
     """:firstline: Any text. Returns the first line of text."""
     try:
         return text.splitlines(True)[0].rstrip('\r\n')
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -297,22 +297,33 @@ 
     style = stringify(args[1][0](context, mapping, args[1][1]))
 
     return minirst.format(text, style=style)
 
 def fill(context, mapping, args):
-    if not (1 <= len(args) <= 2):
-        raise error.ParseError(_("fill expects one or two arguments"))
+    if not (1 <= len(args) <= 4):
+        raise error.ParseError(_("fill expects one to four arguments"))
 
     text = stringify(args[0][0](context, mapping, args[0][1]))
     width = 76
-    if len(args) == 2:
+    initindent = ''
+    hangindent = ''
+    if 2 <= len(args) <= 4:
         try:
             width = int(stringify(args[1][0](context, mapping, args[1][1])))
         except ValueError:
             raise error.ParseError(_("fill expects an integer width"))
+        try:
+            initindent = stringify(args[2][0](context, mapping, args[2][1]))
+            initindent = stringify(runtemplate(context, mapping,
+                                     compiletemplate(initindent, context)))
+            hangindent = stringify(args[3][0](context, mapping, args[3][1]))
+            hangindent = stringify(runtemplate(context, mapping,
+                                     compiletemplate(hangindent, context)))
+        except IndexError:
+            pass
 
-    return templatefilters.fill(text, width)
+    return templatefilters.fill(text, width, initindent, hangindent)
 
 def date(context, mapping, args):
     if not (1 <= len(args) <= 2):
         raise error.ParseError(_("date expects one or two arguments"))