Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if sys.version_info.major > 2:
def unicode(string): return string
basestring = str
from pylatexenc.latexwalker import (
LatexWalker, LatexToken, LatexCharsNode, LatexGroupNode, LatexCommentNode,
LatexMacroNode, LatexSpecialsNode, LatexEnvironmentNode, LatexMathNode,
LatexWalkerParseError, get_default_latex_context_db
)
from pylatexenc import macrospec
def _tmp1133(a, b):
return b is not None and a.argnlist == b.argnlist
macrospec.ParsedMacroArgs.__eq__ = _tmp1133
class TestLatexWalker(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(TestLatexWalker, self).__init__(*args, **kwargs)
self.maxDiff = None
def test_get_token(self):
latextext = r'''Text \`accent and \textbf{bold text} and $\vec b$ vector \& also Fran\c cois
\begin{enumerate}[(i)]
\item Hi there! % here goes a comment
\item[a] Hello! @@@
\end{enumerate}
'''
parsing_state=parsing_state,
pos=13, len=5, delimiters=('{', '}'),
nodelist=[
LatexCharsNode(
parsing_state=parsing_state,
pos=14, len=3,
chars='AAA'
)
]
)
]
)),
LatexCharsNode(parsing_state=parsing_state_defa, pos=18, len=1, chars='\n'),
LatexMacroNode(parsing_state=parsing_state_defa, pos=19, len=26,
macroname='newcommand',
nodeargd=macrospec.ParsedMacroArgs(
argspec='*{[[{',
argnlist=[
None,
LatexMacroNode(
parsing_state=parsing_state_defa,
pos=19+12, len=2,
macroname='b',
nodeargd=None,
),
LatexGroupNode(
parsing_state=parsing_state_defa,
pos=19+15, len=3, delimiters=('[', ']'),
nodelist=[
LatexCharsNode(
parsing_state=parsing_state_defa,
pos=19+16, len=1,
def test_star_0(self):
lw = latexwalker.LatexWalker(r'\cmd xyz')
s = MacroStandardArgsParser('*')
(argd, p, l) = s.parse_args(lw, len(r'\cmd'))
self.assertPMAEqual(
argd,
ParsedMacroArgs(argspec='*', argnlist=[ None ])
)
chars='BBB #1'
)
]
)
]
)),
LatexCharsNode(parsing_state=parsing_state_defab, pos=19+26, len=1+1+16+1+12,
chars=r"""
Blah blah blah.
Use macros: """,
),
LatexMacroNode(parsing_state=parsing_state_defab, pos=19+27+1+16+1+12, len=2,
macroname='a',
nodeargd=macrospec.ParsedMacroArgs()),
LatexGroupNode(parsing_state=parsing_state_defab, pos=19+27+1+16+1+12+2, len=2,
delimiters=('{', '}'), nodelist=[]),
LatexCharsNode(parsing_state=parsing_state_defab, pos=19+27+1+16+1+16, len=5,
chars=r""" and """),
LatexMacroNode(parsing_state=parsing_state_defab, pos=19+27+1+16+1+21, len=33-21,
macroname='b',
nodeargd=macrospec.ParsedMacroArgs(argspec='{{', argnlist=[
LatexGroupNode(
parsing_state=parsing_state_defab,
pos=19+27+1+16+1+23, len=5, delimiters=('{', '}'),
nodelist=[
LatexCharsNode(
parsing_state=parsing_state_defab,
pos=19+27+1+16+1+24, len=3,
chars='xxx'
)
pos=26, len=2),
LatexCommentNode(parsing_state=parsing_state,
comment=' line comment on its own',
comment_post_space='\n',
pos=27+1, len=25+1),
LatexCommentNode(parsing_state=parsing_state,
comment=' and a second line',
comment_post_space='',
pos=27+1+26, len=19),
LatexCharsNode(parsing_state=parsing_state,
chars='\n\nAlso a ',
pos=27+1+26+19, len=2+7),
LatexMacroNode(parsing_state=parsing_state,
macroname=r'itshape',
macro_post_space='',
nodeargd=macrospec.ParsedMacroArgs(argspec='',argnlist=[]),
pos=27+1+26+20+1+7, len=15-7),
LatexCommentNode(parsing_state=parsing_state,
comment=' comment after a macro',
comment_post_space='\n',
pos=27+1+26+20+1+15, len=39-15),
LatexCommentNode(parsing_state=parsing_state,
comment=' and also a second line',
comment_post_space='\n',
pos=27+1+26+20+1+39, len=25),
LatexCharsNode(parsing_state=parsing_state,
chars='some italic text.',
pos=27+1+26+20+1+39+25, len=17),
],
0,len(latextext))
)
'''
lw = LatexWalker(latextext, tolerant_parsing=False)
parsing_state = lw.make_parsing_state()
p = latextext.find(r'\begin{enumerate}')
good_parsed_structure = \
(LatexEnvironmentNode(
parsing_state=parsing_state,
environmentname='enumerate',
nodelist=[
LatexCharsNode(parsing_state=parsing_state,
chars='\n',
pos=p+22, len=1),
LatexMacroNode(parsing_state=parsing_state,
macroname='item',
nodeargd=macrospec.ParsedMacroArgs(argspec='[', argnlist=[None]),
macro_post_space=' ',
pos=p+23, len=6),
LatexCharsNode(parsing_state=parsing_state,
chars='Hi there! ',
pos=p+23+6, len=17-6),
LatexCommentNode(parsing_state=parsing_state,
comment=' here goes a comment',
comment_post_space='\n ',
pos=p+23+17, len=38-17+2),
LatexMacroNode(
parsing_state=parsing_state,
macroname='item',
nodeargd=macrospec.ParsedMacroArgs(argspec='[', argnlist=[
LatexGroupNode(
parsing_state=parsing_state,
delimiters=('[', ']'),
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)
])
pos=p, len=33-24),
], p, 33-24))
p = latextext.find(r'ent and ') + 4 # points on second "and" on first line
nodes, pos, len_ = lw.get_latex_nodes(pos=p, read_max_nodes=5, parsing_state=parsing_state)
parsing_state_inner = nodes[3].nodelist[0].parsing_state # inner state -- math mode -- get this
self.assertTrue(parsing_state_inner.in_math_mode)
self.assertEqual(
(nodes, pos, len_),
([
LatexCharsNode(parsing_state=parsing_state,
chars='and ',
pos=p, len=4),
LatexMacroNode(parsing_state=parsing_state,
macroname='textbf',
nodeargd=macrospec.ParsedMacroArgs(argspec='{', argnlist=[
LatexGroupNode(parsing_state=parsing_state,
delimiters=('{', '}'),
nodelist=[
LatexCharsNode(parsing_state=parsing_state,
chars='bold text',
pos=p+54-42, len=9)
],
pos=p+53-42, len=11)
]),
pos=p+46-42, len=64-46),
LatexCharsNode(parsing_state=parsing_state,
chars=' and ',
pos=p+64-42, len=69-64),
LatexMathNode(parsing_state=parsing_state,
displaytype='inline',
delimiters=('$', '$'),
def test_star_2(self):
lw = latexwalker.LatexWalker(r'\cmd * 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=[
LatexCharsNode(parsing_state=parsing_state,
chars='*',
pos=5,len=1)
])
def __init__(self, macroname, **kwargs):
nodeargd=kwargs.pop('nodeargd', macrospec.ParsedMacroArgs())
macro_post_space=kwargs.pop('macro_post_space', '')
# legacy:
nodeoptarg=kwargs.pop('nodeoptarg', None)
nodeargs=kwargs.pop('nodeargs', [])
super(LatexMacroNode, self).__init__(
_fields = ('macroname','nodeargd','macro_post_space'),
_redundant_fields = ('nodeoptarg','nodeargs'),
**kwargs)
self.macroname = macroname
self.nodeargd = nodeargd
self.macro_post_space = macro_post_space
# legacy:
self.nodeoptarg = nodeoptarg
self.nodeargs = nodeargs