Skip to content

Commit 2c27d5c

Browse files
committed
Moved optional text wrapping before html escaping in markup_unicode(), used by get_unicode_document_content(). Fixes a problem with lines being wrapped when they should not be.
- Legacy-Id: 12480
1 parent eb966c7 commit 2c27d5c

6 files changed

Lines changed: 37 additions & 24 deletions

File tree

ietf/doc/templatetags/ietf_filters.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Copyright The IETF Trust 2007, All Rights Reserved
22

3-
import textwrap
43
import re
54
import datetime
65
import os
@@ -11,6 +10,7 @@
1110

1211
from ietf.doc.models import ConsensusDocEvent
1312
from ietf.doc.utils import get_document_content
13+
from ietf.utils.text import fill
1414
from django import template
1515
from django.conf import settings
1616
from django.utils.html import escape, fix_ampersands
@@ -164,23 +164,7 @@ def bracketpos(pos,posslug):
164164
else:
165165
return "[ ]"
166166

167-
@register.filter(name='fill')
168-
def fill(text, width):
169-
"""Wraps each paragraph in text (a string) so every line
170-
is at most width characters long, and returns a single string
171-
containing the wrapped paragraph.
172-
"""
173-
width = int(width)
174-
paras = text.replace("\r\n","\n").replace("\r","\n").split("\n\n")
175-
wrapped = []
176-
for para in paras:
177-
if para:
178-
lines = para.split("\n")
179-
maxlen = max([len(line) for line in lines])
180-
if maxlen > width:
181-
para = textwrap.fill(para, width, replace_whitespace=False)
182-
wrapped.append(para)
183-
return "\n\n".join(wrapped)
167+
register.filter('fill', fill)
184168

185169
@register.filter(name='rfcspace')
186170
def rfcspace(string):

ietf/doc/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ def add_events_message_info(events):
297297
e.in_reply_to = e.addedmessageevent.in_reply_to
298298

299299

300-
def get_unicode_document_content(key, filename, split=True, markup=True, codec='utf-8', errors='ignore'):
300+
def get_unicode_document_content(key, filename, split=True, markup=True, codec='utf-8', errors='ignore', width=None):
301301
try:
302302
with open(filename, 'rb') as f:
303303
raw_content = f.read().decode(codec,errors)
@@ -306,7 +306,7 @@ def get_unicode_document_content(key, filename, split=True, markup=True, codec='
306306
return error
307307

308308
if markup:
309-
return markup_txt.markup_unicode(raw_content, split)
309+
return markup_txt.markup_unicode(raw_content, split, width)
310310
else:
311311
return raw_content
312312

ietf/doc/views_doc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ def document_main(request, name, rev=None):
582582
if doc.type_id == "review":
583583
basename = "{}.txt".format(doc.name, doc.rev)
584584
pathname = os.path.join(doc.get_file_path(), basename)
585-
content = get_unicode_document_content(basename, pathname, split=False)
585+
content = get_unicode_document_content(basename, pathname, split=False, width=80)
586586

587587
review_req = ReviewRequest.objects.filter(review=doc.name).first()
588588

ietf/templates/doc/document_review.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,6 @@
113113
<h2>{{ doc.type.name }}<br><small>{{ doc.name }}</small></h2>
114114

115115
{% if doc.rev and content != None %}
116-
{{ content|fill:"80"|safe|linebreaksbr|keep_spacing|sanitize_html|safe }}
116+
{{ content|safe|keep_spacing|sanitize_html|safe }}
117117
{% endif %}
118118
{% endblock %}

ietf/utils/markup_txt.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
import string
3535
import re
3636

37-
def markup(content, split=True):
37+
from ietf.utils.text import fill
38+
39+
def markup(content, split=True, width=None):
3840
# normalize line endings to LF only
3941
content = content.replace("\r\n", "\n")
4042
content = content.replace("\r", "\n")
@@ -53,6 +55,10 @@ def markup(content, split=True):
5355
# remove runs of blank lines
5456
content = re.sub("\n\n\n+", "\n\n", content)
5557

58+
# maybe fill. This must be done before the escaping below.
59+
if width:
60+
content = fill(content, width)
61+
5662
# expand tabs + escape
5763
content = escape(content.expandtabs())
5864

@@ -72,7 +78,7 @@ def markup(content, split=True):
7278
else:
7379
return "<pre>" + content + "</pre>\n"
7480

75-
def markup_unicode(content, split=True):
81+
def markup_unicode(content, split=True, width=None):
7682
# normalize line endings to LF only
7783
content = content.replace("\r\n", "\n")
7884
content = content.replace("\r", "\n")
@@ -82,6 +88,10 @@ def markup_unicode(content, split=True):
8288
# remove runs of blank lines
8389
content = re.sub("\n\n\n+", "\n\n", content)
8490

91+
# maybe fill. This must be done before the escaping below.
92+
if width:
93+
content = fill(content, width)
94+
8595
# expand tabs + escape
8696
content = escape(content.expandtabs())
8797

ietf/utils/text.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import re
44
import unicodedata
5+
import textwrap
56

67
from django.utils.functional import allow_lazy
78
from django.utils import six
@@ -30,3 +31,21 @@ def strip_suffix(text, suffix):
3031
return text[:-len(suffix)]
3132
else:
3233
return text
34+
35+
def fill(text, width):
36+
"""Wraps each paragraph in text (a string) so every line
37+
is at most width characters long, and returns a single string
38+
containing the wrapped paragraph.
39+
"""
40+
width = int(width)
41+
paras = text.replace("\r\n","\n").replace("\r","\n").split("\n\n")
42+
wrapped = []
43+
for para in paras:
44+
if para:
45+
lines = para.split("\n")
46+
maxlen = max([len(line) for line in lines])
47+
if maxlen > width:
48+
para = textwrap.fill(para, width, replace_whitespace=False)
49+
wrapped.append(para)
50+
return "\n\n".join(wrapped)
51+

0 commit comments

Comments
 (0)