Login

Prettify HTML5 middleware

Author:
runejuhl
Posted:
July 8, 2011
Language:
Python
Version:
Not specified
Score:
1 (after 1 ratings)

Slightly updated version of http://djangosnippets.org/snippets/172/ . Supports new HTML5 tags (even though tidy doesn't).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import tidy

options = dict(
		add_xml_decl=False,
		doctype='omit',
		indent=1,
		tidy_mark=0,
		hide_comments=False,
		wrap=100,
		new_blocklevel_tags='article,header,footer,section,nav',
		new_inline_tags='video,audio,canvas,ruby,rt,rp',
		input_xml=1,
		output_xhtml=1,
		char_encoding='utf8',
)

class PrettifyMiddleware(object):
	"""Prettify middleware"""

	def process_response(self, request, response):
		if response.__getitem__('Content-Type').split(';', 1)[0] == 'text/html':
			content = response.content
			content = str(tidy.parseString(content, **options))
			response.content = content
		return response

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.