Skip to content

Commit

Permalink
Introduce a word-wrapping filter that checks if there are lines (cont…
Browse files Browse the repository at this point in the history
…aining spaces to not

count URLs) longer than 100 characters, and only wraps the text if that's the case, to 
prevent messing up pre-wrapped text. Use this filter in the review email code.

Branch ready for merge.
 - Legacy-Id: 13495
  • Loading branch information
OleLaursen committed Jun 2, 2017
1 parent be28c2b commit 1a59cf6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
14 changes: 14 additions & 0 deletions ietf/doc/templatetags/ietf_filters.py
Expand Up @@ -17,6 +17,7 @@
from django.template.defaultfilters import truncatewords_html, linebreaksbr, stringfilter, striptags, urlize
from django.utils.safestring import mark_safe, SafeData
from django.utils.html import strip_tags
from django.utils.text import wrap

register = template.Library()

Expand Down Expand Up @@ -313,6 +314,19 @@ def wrap_text(text, width=72):
prev_indent = indent
return "\n".join(filled)

@register.filter
def wrap_text_if_unwrapped(text, width=72, max_tolerated_line_length=100):
text = re.sub(" *\r\n", "\n", text) # get rid of DOS line endings
text = re.sub(" *\r", "\n", text) # get rid of MAC line endings

contains_long_lines = any(" " in l and len(l) > max_tolerated_line_length
for l in text.split("\n"))

if contains_long_lines:
return wrap(text, width)
else:
return text

@register.filter(name="compress_empty_lines")
def compress_empty_lines(text):
text = re.sub("( *\n){3,}", "\n\n", text)
Expand Down
4 changes: 2 additions & 2 deletions ietf/doc/tests_review.py
Expand Up @@ -660,7 +660,7 @@ def test_partially_complete_review(self):
"state": ReviewRequestStateName.objects.get(slug="part-completed").pk,
"reviewed_rev": review_req.doc.rev,
"review_submission": "enter",
"review_content": "This is a review\nwith two lines",
"review_content": "This is a review with a somewhat long line spanning over 80 characters to test word wrapping\nand another line",
})
self.assertEqual(r.status_code, 302)

Expand Down Expand Up @@ -694,7 +694,7 @@ def test_partially_complete_review(self):
"state": ReviewRequestStateName.objects.get(slug="completed").pk,
"reviewed_rev": review_req.doc.rev,
"review_submission": "enter",
"review_content": "This is another review\nwith\nthree lines",
"review_content": "This is another review with a really, really, really, really, really, really, really, really, really, really long line",
})
self.assertEqual(r.status_code, 302)

Expand Down
2 changes: 1 addition & 1 deletion ietf/templates/review/completed_review.txt
@@ -1,4 +1,4 @@
{% autoescape off %}{% filter wordwrap:70 %}{% if review_req.state_id == "part-completed" %}Review is partially done. Another review request has been registered for completing it.
{% autoescape off %}{% load ietf_filters %}{% filter wrap_text_if_unwrapped:80 %}{% if review_req.state_id == "part-completed" %}Review is partially done. Another review request has been registered for completing it.

{% endif %}Reviewer: {{ review_req.reviewer.person }}
Review result: {{ review_req.result.name }}
Expand Down

0 comments on commit 1a59cf6

Please sign in to comment.