1 from django.utils.encoding import force_unicode
2 from django.utils.functional import allow_lazy
6 def splitline(line, prefix_regex):
8 Splits the line into a prefix and the rest of the line, using the given
9 regex. The regex should contain exactly two groups. If no match was
10 found, the prefix is assumed empty.
12 match = re.findall(re.compile(prefix_regex), line)
13 if match and len(match) == 1 and len(match[0]) == 2:
18 def rewrap(text, width, prefix_regex):
20 Rewraps the given text into the given width, properly preserving the
21 prefix identified by prefix_regex. This is similar to vim's gqgq
24 This command tries to preserve any empty lines in input (also lines
25 only containing a prefix). Additionally, unprefixed lines that are
26 longer than width are taken to be paragraphs, and are separated by
27 an empty line after wrapping.
29 The aim of this command is to get proper formatting when wrapping a
30 text and then adding a prefix to every line, such as is customary
33 prefix_regex should be a regex that defines exactly two groups, the
34 first of which matches the prefix and the second matches the rest of
37 text = force_unicode(text)
41 # We want to know if we're in the previous line wrapped
43 for line in text.split('\n'):
44 # Save the previous prefix and find the next one
46 (prefix, line) = splitline(line, prefix_regex)
48 # Unprefixed lines in the input are paragraphs and should be
49 # separated by an empty line to make sure they don't get
50 # merged after being prefixed later on. This only happens
51 # when we have two non-prefixed lines after each other, the
52 # second is not an empty line and the first line was long
53 # enough to wrap (to preserver already properly hardwrapped
55 if wrapped and oldprefix == '' and prefix == '' and line:
59 # Keep track if this line wrapped
62 # Preserver empty lines in the input
68 # Fake the prefix to be empty, so any prefix in the next
69 # line will be yielded first
73 # New line has a different prefix? Terminate line if needed,
74 # and yield the new prefix
75 if prefix != oldprefix:
81 for word in line.split(' '):
82 if length != 0 and len(prefix) + length + len(word) > width:
83 # This word would go over width, terminate the line.
93 return u''.join(_generator())
94 rewrap = allow_lazy(rewrap, unicode)