Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_filter_context_0(self):
db = LatexContextDb()
db.set_unknown_macro_spec(MacroSpec(''))
db.set_unknown_environment_spec(EnvironmentSpec(''))
db.add_context_category('cat1',
[ std_macro('aaa', '{'), std_macro('bbb', '[{[') ],
[ std_environment('eaaa', '{'), std_environment('ebbb', '[{[') ])
db.add_context_category('cat2',
[ std_macro('aaa', '[{'), std_macro('ccc', None) ],
[ std_environment('eaaa', '[{'), std_environment('eccc', None) ])
db.add_context_category('cat3',
[ std_macro('ccc', '*{'), std_macro('ddd', '{{[') ],
[ std_environment('eccc', '*{'), std_environment('eddd', '{{[') ],
prepend=True)
db2 = db.filter_context(keep_categories=['cat1', 'cat2'])
# this should give 'ccc' from cat2, not cat3
self.assertEqual(db2.get_macro_spec('ccc').macroname, 'ccc')
self.assertEqual(db2.get_macro_spec('ccc').args_parser.argspec, '')
(argd, npos, nlen) = super(MySimpleNewcommandArgsParser, self).parse_args(
w=w, pos=pos, parsing_state=parsing_state, **kwargs
)
if argd.argnlist[1].isNodeType(LatexGroupNode):
argd.argnlist[1] = argd.argnlist[1].nodelist[0] # {\command} -> \command
assert argd.argnlist[1].isNodeType(LatexMacroNode)
argd.argnlist[1].nodeargd = None # hmmm, we should really have a
# custom parser here to read a
# single token
newcmdname = argd.argnlist[1].macroname
numargs = int(argd.argnlist[2].nodelist[0].chars) if argd.argnlist[2] else 0
new_latex_context = parsing_state.latex_context.filter_context()
new_latex_context.add_context_category(
'newcommand-{}'.format(newcmdname),
macros=[
macrospec.MacroSpec(newcmdname, '{'*numargs)
],
prepend=True
)
new_parsing_state = parsing_state.sub_context(latex_context=new_latex_context)
return (argd, npos, nlen, dict(new_parsing_state=new_parsing_state))
def test_filter_context_1(self):
db = LatexContextDb()
db.set_unknown_macro_spec(MacroSpec(''))
db.set_unknown_environment_spec(EnvironmentSpec(''))
db.add_context_category('cat1',
[ std_macro('aaa', '{'), std_macro('bbb', '[{[') ],
[ std_environment('eaaa', '{'), std_environment('ebbb', '[{[') ])
db.add_context_category('cat2',
[ std_macro('aaa', '[{'), std_macro('ccc', None) ],
[ std_environment('eaaa', '[{'), std_environment('eccc', None) ])
db.add_context_category('cat3',
[ std_macro('ccc', '*{'), std_macro('ddd', '{{[') ],
[ std_environment('eccc', '*{'), std_environment('eddd', '{{[') ],
prepend=True)
db2 = db.filter_context(exclude_categories=['cat3'])
# this should give 'ccc' from cat2, not cat3
self.assertEqual(db2.get_macro_spec('ccc').macroname, 'ccc')
self.assertEqual(db2.get_macro_spec('ccc').args_parser.argspec, '')
def test_filter_context_3(self):
db = LatexContextDb()
db.set_unknown_macro_spec(MacroSpec(''))
db.set_unknown_environment_spec(EnvironmentSpec(''))
db.add_context_category('cat1',
[ std_macro('aaa', '{'), std_macro('bbb', '[{[') ],
[ std_environment('eaaa', '{'), std_environment('ebbb', '[{[') ])
db.add_context_category('cat2',
[ std_macro('aaa', '[{'), std_macro('ccc', None) ],
[ std_environment('eaaa', '[{'), std_environment('eccc', None) ])
db.add_context_category('cat3',
[ std_macro('ccc', '*{'), std_macro('ddd', '{{[') ],
[ std_environment('eccc', '*{'), std_environment('eddd', '{{[') ],
prepend=True)
db2 = db.filter_context(keep_categories=['cat1', 'cat3'], exclude_categories=['cat3'],
keep_which=['macros'])
# this should give 'aaa' from cat1
self.assertEqual(db2.get_macro_spec('aaa').macroname, 'aaa')
def test_can_get_macro_spec(self):
db = LatexContextDb()
db.set_unknown_macro_spec(MacroSpec(''))
db.add_context_category('cat1', [ std_macro('aaa', '{'), std_macro('bbb', '[{[') ], [])
db.add_context_category('cat2', [ std_macro('aaa', '[{'), std_macro('ccc', None) ], [])
db.add_context_category('cat3', [ std_macro('ccc', '*{'), std_macro('ddd', '{{[') ], [],
prepend=True)
self.assertEqual(list(db.categories()), ['cat3', 'cat1', 'cat2'])
self.assertEqual(db.get_macro_spec('aaa').macroname, 'aaa')
self.assertEqual(db.get_macro_spec('aaa').args_parser.argspec, '{')
self.assertEqual(db.get_macro_spec('bbb').macroname, 'bbb')
self.assertEqual(db.get_macro_spec('bbb').args_parser.argspec, '[{[')
self.assertEqual(db.get_macro_spec('ccc').macroname, 'ccc')
self.assertEqual(db.get_macro_spec('ccc').args_parser.argspec, '*{')
# cf. https://tex.stackexchange.com/a/439652/32188 "fake ligatures":
std_specials('``'),
std_specials("''"),
std_specials("--"),
std_specials("---"),
std_specials("!`"),
std_specials("?`"),
]}),
#
# CATEGORY: verbatim
#
('verbatim', {
'macros': [
MacroSpec('verb',
args_parser=VerbatimArgsParser(verbatim_arg_type='verb-macro')),
],
'environments': [
EnvironmentSpec('verbatim',
args_parser=VerbatimArgsParser(verbatim_arg_type='verbatim-environment')),
],
'specials': [
# optionally users could include the specials "|" like in latex-doc
# for verbatim |\like \this|...
]}),
#
# CATEGORY: theorems
#
('theorems', {
'macros': [],
std_macro("`", False, 1),
std_macro('"', False, 1),
std_macro("c", False, 1),
std_macro("^", False, 1),
std_macro("~", False, 1),
std_macro("H", False, 1),
std_macro("k", False, 1),
std_macro("=", False, 1),
std_macro("b", False, 1),
std_macro(".", False, 1),
std_macro("d", False, 1),
std_macro("r", False, 1),
std_macro("u", False, 1),
std_macro("v", False, 1),
MacroSpec('ensuremath', args_parser=MacroStandardArgsParser('{', args_math_mode=[True])),
std_macro("not", False, 1),
std_macro("vec", False, 1),
std_macro("dot", False, 1),
std_macro("hat", False, 1),
std_macro("check", False, 1),
std_macro("breve", False, 1),
std_macro("acute", False, 1),
std_macro("grave", False, 1),
std_macro("tilde", False, 1),
std_macro("bar", False, 1),
std_macro("ddot", False, 1),
std_macro('frac', False, 2),
std_macro('nicefrac', False, 2),
std_macro('section', '*[{'),
std_macro('subsection', '*[{'),
std_macro('subsubsection', '*[{'),
std_macro('pagagraph', '*[{'),
std_macro('subparagraph', '*[{'),
std_macro('bibliography', '{'),
std_macro('emph', False, 1),
MacroSpec('textrm', args_parser=MacroStandardArgsParser('{', args_math_mode=[False])),
MacroSpec('textit', args_parser=MacroStandardArgsParser('{', args_math_mode=[False])),
MacroSpec('textbf', args_parser=MacroStandardArgsParser('{', args_math_mode=[False])),
MacroSpec('textsc', args_parser=MacroStandardArgsParser('{', args_math_mode=[False])),
MacroSpec('textsl', args_parser=MacroStandardArgsParser('{', args_math_mode=[False])),
MacroSpec('text', args_parser=MacroStandardArgsParser('{', args_math_mode=[False])),
std_macro('mathrm', False, 1), # only allowed in math mode anyway
std_macro('mathbb', False, 1), # only allowed in math mode anyway
std_macro('mathbf', False, 1),
std_macro('mathsf', False, 1),
std_macro('mathscr', False, 1),
std_macro('mathfrak', False, 1),
std_macro('label', False, 1),
std_macro('ref', False, 1),
std_macro('autoref', False, 1),
std_macro('cref', False, 1),
std_macro('Cref', False, 1),
std_macro('eqref', False, 1),
std_macro('url', False, 1),
std_macro('hypersetup', False, 1),
std_macro('chapter', '*[{'),
std_macro('section', '*[{'),
std_macro('subsection', '*[{'),
std_macro('subsubsection', '*[{'),
std_macro('pagagraph', '*[{'),
std_macro('subparagraph', '*[{'),
std_macro('bibliography', '{'),
std_macro('emph', False, 1),
MacroSpec('textrm', args_parser=MacroStandardArgsParser('{', args_math_mode=[False])),
MacroSpec('textit', args_parser=MacroStandardArgsParser('{', args_math_mode=[False])),
MacroSpec('textbf', args_parser=MacroStandardArgsParser('{', args_math_mode=[False])),
MacroSpec('textsc', args_parser=MacroStandardArgsParser('{', args_math_mode=[False])),
MacroSpec('textsl', args_parser=MacroStandardArgsParser('{', args_math_mode=[False])),
MacroSpec('text', args_parser=MacroStandardArgsParser('{', args_math_mode=[False])),
std_macro('mathrm', False, 1), # only allowed in math mode anyway
std_macro('mathbb', False, 1), # only allowed in math mode anyway
std_macro('mathbf', False, 1),
std_macro('mathsf', False, 1),
std_macro('mathscr', False, 1),
std_macro('mathfrak', False, 1),
std_macro('label', False, 1),
std_macro('ref', False, 1),
std_macro('autoref', False, 1),
std_macro('cref', False, 1),
std_macro('Cref', False, 1),
std_macro('eqref', False, 1),