<s> from __future__ import absolute_import , unicode_literals <EOL> import re <EOL> import tidylib <EOL> from django . utils import six <EOL> try : <EOL> unichr <EOL> except NameError : <EOL> unichr = chr <EOL> CONTROL_CHAR_RE = re . compile ( '<STR_LIT>' % "<STR_LIT>" . join ( <EOL> re . escape ( unichr ( c ) ) for c in range ( <NUM_LIT:0> , <NUM_LIT:8> ) + range ( <NUM_LIT> , <NUM_LIT> ) + range ( <NUM_LIT> , <NUM_LIT> ) <EOL> ) ) <EOL> def tidy_html ( html ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( html , six . text_type ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> errors = list ( ) <EOL> warnings = list ( ) <EOL> html , sub_count = CONTROL_CHAR_RE . subn ( "<STR_LIT:U+0020>" , html ) <EOL> if sub_count : <EOL> warnings . append ( "<STR_LIT>" % ( <EOL> sub_count , <EOL> set ( ord ( i ) for i in CONTROL_CHAR_RE . findall ( html ) ) <EOL> ) ) <EOL> if "<STR_LIT>" in html [ : <NUM_LIT> ] : <EOL> tidy_f = tidylib . tidy_document <EOL> doc_mode = True <EOL> else : <EOL> tidy_f = tidylib . tidy_fragment <EOL> doc_mode = False <EOL> html , messages = tidy_f ( html . strip ( ) , { <EOL> "<STR_LIT>" : "<STR_LIT:utf8>" , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : True , <EOL> } <EOL> ) <EOL> messages = filter ( None , ( l . strip ( ) for l in messages . split ( "<STR_LIT:\n>" ) if l ) ) <EOL> for msg in messages : <EOL> if not doc_mode and "<STR_LIT>" in msg : <EOL> continue <EOL> if ( not doc_mode <EOL> and "<STR_LIT>" in msg ) : <EOL> continue <EOL> if not doc_mode and "<STR_LIT>" in msg : <EOL> continue <EOL> if "<STR_LIT>" in msg : <EOL> errors . append ( msg ) <EOL> else : <EOL> warnings . append ( msg ) <EOL> return html , errors , warnings </s>
