#NEWLINE# @ register . tag ( name = 'macro' ) #NEWLINE# def do_macro( parser , token ) : #NEWLINE# #INDENT# ' the function taking the parsed tag and returning\n    a DefineMacroNode object.\n    ' #NEWLINE# try : #NEWLINE# #INDENT# bits = token . split_contents ( ) #NEWLINE# ( tag_name , macro_name , arguments ) = ( bits [ 0 ] , bits [ 1 ] , bits [ 2 : ] ) #NEWLINE# #UNINDENT# except IndexError : #NEWLINE# #INDENT# raise template . TemplateSyntaxError ( "'{0}' tag requires at least one argument (macro name)" . format ( token . contents . split ( ) [ 0 ] ) ) #NEWLINE# #UNINDENT# arg_regex = '^([A-Za-z_][\\w_]*)$' #NEWLINE# kwarg_regex = '^([A-Za-z_][\\w_]*)=(".*"|{0}.*{0}|[A-Za-z_][\\w_]*)$' . format ( "'" ) #NEWLINE# args = [ ] #NEWLINE# kwargs = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for argument in arguments : #NEWLINE# #INDENT# arg_match = regex_match ( arg_regex , argument ) #NEWLINE# if arg_match : #NEWLINE# #INDENT# args . append ( arg_match . groups ( ) [ 0 ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# kwarg_match = regex_match ( kwarg_regex , argument ) #NEWLINE# if kwarg_match : #NEWLINE# #INDENT# kwargs [ kwarg_match . groups ( ) [ 0 ] ] = template . Variable ( kwarg_match . groups ( ) [ 1 ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise template . TemplateSyntaxError ( 'Malformed arguments to the {0} tag.' . format ( tag_name ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# nodelist = parser . parse ( ( 'endmacro' , ) ) #NEWLINE# parser . delete_first_token ( ) #NEWLINE# _setup_macros_dict ( parser ) #NEWLINE# parser . _macros [ macro_name ] = DefineMacroNode ( macro_name , nodelist , args , kwargs ) #NEWLINE# return parser . _macros [ macro_name ]