|
6 | 6 | import types |
7 | 7 | from email.utils import parseaddr |
8 | 8 |
|
9 | | -import debug # pyflakes:ignore |
10 | | - |
11 | | -from ietf.doc.models import ConsensusDocEvent |
12 | | -from ietf.doc.utils import get_document_content |
13 | | -from ietf.utils.text import fill |
14 | 9 | from django import template |
15 | 10 | from django.conf import settings |
16 | 11 | from django.utils.html import escape |
17 | 12 | from django.template.defaultfilters import truncatewords_html, linebreaksbr, stringfilter, striptags, urlize |
18 | 13 | from django.utils.safestring import mark_safe, SafeData |
19 | 14 | from django.utils.html import strip_tags |
20 | 15 |
|
| 16 | +import debug # pyflakes:ignore |
| 17 | + |
| 18 | +from ietf.doc.models import ConsensusDocEvent |
| 19 | +from ietf.doc.utils import get_document_content |
| 20 | +from ietf.utils.text import wordwrap, fill, wrap_text_if_unwrapped |
| 21 | + |
| 22 | + |
21 | 23 | register = template.Library() |
22 | 24 |
|
23 | 25 | def collapsebr(html): |
@@ -254,64 +256,9 @@ def truncate_ellipsis(text, arg): |
254 | 256 | def split(text, splitter=None): |
255 | 257 | return text.split(splitter) |
256 | 258 |
|
257 | | -@register.filter(name="wrap_long_lines") |
258 | | -def wrap_long_lines(text, width=72): |
259 | | - """Wraps long lines without loosing the formatting and indentation |
260 | | - of short lines""" |
261 | | - if not isinstance(text, (types.StringType,types.UnicodeType)): |
262 | | - return text |
263 | | - text = re.sub(" *\r\n", "\n", text) # get rid of DOS line endings |
264 | | - text = re.sub(" *\r", "\n", text) # get rid of MAC line endings |
265 | | - text = re.sub("( *\n){3,}", "\n\n", text) # get rid of excessive vertical whitespace |
266 | | - lines = text.split("\n") |
267 | | - filled = [] |
268 | | - wrapped = False |
269 | | - for line in lines: |
270 | | - if wrapped and line.strip() != "": |
271 | | - line = filled[-1] + " " + line |
272 | | - filled = filled[:-1] |
273 | | - else: |
274 | | - wrapped = False |
275 | | - while (len(line) > 80) and (" " in line[:80]): |
276 | | - wrapped = True |
277 | | - breakpoint = line.rfind(" ",0,80) |
278 | | - filled += [ line[:breakpoint] ] |
279 | | - line = line[breakpoint+1:] |
280 | | - filled += [ line.rstrip() ] |
281 | | - return "\n".join(filled) |
282 | | - |
283 | | -@register.filter(name="wrap_text") |
284 | | -def wrap_text(text, width=72): |
285 | | - """Wraps long lines without loosing the formatting and indentation |
286 | | - of short lines""" |
287 | | - if not isinstance(text, (types.StringType,types.UnicodeType)): |
288 | | - return text |
289 | | - text = re.sub(" *\r\n", "\n", text) # get rid of DOS line endings |
290 | | - text = re.sub(" *\r", "\n", text) # get rid of MAC line endings |
291 | | - text = re.sub("( *\n){3,}", "\n\n", text) # get rid of excessive vertical whitespace |
292 | | - lines = text.split("\n") |
293 | | - filled = [] |
294 | | - wrapped = False |
295 | | - prev_indent = None |
296 | | - for line in lines: |
297 | | - line = line.expandtabs() |
298 | | - indent = " " * (len(line) - len(line.lstrip())) |
299 | | - if wrapped and line.strip() != "" and indent == prev_indent: |
300 | | - line = filled[-1] + " " + line.lstrip() |
301 | | - filled = filled[:-1] |
302 | | - else: |
303 | | - wrapped = False |
304 | | - while (len(line) > width) and (" " in line[:width]): |
305 | | - linelength = len(line) |
306 | | - wrapped = True |
307 | | - breakpoint = line.rfind(" ",0,width) |
308 | | - filled += [ line[:breakpoint] ] |
309 | | - line = indent + line[breakpoint+1:] |
310 | | - if len(line) >= linelength: |
311 | | - break |
312 | | - filled += [ line.rstrip() ] |
313 | | - prev_indent = indent |
314 | | - return "\n".join(filled) |
| 259 | +register.filter("maybewordwrap", stringfilter(wrap_text_if_unwrapped)) |
| 260 | + |
| 261 | +register.filter("wordwrap", stringfilter(wordwrap)) |
315 | 262 |
|
316 | 263 | @register.filter(name="compress_empty_lines") |
317 | 264 | def compress_empty_lines(text): |
|
0 commit comments