Beispiel #1
0
    def process(self, document, command):
        # BREAKPOINT >>> # import pdb; pdb.set_trace()  # <<< BREAKPOINT #
        
        # check arity
        if len(command.arguments) != self.cmd_arity:
            raise MacroError("Wrong macro-command arity for '{}': expected {} but given {} argument{}"
                             .format(self.cmd_name,                                                                                  
                                     self.cmd_arity,
                                     len(command.arguments),
                                     "s" if len(command.arguments) > 1 else ""))

        # argument expansion
        tpl_env = dict()
        for arg_num in range(1,self.cmd_arity+1):
            tpl_env['_'+str(arg_num)] = r"\macroCommandArgument[{}]".format(arg_num-1)
            
        tpl_env['options'] = command.cmd_opts

        # template rendering for the body
        self.cmd_template.compile()
        result_to_parse = self.cmd_template.render(tpl_env)
        
        # recursive parsing of template result
        from tangolib.parser import Parser
        parser = Parser()

        lex = parser.prepare_string_lexer(result_to_parse)
        doc = MacroCommandDocument(document, "<<<MacroCommand:{}>>>".format(self.cmd_name), self.cmd_start_pos, self.cmd_end_pos, lex)
        
        result_parsed = parser.parse(doc, macro_cmd_arguments=command.arguments)

        return result_parsed
Beispiel #2
0
    def process_header(self, document, env):
        
        # first: check arity
        if len(env.arguments) != self.env_arity:
            raise MacroError("Wrong macro-command arity for '{}': expected {} but given {} argument{}"
                             .format(self.env_name,                                                                                  
                                     self.env_arity,
                                     len(env.arguments),
                                     "s" if len(env.arguments) > 1 else ""))

        # second: template rendering
        env.template_env = dict()
        for arg_num in range(1,self.env_arity+1):
            env.template_env['_'+str(arg_num)] = r"\macroCommandArgument[{}]".format(arg_num-1)

        env.template_env['options'] = env.env_opts
        
        self.env_header_tpl.compile()

        result_to_parse = self.env_header_tpl.render(env.template_env)
        
        # third: recursive parsing of template result
        from tangolib.parser import Parser
        parser = Parser()

        lex = parser.prepare_string_lexer(result_to_parse)
        doc = MacroEnvDocument(document, "<<<MacroEnv:{}>>>".format(self.env_name), self.env_start_pos, self.env_end_pos, lex)
        
        result_parsed = parser.parse(doc, macro_cmd_arguments=env.arguments)

        return result_parsed
Beispiel #3
0
    def process_command(self, processor, cmd):
        if len(cmd.arguments) != 1:
            raise IncludeError("Cannot include document: expecting file name argument")            

        sub_filename = search_content_by_types(cmd.content,{"text","preformated"})
        if not sub_filename:
            raise IncludeError("Cannot find include pathname")
        else:
            sub_filename = sub_filename.text

        try:
            sub_file = open(sub_filename, "r")
        except OSError:
            raise IncludeError("Cannot open included file: {}".format(sub_filename))

        try:
            sub_input = sub_file.read()
        except IOError:
            raise IncludeError("Cannot read included file: {} (IO error)".format(sub_filename))
        finally:
            sub_file.close()
     

        from tangolib.parser import Parser
        parser = Parser()
        sub_lex = parser.prepare_string_lexer(sub_input)
        sub_doc = SubDocument(cmd.doc, sub_filename, cmd.start_pos, sub_lex)

        result_parsed = parser.parse(sub_doc)
   
        return (result_parsed, True)
Beispiel #4
0
    def process_footer(self, document, env):

        self.env_footer_tpl.compile()
        result_to_parse = self.env_footer_tpl.render(env.template_env)
        del env.template_env
        
        from tangolib.parser import Parser
        parser = Parser()

        lex = parser.prepare_string_lexer(result_to_parse)
        doc = MacroEnvFooterDocument(document, "<<<MacroEnvFooter:{}>>>".format(self.env_name), self.env_start_pos, self.env_end_pos, lex)
        
        result_parsed = parser.parse(doc, macro_cmd_arguments=env.arguments)

        return result_parsed