Skip to content

Commit 65b3f93

Browse files
committed
Convert markdown to html if Accept header prioritizes text/html over text/markdown. Fixes ietf-tools#1926. Commit ready for merge.
- Legacy-Id: 16564
1 parent f76c46b commit 65b3f93

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

ietf/meeting/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,21 @@ def is_nomcom_eligible(person, date=datetime.date.today()):
160160
is_iab = person.role_set.filter(group__acronym='iab',name_id__in=['member','chair']).exists()
161161
is_iaoc = person.role_set.filter(group__acronym='iaoc',name_id__in=['member','chair']).exists()
162162
return len(attended)>=3 and not (is_iesg or is_iab or is_iaoc)
163+
164+
def sort_accept_tuple(accept):
165+
tup = []
166+
if accept:
167+
accept_types = accept.split(',')
168+
for at in accept_types:
169+
keys = at.split(';', 1)
170+
q = 1.0
171+
if len(keys) != 1:
172+
qlist = keys[1].split('=', 1)
173+
if len(qlist) == 2:
174+
try:
175+
q = float(qlist[1])
176+
except ValueError:
177+
q = 0.0
178+
tup.append((keys[0], q))
179+
return sorted(tup, key = lambda x: float(x[1]), reverse = True)
180+
return tup

ietf/meeting/views.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import re
1515
import six
1616
import tarfile
17+
import markdown2
1718

1819

1920
from calendar import timegm
@@ -67,6 +68,7 @@
6768
from ietf.meeting.helpers import send_interim_approval_request
6869
from ietf.meeting.helpers import send_interim_announcement_request
6970
from ietf.meeting.utils import finalize
71+
from ietf.meeting.utils import sort_accept_tuple
7072
from ietf.message.utils import infer_message
7173
from ietf.secr.proceedings.utils import handle_upload_file
7274
from ietf.secr.proceedings.proc_utils import (get_progress_stats, post_process, import_audio_files,
@@ -210,6 +212,19 @@ def materials_document(request, document, num=None, ext=None):
210212

211213
mtype, chset = get_mime_type(bytes)
212214
content_type = "%s; %s" % (mtype, chset)
215+
216+
file_ext = os.path.splitext(filename)
217+
if len(file_ext) == 2 and file_ext[1] == '.md' and mtype == 'text/plain':
218+
sorted_accept = sort_accept_tuple(request.META.get('HTTP_ACCEPT'))
219+
for atype in sorted_accept:
220+
if atype[0] == 'text/markdown':
221+
content_type = content_type.replace('plain', 'markdown', 1)
222+
break;
223+
elif atype[0] == 'text/html':
224+
bytes = "<html>\n<head></head>\n<body>\n%s\n</body>\n</html>\n" % markdown2.markdown(bytes)
225+
content_type = content_type.replace('plain', 'html', 1)
226+
break;
227+
213228
response = HttpResponse(bytes, content_type=content_type)
214229
response['Content-Disposition'] = 'inline; filename="%s"' % basename
215230
return response

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ Unidecode>=0.4.18
6363
xml2rfc>=2.9.3,!=2.6.0
6464
xym==0.4.2,<1.0
6565
#zxcvbn-python>=4.4.14 # Not needed until we do back-end password entropy validation
66-
66+
markdown2>=2.3.8

0 commit comments

Comments
 (0)