示例#1
0
文件: macros.py 项目: fredokun/tango
    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
示例#2
0
    def test_macro_ev_nested_replace(self):
        parser = Parser()

        doc = parser.parse_from_string(r"""

% macro definition
\defEnv{new}[0]{New}{Old}
\defEnv{hello}[1]{hello #1}{#1 world}

% macro expansion
\begin{hello}{\begin{new}
    crazy
\end{new}}
brave
\end{hello} 

""")

        print("env_nested_replace (before expansion) = {}".format(doc))

        processor = DocumentProcessor(doc)

        # BREAKPOINT >>> # import pdb; pdb.set_trace()  # <<< BREAKPOINT #
        processor.process()

        print("env_nested_replace (after expansion) = {}".format(doc))
示例#3
0
    def test_macro_simple_text(self):
        parser = Parser()

        doc = parser.parse_from_string(r"""

% macro definition
\defCommand{\hello}[0]{brave world}
\defCommand{\hello2}{brave world 2}

% macro expansion
Hello \hello 

% macro expansion
Hello \hello2 

""")

        print("simple_text (before expansion) = {}".format(doc))

        processor = DocumentProcessor(doc)

        # BREAKPOINT >>> # import pdb; pdb.set_trace()  # <<< BREAKPOINT #
        processor.process()

        print("simple_text (after expansion) = {}".format(doc))
示例#4
0
    def test_exos_liste(self):
        # 1) parsing
        
        parser = Parser()
        # BREAKPOINT >>> # import pdb; pdb.set_trace()  # <<< BREAKPOINT #
        doc = parser.parse_from_file("../examples/exosliste.tango.tex")

        # 2) processing
        processor = DocumentProcessor(doc)
        core.register_core_processors(processor)
        py_ctx = codeactive.PythonContext(dict())
        codeactive.register_processors(processor, py_ctx)

        try:
            processor.process()
        except codeactive.CheckPythonFailure:
            print("CheckPython failed, aborpting ...")
            return

        #print("After process = \n" + str(doc))

        # 3) generating
        latex_config = LatexConfiguration()
        latex_config.document_class = "article"
        generator = LatexDocumentGenerator(doc, latex_config)
        generator.generate()
示例#5
0
    def test_basic_example(self):
        parser = Parser()

        # BREAKPOINT >>> # import pdb; pdb.set_trace()  # <<< BREAKPOINT #
        ret = parser.parse_from_file("../examples/basic.tango.tex")

        print(ret)
示例#6
0
文件: macros.py 项目: fredokun/tango
    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
示例#7
0
文件: core.py 项目: Mag-Stellon/tango
    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)
示例#8
0
    def test_macro_env_simple_text(self):
        parser = Parser()

        doc = parser.parse_from_string(r"""

% macro definition
\defEnv{hello}[0]{hello}{world}
\defEnv{hello2}{hello2}{world2}

% macro expansion
\begin{hello}
brave
\end{hello}

% macro expansion
\begin{hello2}
brave 
\end{hello2}

""")

        print("env_simple_text (before expansion) = {}".format(doc))

        processor = DocumentProcessor(doc)

        # BREAKPOINT >>> # import pdb; pdb.set_trace()  # <<< BREAKPOINT #
        processor.process()

        print("env_simple_text (after expansion) = {}".format(doc))
示例#9
0
    def test_mdsection_simple(self):
        parser = Parser()

        # BREAKPOINT >>> # import pdb; pdb.set_trace()  # <<< BREAKPOINT #
        ret = parser.parse_from_string(r"""

= section 1 =

this is in section \emph{section 1} 

== subsection 1.1 ==

this is in section \emph{subsection 1.1} 

== subsection 1.2 ==

this is in section \emph{subsection 1.2}

= section 2 = 

this is in section \emph{section 2} 

== subsection 2.1 ==

this is in section \emph{subsection 2.1} 

== subsection 2.2 ==

this is in section \emph{subsection 2.2}

""")

        print("doc 8 = {}".format(ret))
示例#10
0
    def test_section_simple(self):
        parser = Parser()

        # BREAKPOINT >>> # import ipdb; ipdb.set_trace()  # <<< BREAKPOINT #
        ret = parser.parse_from_string(r"""

\section{section 1}

this is in section \emph{section 1} 

\subsection{subsection 1.1} 

this is in section \emph{subsection 1.1} 

\subsection{subsection 1.2} 

this is in section \emph{subsection 1.2}

\section{section 2} 

this is in section \emph{section 2} 

\subsection{subsection 2.1} 

this is in section \emph{subsection 2.1} 

\subsection{subsection 2.2} 

this is in section \emph{subsection 2.2}

""")

        print("doc 7 = {}".format(ret))
示例#11
0
    def test_parentheses(self):
        parser = Parser()

        # BREAKPOINT >>> # import pdb; pdb.set_trace()  # <<< BREAKPOINT #
        ret = parser.parse_from_string(r"""
text (in parentheses)
""")

        print("doc 6 = {}".format(ret))
示例#12
0
    def test_inline_preformated(self):
        parser = Parser()

        ret = parser.parse_from_string(r"""

  The following is `inline preformated` and not the rest.

""")

        print("doc 11 = {}".format(ret))
示例#13
0
    def test_line_comment(self):
        parser = Parser()

        ret = parser.parse_from_string("""
% this is a comment
    % after some space

""")

        print("doc 1 = {}".format(ret))
示例#14
0
    def test_mdlist_simple(self):
        parser = Parser()

        ret = parser.parse_from_string(r"""

   - item 1.1
   - item 1.2
     * item 2.1
     * item 2.2

""")

        print("doc 10 = {}".format(ret))
示例#15
0
    def test_count_item(self):
        parser = Parser()
        # BREAKPOINT >>> # import pdb; pdb.set_trace()  # <<< BREAKPOINT #
        doc = parser.parse_from_file("../examples/basic.tango.tex")

        processor = DocumentProcessor(doc)

        count = TestCommandProcess.CountItem()
        processor.register_command_processor("item",count)

        processor.process()

        #print("count = {}".format(count.count))
        self.assertEqual(count.count, 5)
示例#16
0
    def test_eval_python(self):
        parser = Parser()
        doc = parser.parse_from_string("""
\evalPython{{{3+2}}}
""")

        print("Before processing = \n" + str(doc));

        process = DocumentProcessor(doc)
        py_ctx = codeactive.PythonContext(dict())
        codeactive.register_processors(process, py_ctx)
        process.process()

        print("After processing = \n" + str(doc));
示例#17
0
    def test_command_simple(self):
        parser = Parser()

        # BREAKPOINT >>> # import pdb; pdb.set_trace()  # <<< BREAKPOINT #
        ret = parser.parse_from_string(r"""
% this is a comment
\mycommand[key1=value1,key2=value2]{
contents of the command
% a comment in the command
}
    % after some space

""")

        print("doc 2 = {}".format(ret))
示例#18
0
文件: macros.py 项目: fredokun/tango
    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
示例#19
0
    def test_command_simple_preformated(self):
        parser = Parser()

        # BREAKPOINT >>> # import pdb; pdb.set_trace()  # <<< BREAKPOINT #
        ret = parser.parse_from_string(r"""
% this is a comment
\mycommand[key1=value1,key2=value2]{{{
contents of the command
  is preformated with {  curly brackets
  allowed }  
% a comment in the command
}}}
    % after some space

""")

        print("doc 2 bis = {}".format(ret))
示例#20
0
    def test_def_python(self):
        parser = Parser()
        doc = parser.parse_from_string("""
\defPython[fact]{{{
def fact(n):
    return 1 if n<=1 else n*fact(n-1)
}}}

\evalPython{{{3+fact(4)}}}
""")

        #print("Before processing = \n" + str(doc));

        process = DocumentProcessor(doc)
        py_ctx = codeactive.PythonContext(dict())
        codeactive.register_processors(process, py_ctx)
        process.process()
示例#21
0
    def test_macro_simple_replace(self):
        parser = Parser()

        doc = parser.parse_from_string(r"""

% macro definition
        \defCommand{\hello}[1]{brave #1 world}

% macro expansion
Hello \hello{new} 

""")

        # print("simple_replace (before expansion) = {}".format(doc))

        processor = DocumentProcessor(doc)

        # BREAKPOINT >>> # import pdb; pdb.set_trace()  # <<< BREAKPOINT #
        processor.process()
示例#22
0
    def test_env_simple(self):
        parser = Parser()

        # BREAKPOINT >>> # import pdb; pdb.set_trace()  # <<< BREAKPOINT #
        ret = parser.parse_from_string(r"""
% a comment before the environment
\begin{env}
% this is a comment
\mycommand[key1=value1,key2=value2]{
contents of the command
% a comment in the command
}
    % after some space
\end{env}

% a last comment outside the environment
""")

        print("doc 3 = {}".format(ret))
示例#23
0
    def test_env_itemize2(self):
        parser = Parser()

        # BREAKPOINT >>> # import pdb; pdb.set_trace()  # <<< BREAKPOINT #
        ret = parser.parse_from_string(r"""
% a comment before the environment
\begin{itemize}
\item 1
\item 2
\begin{itemize}
\item a (b)
\item 3.2
\end{itemize}
\item 4
\end{itemize}

% a last comment outside the environment
""")

        print("doc 5 = {}".format(ret))
示例#24
0
    def test_list_simple(self):
        parser = Parser()

        ret = parser.parse_from_string(r"""

\begin{itemize}

\item item 1.1
\item item 1.2

\begin{enumerate}

\item item 2.1
\item item 2.2

\end{enumerate}

\end{itemize}
""")

        print("doc 9 = {}".format(ret))
示例#25
0
    def test_macro_env_simple_replace(self):
        parser = Parser()

        doc = parser.parse_from_string(r"""

% macro definition
\defEnv{hello}[1]{hello #1}{#1 world}

% macro expansion
\begin{hello}{brave}
new
\end{hello}

""")

        #print("env_simple_replace (before expansion) = {}".format(doc))

        processor = DocumentProcessor(doc)

        # BREAKPOINT >>> # import pdb; pdb.set_trace()  # <<< BREAKPOINT #
        processor.process()
示例#26
0
    def test_basic_example(self):
        # 1) parsing
        
        parser = Parser()

        # BREAKPOINT >>> # import pdb; pdb.set_trace()  # <<< BREAKPOINT #
        doc = parser.parse_from_file("../examples/basic.tango.tex")

        # 2) processing
        processor = DocumentProcessor(doc)
        core.register_core_processors(processor)
        processor.process()

        # 3) generating
        latex_config = LatexConfiguration()
        latex_config.document_class = "article"

        generator = LatexDocumentGenerator(doc, latex_config)
        generator.generate()

        print("Output =\n" + str(generator.output))
示例#27
0
    def test_env_itemize(self):
        parser = Parser()

        # BREAKPOINT >>> # import pdb; pdb.set_trace()  # <<< BREAKPOINT #
        ret = parser.parse_from_string(r"""
% a comment before the environment
\begin{itemize}
% this is a comment
\item first item
\item second item with command \mycommand[key1=value1,key2=value2]{
contents of the command
% a comment in the command
}

\item an item
    % after some space
\end{itemize}

% a last comment outside the environment
""")

        print("doc 4 = {}".format(ret))
示例#28
0
    def test_macro_env_active_simple(self):
        parser = Parser()

        doc = parser.parse_from_string(r"""

% macro definition
\defEnv{myenv}{
\execPython{{{
enter_count += 1
}}}
}{
\execPython{{{
leave_count += 1
}}}
}

\execPython{{{
enter_count = 0
leave_count = 0
}}}

% macro expansion
\begin{myenv}
enter then leave
\end{myenv}

""")

        print("env_active_simple (before expansion) = {}".format(doc))

        processor = DocumentProcessor(doc)

        # BREAKPOINT >>> # import pdb; pdb.set_trace()  # <<< BREAKPOINT #
        processor.process()

        print("env_active_simple (after expansion) = {}".format(doc))
示例#29
0
文件: tango.py 项目: fredokun/tango
    if not enable_generate_phase:
        enable_write_phase = False

    if enable_write_phase:
        tangoPrintln("Write phase enabled")


    tangolib.globalvars.init_global_vars()

    import os
    tangoPrintln("Current work directory = '{}'".format(os.getcwd()))

    # 1) parsing

    parser = Parser()

    tangoPrintln("Parsing from file '{}' ...".format(args.input_filename))

    doc = parser.parse_from_file(args.input_filename)

    tangoPrintln("==> parsing done.")

    # 2) processing

    if enable_process_phase:

        tangoPrintln("Processing phase ...")

        processor = DocumentProcessor(doc)
        core.register_core_processors(processor)