Login

prettify html

Author:
marchino
Posted:
April 11, 2007
Language:
Python
Version:
.96
Tags:
html xhtml tidy standard
Score:
8 (after 8 ratings)

with this middleware you can use tidy to prettify your html, just add the class to the MIDDLEWARE_CLASSES setting.

tidy has an enormous number of options, se Tidy Options Reference .

you must have ┬ÁTidylib installed.

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

options = dict(output_xhtml=True,
               add_xml_decl=True,
               doctype='strict',
               indent='auto',
               tidy_mark=False,
               hide_comments=True,
               wrap=100)


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

    def process_response(self, request, response):
        if response.headers['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. Prettify HTML body contents in HTTP response by n1k0 4 years, 6 months ago
  2. Prettify HTML5 middleware by runejuhl 3 years, 10 months ago
  3. Ultimate(?) export/download CSV admin action by anentropic 1 year, 8 months ago
  4. HTML Prettifier by Eloff 7 years, 4 months ago
  5. HTML Validation Middleware by adamcik 6 years, 3 months ago

Comments

henriklied (on April 12, 2007):

This is great, I needed this for a upcoming project. Thanks!

#

whiteinge (on April 12, 2007):

I wonder what the performance hit is. Still, this is awesome. Thanks!

#

luckystarr (on September 10, 2007):

Has problems with unicode though... messes up all my umlauts.

#

findlay (on May 12, 2008):

In the latest Django SVN (0.97-pre-SVN-7523) line 16 is just

if response['Content-Type'].split(';', 1)[0] == 'text/html':

#

Please login first before commenting.