Mercurial > public > mercurial-scm > hg
comparison mercurial/templatefilters.py @ 49021:51aed118f9dc
templates: extract function to `stringutil` for getting first line of text
It's surprisingly hard to get the first line from a string, so let's
have our own function in `stringutil` for it.
Differential Revision: https://phab.mercurial-scm.org/D12404
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Thu, 24 Mar 2022 16:09:12 -0700 |
parents | 1138674ecdb8 |
children | d44e3c45f0e4 |
comparison
equal
deleted
inserted
replaced
49020:1138674ecdb8 | 49021:51aed118f9dc |
---|---|
266 | 266 |
267 | 267 |
268 @templatefilter(b'firstline', intype=bytes) | 268 @templatefilter(b'firstline', intype=bytes) |
269 def firstline(text): | 269 def firstline(text): |
270 """Any text. Returns the first line of text.""" | 270 """Any text. Returns the first line of text.""" |
271 try: | 271 return stringutil.firstline(text) |
272 return text.splitlines()[0] | |
273 except IndexError: | |
274 return b'' | |
275 | 272 |
276 | 273 |
277 @templatefilter(b'hex', intype=bytes) | 274 @templatefilter(b'hex', intype=bytes) |
278 def hexfilter(text): | 275 def hexfilter(text): |
279 """Any text. Convert a binary Mercurial node identifier into | 276 """Any text. Convert a binary Mercurial node identifier into |