def test_combined_0(self): lw = latexwalker.LatexWalker(r'\cmd{ab}c*') s = MacroStandardArgsParser('{*[{*') parsing_state = lw.make_parsing_state() (argd, p, l) = s.parse_args(lw, len(r'\cmd'), parsing_state=parsing_state) self.assertPMAEqual( argd, ParsedMacroArgs(argspec='{*[{*', argnlist=[ LatexGroupNode( parsing_state=parsing_state, delimiters=('{', '}'), nodelist=[ LatexCharsNode( parsing_state=parsing_state, chars='ab', pos=5, len=2) ], pos=4, len=4), None, None, LatexCharsNode(parsing_state=parsing_state, chars='c', pos=8, len=1), LatexCharsNode(parsing_state=parsing_state, chars='*', pos=9, len=1) ]))
def test_get_latex_expression(self): latextext = r'''Text and \`accent and \textbf{bold text} and $\vec b$ more stuff for Fran\c cois \begin{enumerate}[(i)] \item Hi there! % here goes a comment \item[a] Hello! @@@ \end{enumerate} ''' lw = LatexWalker(latextext, tolerant_parsing=True) self.assertEqual(lw.get_latex_expression(pos=0), ( LatexCharsNode('T'), 0, 1, )) p = latextext.find(r'\`') self.assertEqual(lw.get_latex_expression(pos=p), ( LatexMacroNode('`', None, []), p, 2, )) p = latextext.find(r'{') self.assertEqual(lw.get_latex_expression(pos=p), ( LatexGroupNode([LatexCharsNode('bold text')]), p, 11, )) p = latextext.find(r'%') # check: correctly skips comments self.assertEqual(lw.get_latex_expression(pos=p), ( LatexMacroNode('item', None, []), p + len('% here goes a comment\n'), 5, )) p = latextext.find(r'%') # check: correctly skips comments self.assertEqual(lw.get_latex_expression(pos=p), ( LatexMacroNode('item', None, []), p + len('% here goes a comment\n'), 5, )) # check correct behavior if directly on brace close p = latextext.find(r'}') self.assertEqual(lw.get_latex_expression(pos=p, strict_braces=True), ( LatexCharsNode(''), p, 0, )) lw2 = LatexWalker(latextext, tolerant_parsing=False) self.assertEqual(lw2.get_latex_expression(pos=p, strict_braces=False), ( LatexCharsNode(''), p, 0, )) with self.assertRaises(LatexWalkerParseError): dummy = lw2.get_latex_expression(pos=p, strict_braces=True)
def test_get_latex_environment(self): latextext = r'''Text and \`accent and \textbf{bold text} and $\vec b$ more stuff for Fran\c cois \begin{enumerate}[(i)] \item Hi there! % here goes a comment \item[a] Hello! @@@ \end{enumerate} Indeed thanks to \cite[Lemma 3]{Author}, we know that... Also: {\itshape some italic text}. ''' lw = LatexWalker(latextext, tolerant_parsing=False) p = latextext.find(r'\begin{enumerate}') self.assertEqual( lw.get_latex_environment(pos=p, environmentname='enumerate'), ( LatexEnvironmentNode('enumerate', [ LatexCharsNode('\n'), LatexMacroNode('item', None, [], macro_post_space=' '), LatexCharsNode('Hi there! '), LatexCommentNode(' here goes a comment'), LatexMacroNode('item', LatexGroupNode( [LatexCharsNode('a')]), []), LatexCharsNode(' Hello! @@@\n ') ], [LatexGroupNode([LatexCharsNode('(i)')])], []), p, latextext.find(r'\end{enumerate}') + len(r'\end{enumerate}') - p, )) self.assertEqual(lw.get_latex_environment(pos=p), ( LatexEnvironmentNode('enumerate', [ LatexCharsNode('\n'), LatexMacroNode('item', None, [], macro_post_space=' '), LatexCharsNode('Hi there! '), LatexCommentNode(' here goes a comment'), LatexMacroNode('item', LatexGroupNode([LatexCharsNode('a')]), []), LatexCharsNode(' Hello! @@@\n ') ], [LatexGroupNode([LatexCharsNode('(i)')])], []), p, latextext.find(r'\end{enumerate}') + len(r'\end{enumerate}') - p, )) with self.assertRaises(LatexWalkerParseError): dummy = lw.get_latex_environment(pos=p, environmentname='XYZNFKLD-WRONG')
def test_marg_1(self): lw = latexwalker.LatexWalker(r'\cmd ab') s = MacroStandardArgsParser('{') parsing_state = lw.make_parsing_state() (argd, p, l) = s.parse_args(lw, len(r'\cmd'), parsing_state=parsing_state) self.assertPMAEqual( argd, ParsedMacroArgs(argspec='{', argnlist=[ LatexCharsNode(parsing_state=parsing_state, chars='a', pos=len(r'\cmd')+1,len=1) ]) )
def test_get_latex_braced_group(self): latextext = r'''Text and \`accent and \textbf{bold text} and $\vec b$ more stuff for Fran\c cois \begin{enumerate}[(i)] \item Hi there! % here goes a comment \item[a] Hello! @@@ \end{enumerate} Indeed thanks to \cite[Lemma 3]{Author}, we know that... Also: {\itshape some italic text}. ''' lw = LatexWalker(latextext, tolerant_parsing=False) p = latextext.find(r'Also: {') + len( 'Also:') # points on space after 'Also:' self.assertEqual(lw.get_latex_braced_group(pos=p, brace_type='{'), ( LatexGroupNode([ LatexMacroNode('itshape', None, [], macro_post_space=' '), LatexCharsNode('some italic text') ]), p + 1, len('{\itshape some italic text}'), )) self.assertEqual(lw.get_latex_braced_group( pos=p + 1, brace_type='{'), ( LatexGroupNode([ LatexMacroNode('itshape', None, [], macro_post_space=' '), LatexCharsNode('some italic text') ]), p + 1, len('{\itshape some italic text}'), )) p = latextext.find(r'[(i)]') self.assertEqual(lw.get_latex_braced_group(pos=p, brace_type='['), ( LatexGroupNode([LatexCharsNode('(i)')]), p, 5, ))
def test_get_latex_maybe_optional_arg(self): latextext = r'''Text and \`accent and \textbf{bold text} and $\vec b$ more stuff for Fran\c cois \begin{enumerate}[(i)] \item Hi there! % here goes a comment \item[a] Hello! @@@ \end{enumerate} Indeed thanks to \cite[Lemma 3]{Author}, we know that... ''' lw = LatexWalker(latextext, tolerant_parsing=False) p = latextext.find(r'\textbf') + len(r'\textbf') self.assertEqual(lw.get_latex_maybe_optional_arg(pos=p), None) p = latextext.find(r'\cite') + len(r'\cite') self.assertEqual(lw.get_latex_maybe_optional_arg(pos=p), ( LatexGroupNode([LatexCharsNode('Lemma 3')]), p, 9, ))
def test_oarg_0(self): lw = latexwalker.LatexWalker(r'\cmd[ab] xyz') s = MacroStandardArgsParser('[') parsing_state = lw.make_parsing_state() (argd, p, l) = s.parse_args(lw, len(r'\cmd'), parsing_state=parsing_state) self.assertPMAEqual( argd, ParsedMacroArgs(argspec='[', argnlist=[ LatexGroupNode( parsing_state=parsing_state, delimiters=('[', ']'), nodelist=[ LatexCharsNode(parsing_state=parsing_state, chars='ab', pos=5,len=2) ], pos=4,len=4) ]) )
def test_marg_0(self): lw = latexwalker.LatexWalker(r'{ab}') s = MacroStandardArgsParser('{') parsing_state = lw.make_parsing_state() (argd, p, l) = s.parse_args(lw, 0, parsing_state=parsing_state) self.assertPMAEqual( argd, ParsedMacroArgs( argspec='{', argnlist=[ LatexGroupNode( parsing_state=parsing_state, delimiters=('{','}'), nodelist=[ LatexCharsNode(parsing_state=parsing_state, chars='ab', pos=1,len=2) ], pos=0,len=4) ]) )
def test_get_latex_nodes(self): latextext = r'''Text and \`accent and \textbf{bold text} and $\vec b$ more stuff for Fran\c cois \begin{enumerate}[(i)] \item Hi there! % here goes a comment \item[a] Hello! @@@ \end{enumerate} Indeed thanks to \cite[Lemma 3]{Author}, we know that... Also: {\itshape some italic text}. ''' lw = LatexWalker(latextext, tolerant_parsing=False) #lw.get_latex_nodes(pos=0,stop_upon_closing_brace=None,stop_upon_end_environment=None, # stop_upon_closing_mathmode=None) p = latextext.find('Also: {') self.assertEqual(lw.get_latex_nodes(pos=p), ([ LatexCharsNode('Also: '), LatexGroupNode([ LatexMacroNode('itshape', None, [], macro_post_space=' '), LatexCharsNode('some italic text') ]), LatexCharsNode('.') ], p, len(latextext) - p - 1)) # trailing '\n' is not included p = latextext.find('Also: {') + len( 'Also: {') # points inside right after open brace self.assertEqual( lw.get_latex_nodes(pos=p, stop_upon_closing_brace='}'), ([ LatexMacroNode('itshape', None, [], macro_post_space=' '), LatexCharsNode('some italic text') ], p, len('\itshape some italic text}'))) # test our own macro lists etc. pindeed = latextext.find('Indeed thanks to') lineindeed = latextext[pindeed:latextext.find('\n', pindeed)] lw2 = LatexWalker(lineindeed, tolerant_parsing=False, macro_dict={'cite': MacrosDef('cite', False, 4)}) self.assertEqual(lw2.get_latex_nodes(pos=0), ([ LatexCharsNode('Indeed thanks to '), LatexMacroNode('cite', None, [ LatexCharsNode('['), LatexCharsNode('L'), LatexCharsNode('e'), LatexCharsNode('m'), ]), LatexCharsNode('ma 3]'), LatexGroupNode([LatexCharsNode('Author')]), LatexCharsNode(', we know that...'), ], 0, len(lineindeed)))