Patchwork [1,of,5] templatefilters: add "upper" and "lower" for case conversion

login
register
mail settings
Submitter Yuya Nishihara
Date April 1, 2015, 1:25 p.m.
Message ID <8df3899c929cb405b037.1427894750@mimosa>
Download mbox | patch
Permalink /patch/8410/
State Accepted
Headers show

Comments

Yuya Nishihara - April 1, 2015, 1:25 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1427727269 -32400
#      Mon Mar 30 23:54:29 2015 +0900
# Node ID 8df3899c929cb405b0370b9538b6cb1e73062bc8
# Parent  1b97cc5d2272c272961cc3e1d738e521af012a40
templatefilters: add "upper" and "lower" for case conversion

Typically it will be used in patchbomb's flag template, which will be
implemented by future patches.

Patch

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -234,6 +234,10 @@  def localdate(text):
     """:localdate: Date. Converts a date to local date."""
     return (util.parsedate(text)[0], util.makedate()[1])
 
+def lower(text):
+    """:lower: Any text. Converts the text to lowercase."""
+    return encoding.lower(text)
+
 def nonempty(str):
     """:nonempty: Any text. Returns '(none)' if the string is empty."""
     return str or "(none)"
@@ -344,6 +348,10 @@  def tabindent(text):
     """
     return indent(text, '\t')
 
+def upper(text):
+    """:upper: Any text. Converts the text to uppercase."""
+    return encoding.upper(text)
+
 def urlescape(text):
     """:urlescape: Any text. Escapes all "special" characters. For example,
     "foo bar" becomes "foo%20bar".
@@ -387,6 +395,7 @@  filters = {
     "json": json,
     "jsonescape": jsonescape,
     "localdate": localdate,
+    "lower": lower,
     "nonempty": nonempty,
     "obfuscate": obfuscate,
     "permissions": permissions,
@@ -402,6 +411,7 @@  filters = {
     "strip": strip,
     "stripdir": stripdir,
     "tabindent": tabindent,
+    "upper": upper,
     "urlescape": urlescape,
     "user": userfilter,
     "emailuser": emailuser,
diff --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -1871,6 +1871,16 @@  Count filter:
   o  0: children: 1, tags: 0, file_adds: 1, ancestors: 1
   
 
+Upper/lower filters:
+
+  $ hg log -r0 --template '{branch|upper}\n'
+  DEFAULT
+  $ hg log -r0 --template '{author|lower}\n'
+  user name <user@hostname>
+  $ hg log -r0 --template '{date|upper}\n'
+  abort: template filter 'upper' is not compatible with keyword 'date'
+  [255]
+
 Error on syntax:
 
   $ echo 'x = "f' >> t