Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 0fc42b607e06676ef23041853c02ff5b1375f8aa 0 parents
@natw authored
Showing with 99 additions and 0 deletions.
  1. +33 −0 README
  2. +66 −0 hgextendedfilters.py
33 README
@@ -0,0 +1,33 @@
+HG Extended Filters
+by Nat Williams
+
+This is just a few nice filters for making writing mercurial templates or
+style files less painful.
+
+
+trailingspace
+-------------
+
+The trailingspace filter returns the item with the addition of a trailing
+space, but only if the item is not empty. This is useful for items like
+{tags} that aren't necessarily meaningful for every changeset.
+
+
+parens, squarebrackets, anglebrackets
+-------------------------------------
+
+These three filters return the item surrounded in their namesake bracket.
+Like trailingspace, they all return an empty string if the item itself
+is also empty.
+
+
+colors
+------
+
+Rather than have to manually put horrible color codes into your templates,
+just use these filters to colorize items.
+
+Recognized color names are:
+ black, red, green, brown, blue, purple, cyan, light_gray, dark_gray,
+ light_red, light_green, yellow, light_blue, light_purple, light_cyan,
+ white, and reset.
66 hgextendedfilters.py
@@ -0,0 +1,66 @@
+# hgextendedfilters.py - A collection of extra template filters for Mercurial
+#
+# Copyright 2011 Nat Williams <nat.williams@gmail.com>
+#
+# Since hg is GPL2, I'm guessing that this is too. So blah blah blah whatever
+# that entails
+
+from functools import partial
+
+from mercurial import templatefilters
+
+
+def trailingspace(thing):
+ """Template filter to add trailing space after non-empty item.
+ Note that this filter implicitly calls stringify on the item.
+
+ """
+ thing = templatefilters.filters['stringify'](thing)
+ if thing:
+ return thing + " "
+ else:
+ return thing
+
+
+def surround(thing, left, right):
+ thing = templatefilters.filters['stringify'](thing)
+ if thing:
+ return "%s%s%s" % (left, thing, right)
+ else:
+ return thing
+
+
+color_codes = {
+ 'black': '0;30',
+ 'red': '0;31',
+ 'green': '0;32',
+ 'brown': '0;33',
+ 'blue': '0;34',
+ 'purple': '0;35',
+ 'cyan': '0;36',
+ 'light_gray': '0;37',
+ 'dark_gray': '1;30',
+ 'light_red': '1;31',
+ 'light_green': '1;32',
+ 'yellow': '1;33',
+ 'light_blue': '1;34',
+ 'light_purple': '1;35',
+ 'light_cyan': '1;36',
+ 'white': '1;37',
+ 'reset': '0',
+}
+
+def colorize(text, color):
+ # TODO: make this controled by the color plugin?
+ return '\033[%sm%s\033[%sm' % (color_codes[color], text,
+ color_codes['reset'])
+
+def reposetup(ui, repo):
+ templatefilters.filters.update({
+ 'trailingspace': addtrailingspace,
+ 'parens': lambda x: surround(x, '(', ')'),
+ 'squarebrackets': lambda x: surround(x, '[', ']'),
+ 'anglebrackets': lambda x: surround(x, '<', '>'),
+ })
+ for code in color_codes:
+ templatefilters.filters[code] = partial(colorize, color=code)
Please sign in to comment.
Something went wrong with that request. Please try again.