# HG changeset patch # User Jesse Glick # Date 1201576752 18000 # Node ID 434139080ed4007d99e843b203c7da0850238744 # Parent 963000ed8cacd72310a54fb5956f3ab3d3e21997 Permit XML entities to be escaped in template output. Useful for creating XML documents directly from Hg logging. Can also be used for HTML. For use in content, will escape '&', '<', and for completeness '>' (although it is not strictly necessary). For use in attributes, will also escape ' and ". Will also replace nonprinting (ASCII) control characters with spaces, since these are illegal in XML. diff -r 963000ed8cac -r 434139080ed4 mercurial/templatefilters.py --- a/mercurial/templatefilters.py Sun Feb 24 17:58:55 2008 +0100 +++ b/mercurial/templatefilters.py Mon Jan 28 22:19:12 2008 -0500 @@ -121,6 +121,15 @@ return "-rwxr-xr-x" return "-rw-r--r--" +def xmlescape(text): + text = (text + .replace('&', '&') + .replace('<', '<') + .replace('>', '>') + .replace('"', '"') + .replace("'", ''')) # ' invalid in HTML + return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text) + filters = { "addbreaks": nl2br, "basename": os.path.basename, @@ -147,5 +156,6 @@ "urlescape": lambda x: urllib.quote(x), "user": lambda x: util.shortuser(x), "stringescape": lambda x: x.encode('string_escape'), + "xmlescape": xmlescape, } diff -r 963000ed8cac -r 434139080ed4 tests/test-command-template --- a/tests/test-command-template Sun Feb 24 17:58:55 2008 +0100 +++ b/tests/test-command-template Mon Jan 28 22:19:12 2008 -0500 @@ -106,6 +106,7 @@ hg log --template '{date|rfc822date}\n' hg log --template '{desc|firstline}\n' hg log --template '{node|short}\n' +hg log --template '\n' echo '# formatnode filter works' echo '# quiet' diff -r 963000ed8cac -r 434139080ed4 tests/test-command-template.out --- a/tests/test-command-template.out Sun Feb 24 17:58:55 2008 +0100 +++ b/tests/test-command-template.out Mon Jan 28 22:19:12 2008 -0500 @@ -541,6 +541,14 @@ 97054abb4ab8 b608e9d1a3f0 1e4e1b8f71e0 + + + + + + + + # formatnode filter works # quiet 1e4e1b8f71e0