Login

Prettify HTML5 middleware

Author:
runejuhl
Posted:
July 8, 2011
Language:
Python
Version:
Not specified
Tags:
middleware tidy standard html5
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. HTML Validation Middleware by adamcik 6 years, 5 months ago
  2. prettify html by marchino 8 years, 3 months ago
  3. HTML5 Placeholder from FormField label by Hillshum 2 years, 9 months ago
  4. HTML 5 Firefox 2 Hack by epicserve 5 years, 8 months ago
  5. Custom Widget Types for HTML5 Form Fields by leveillej 5 years, 2 months ago

Comments

Please login first before commenting.