From 264e4f6b3071dc1267e78c9cf40188a1b46add63 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Wed, 22 Feb 2017 21:52:59 +0000 Subject: [PATCH] protect_start_spaces --- sourcing/utils.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/sourcing/utils.py b/sourcing/utils.py index 701e875..1ad86fe 100644 --- a/sourcing/utils.py +++ b/sourcing/utils.py @@ -1,5 +1,6 @@ import humanize from datetime import date, timedelta +from jinja2 import Markup def display_datetime(dt): if dt is None: @@ -10,4 +11,16 @@ def display_datetime(dt): else: return dt.strftime('%a, %d %b %Y') +def nbsp_at_start(line): + ''' Protect spaces at the start of a string. ''' + space_count = 0 + for c in line: + if c != ' ': + break + space_count += 1 + # return Markup(' ') * space_count + line[space_count:] + return '\u00A0' * space_count + line[space_count:] + +def protect_start_spaces(text): + return '\n'.join(nbsp_at_start(line) for line in text.splitlines())