#NEWLINE# def format_block( block , nlspaces = 0 ) : #NEWLINE# #INDENT# 'Format the given block of text, trimming leading/trailing\n    empty lines and any leading whitespace that is common to all lines.\n    The purpose is to let us list a code block as a multiline,\n    triple-quoted Python string, taking care of\n    indentation concerns.\n    http://code.activestate.com/recipes/145672/' #NEWLINE# import re #NEWLINE# lines = smart_text ( block ) . split ( '\n' ) #NEWLINE# while ( lines and ( not lines [ 0 ] ) ) : #NEWLINE# #INDENT# del lines [ 0 ] #NEWLINE# #UNINDENT# while ( lines and ( not block [ ( - 1 ) ] ) ) : #NEWLINE# #INDENT# del lines [ ( - 1 ) ] #NEWLINE# #UNINDENT# ws = re . match ( '\\s*' , lines [ 0 ] ) . group ( 0 ) #NEWLINE# if ws : #NEWLINE# #INDENT# lines = map ( ( lambda x : x . replace ( ws , '' , 1 ) ) , lines ) #NEWLINE# #UNINDENT# while ( lines and ( not lines [ 0 ] ) ) : #NEWLINE# #INDENT# del lines [ 0 ] #NEWLINE# #UNINDENT# while ( lines and ( not lines [ ( - 1 ) ] ) ) : #NEWLINE# #INDENT# del lines [ ( - 1 ) ] #NEWLINE# #UNINDENT# flines = [ ( '%s%s' % ( ( ' ' * nlspaces ) , line ) ) for line in lines ] #NEWLINE# return ( '\n' . join ( flines ) + '\n' )