Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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=('[', ']'),
nodelist=[
LatexCharsNode(parsing_state=parsing_state,
chars='a',
pos=p+23+39+7, len=1)
],
pos=p+23+39+6, len=3)
]),
pos=p+23+39+1,
len=9-1
),
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,
chars='2'
)
]
),
\begin{document}
% empty document
\end{document}
'''
lw = LatexWalker(latextext, tolerant_parsing=False)
parsing_state = lw.make_parsing_state()
p = 0
self.assertEqual(
lw.get_latex_nodes(pos=p, read_max_nodes=3, parsing_state=parsing_state),
([
LatexCharsNode(parsing_state=parsing_state,
chars='\n',
pos=0, len=1),
LatexMacroNode(parsing_state=parsing_state,
macroname='documentclass',
nodeargd=macrospec.ParsedMacroArgs(argspec='[{', argnlist=[
LatexGroupNode(parsing_state=parsing_state,
delimiters=('[', ']'),
nodelist=[
LatexCharsNode(parsing_state=parsing_state,
chars='stuff',
pos=1+15,len=20-15),
],
pos=1+14,len=21-14),
LatexGroupNode(parsing_state=parsing_state,
delimiters=('{', '}'),
nodelist=[
LatexCharsNode(parsing_state=parsing_state,
chars='docclass',
pos=1+22,len=30-22),
good_parsed_structure = lambda parsing_state, ps2, ps3: [
LatexCharsNode(parsing_state=parsing_state,
chars=' cx^2+z=-d',
pos=p+2,len=12-2),
LatexMacroNode(parsing_state=parsing_state,
macroname='quad',
pos=p+12,len=17-12),
LatexMacroNode(parsing_state=parsing_state,
macroname='text',
nodeargd=macrospec.ParsedMacroArgs(argspec='{', argnlist=[
LatexGroupNode(
parsing_state=ps2,
delimiters=('{', '}'),
nodelist=[
LatexCharsNode(parsing_state=ps2,
chars='if ',
pos=p+23,len=26-23),
LatexMathNode(parsing_state=ps2,
displaytype='inline', nodelist=[
LatexCharsNode(parsing_state=ps3,
chars='x<0',
pos=p+27,len=3)
],
delimiters=('$','$'),
\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)
parsing_state = lw.make_parsing_state()
p = latextext.find(r'Also: {')+len('Also:') # points on space after 'Also:'
good_parsed_structure = \
( LatexGroupNode(
parsing_state=parsing_state,
delimiters=('{','}'),
nodelist=[
LatexMacroNode(parsing_state=parsing_state,
macroname='itshape',
macro_post_space=' ',
pos=p+2,
len=len(r'\itshape ')),
LatexCharsNode(parsing_state=parsing_state,
chars='some italic text',
pos=(p+16-5),
len=32-16)
],
pos=p+1,
len=len(r'{\itshape some italic text}')
),
p+1, len(r'{\itshape some italic text}'), )
self.assertEqual(
lw.get_latex_braced_group(pos=p, brace_type='{', parsing_state=parsing_state),
good_parsed_structure
Indeed thanks to \cite[Lemma 3]{Author}, we know that...
Also: {\itshape some italic text}.
'''
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(
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=('$', '$'),
nodelist=[
LatexMacroNode(parsing_state=parsing_state_inner,
macroname='vec',
macro_post_space=' ',
nodeargd=macrospec.ParsedMacroArgs(argspec='{', argnlist=[
LatexCharsNode(parsing_state=parsing_state_inner,
chars='b',
pos=p+75-42, len=1)
]),
pos=p+70-42, len=76-70),
],
pos=p+69-42, len=77-69),
LatexCharsNode(parsing_state=parsing_state,
chars=' more stuff for Fran',
pos=p+77-42, len=97-77),
], p, 97-42))
nodeargd=macrospec.ParsedMacroArgs(argspec='{', argnlist=[
LatexGroupNode(parsing_state=parsing_state,
delimiters=('{', '}'),
nodelist=[
LatexCharsNode(parsing_state=parsing_state,
chars='x',
pos=1+40,len=1)
],
pos=1+39,len=3)
]),
macro_post_space='',
pos=1+35,len=42-35),
LatexCharsNode(parsing_state=parsing_state,
chars=r' + ',
pos=1+42,len=45-42),
LatexMacroNode(parsing_state=parsing_state,
macroname=r'hat',
nodeargd=macrospec.ParsedMacroArgs(argspec='{', argnlist=[
LatexCharsNode(parsing_state=parsing_state,
chars='p',
pos=1+50,len=1)]),
macro_post_space=' ',
pos=1+45,len=51-45),
]
(LatexMacroNode(parsing_state=parsing_state,
macroname='`',
nodeargd=None, pos=p, len=2),p,2,))
p = latextext.find(r'{')
self.assertEqual(lw.get_latex_expression(pos=p, parsing_state=parsing_state),
(LatexGroupNode(parsing_state=parsing_state,
nodelist=[
LatexCharsNode(parsing_state=parsing_state,
chars='bold text',
pos=p+1,len=9)
],
pos=p, len=11),
p,11,))
p = latextext.find(r'%')-2 # check: correctly skips comments also after space
self.assertEqual(lw.get_latex_expression(pos=p, parsing_state=parsing_state),
(LatexMacroNode(parsing_state=parsing_state,
macroname='item',
nodeargd=None,
pos=p+2+len('% here goes a comment\n'),
len=5),
p+2+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, parsing_state=parsing_state,
strict_braces=True),
(LatexCharsNode(parsing_state=parsing_state,
chars='', pos=p, len=0),p,0,))
lw2 = LatexWalker(latextext, tolerant_parsing=False)
parsing_state2 = lw2.make_parsing_state()
self.assertEqual(lw2.get_latex_expression(pos=p, parsing_state=parsing_state2,
strict_braces=False),
(LatexCharsNode(parsing_state=parsing_state2,
def _is_bare_macro_node(self, node):
return (node is not None and
node.isNodeType(latexwalker.LatexMacroNode) and
node.nodeoptarg is None and
len(node.nodeargs) == 0)