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
This commit is contained in:
Henrik Levkowetz 2016-12-08 16:27:05 +00:00
parent eb966c7545
commit 2c27d5c611
6 changed files with 37 additions and 24 deletions

View file

@ -1,6 +1,5 @@
# Copyright The IETF Trust 2007, All Rights Reserved
import textwrap
import re
import datetime
import os
@ -11,6 +10,7 @@ import debug # pyflakes:ignore
from ietf.doc.models import ConsensusDocEvent
from ietf.doc.utils import get_document_content
from ietf.utils.text import fill
from django import template
from django.conf import settings
from django.utils.html import escape, fix_ampersands
@ -164,23 +164,7 @@ def bracketpos(pos,posslug):
else:
return "[ ]"
@register.filter(name='fill')
def fill(text, width):
"""Wraps each paragraph in text (a string) so every line
is at most width characters long, and returns a single string
containing the wrapped paragraph.
"""
width = int(width)
paras = text.replace("\r\n","\n").replace("\r","\n").split("\n\n")
wrapped = []
for para in paras:
if para:
lines = para.split("\n")
maxlen = max([len(line) for line in lines])
if maxlen > width:
para = textwrap.fill(para, width, replace_whitespace=False)
wrapped.append(para)
return "\n\n".join(wrapped)
register.filter('fill', fill)
@register.filter(name='rfcspace')
def rfcspace(string):

View file

@ -297,7 +297,7 @@ def add_events_message_info(events):
e.in_reply_to = e.addedmessageevent.in_reply_to
def get_unicode_document_content(key, filename, split=True, markup=True, codec='utf-8', errors='ignore'):
def get_unicode_document_content(key, filename, split=True, markup=True, codec='utf-8', errors='ignore', width=None):
try:
with open(filename, 'rb') as f:
raw_content = f.read().decode(codec,errors)
@ -306,7 +306,7 @@ def get_unicode_document_content(key, filename, split=True, markup=True, codec='
return error
if markup:
return markup_txt.markup_unicode(raw_content, split)
return markup_txt.markup_unicode(raw_content, split, width)
else:
return raw_content

View file

@ -582,7 +582,7 @@ def document_main(request, name, rev=None):
if doc.type_id == "review":
basename = "{}.txt".format(doc.name, doc.rev)
pathname = os.path.join(doc.get_file_path(), basename)
content = get_unicode_document_content(basename, pathname, split=False)
content = get_unicode_document_content(basename, pathname, split=False, width=80)
review_req = ReviewRequest.objects.filter(review=doc.name).first()

View file

@ -113,6 +113,6 @@
<h2>{{ doc.type.name }}<br><small>{{ doc.name }}</small></h2>
{% if doc.rev and content != None %}
{{ content|fill:"80"|safe|linebreaksbr|keep_spacing|sanitize_html|safe }}
{{ content|safe|keep_spacing|sanitize_html|safe }}
{% endif %}
{% endblock %}

View file

@ -34,7 +34,9 @@ from django.utils.html import escape
import string
import re
def markup(content, split=True):
from ietf.utils.text import fill
def markup(content, split=True, width=None):
# normalize line endings to LF only
content = content.replace("\r\n", "\n")
content = content.replace("\r", "\n")
@ -53,6 +55,10 @@ def markup(content, split=True):
# remove runs of blank lines
content = re.sub("\n\n\n+", "\n\n", content)
# maybe fill. This must be done before the escaping below.
if width:
content = fill(content, width)
# expand tabs + escape
content = escape(content.expandtabs())
@ -72,7 +78,7 @@ def markup(content, split=True):
else:
return "<pre>" + content + "</pre>\n"
def markup_unicode(content, split=True):
def markup_unicode(content, split=True, width=None):
# normalize line endings to LF only
content = content.replace("\r\n", "\n")
content = content.replace("\r", "\n")
@ -82,6 +88,10 @@ def markup_unicode(content, split=True):
# remove runs of blank lines
content = re.sub("\n\n\n+", "\n\n", content)
# maybe fill. This must be done before the escaping below.
if width:
content = fill(content, width)
# expand tabs + escape
content = escape(content.expandtabs())

View file

@ -2,6 +2,7 @@ from __future__ import unicode_literals
import re
import unicodedata
import textwrap
from django.utils.functional import allow_lazy
from django.utils import six
@ -30,3 +31,21 @@ def strip_suffix(text, suffix):
return text[:-len(suffix)]
else:
return text
def fill(text, width):
"""Wraps each paragraph in text (a string) so every line
is at most width characters long, and returns a single string
containing the wrapped paragraph.
"""
width = int(width)
paras = text.replace("\r\n","\n").replace("\r","\n").split("\n\n")
wrapped = []
for para in paras:
if para:
lines = para.split("\n")
maxlen = max([len(line) for line in lines])
if maxlen > width:
para = textwrap.fill(para, width, replace_whitespace=False)
wrapped.append(para)
return "\n\n".join(wrapped)