#!/usr/bin/python '''Minify XHTML Simple filter. This simple XHTML minifier removes only indentation and comments. The reason I wrote this is that the other HTML minifiers caused validation errors. ''' #import sys no_minify = [ ('
') # Start/stop patterns. ] def engine(get_input, set_output): '''get_input is a function that returns a string (one line) with a trailing newline. set_output is a function that takes one argument: string. Returns when get_input returns empty string. ''' nop_mode = False comment = False in_str = get_input() if not in_str: return #Comment # Leave? #No comment # Does a comment begin on this line? # nop_mode # Leave nop_mode? # normal mode # Enter nop_mode? # Did a comment begin on this line? while True: # Get input it before the loop and in the end, simplifies # nop_mode changes. if comment: # Leave comment mode? ptr = in_str.find('-->') if ptr >= 0: in_str = in_str[ptr + 3:] comment = False continue else: # Does a comment begin on this line? comment_ptr = in_str.find('