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_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, '*{')
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')
def test_filter_context_4(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=['environments'])
# no macros should exist any longer
def test_filter_context_2(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'])
# this should give 'aaa' from cat1
self.assertEqual(db2.get_macro_spec('aaa').macroname, 'aaa')
If you want to add your own definitions, you should use the
:py:meth:`pylatexenc.macrospec.LatexContextDb.add_context_category()`
method. If you would like to override some definitions, use that method
with the argument `prepend=True`. See docs for
:py:meth:`pylatexenc.macrospec.LatexContextDb.add_context_category()`.
If there are too many macro/environment definitions, or if there are some
irrelevant ones, you can always filter the returned database using
:py:meth:`pylatexenc.macrospec.LatexContextDb.filter_context()`.
.. versionadded:: 2.0
The :py:class:`pylatexenc.macrospec.LatexContextDb` class as well as this
method, were all introduced in `pylatexenc 2.0`.
"""
db = macrospec.LatexContextDb()
from ._defaultspecs import specs
for cat, catspecs in specs:
db.add_context_category(cat,
macros=catspecs['macros'],
environments=catspecs['environments'],
specials=catspecs['specials'])
return db
If you want to add your own definitions, you should use the
:py:meth:`pylatexenc.macrospec.LatexContextDb.add_context_category()`
method. If you would like to override some definitions, use that method
with the argument `prepend=True`. See docs for
:py:meth:`pylatexenc.macrospec.LatexContextDb.add_context_category()`.
If there are too many macro/environment definitions, or if there are some
irrelevant ones, you can always filter the returned database using
:py:meth:`pylatexenc.macrospec.LatexContextDb.filter_context()`.
.. versionadded:: 2.0
The :py:class:`pylatexenc.macrospec.LatexContextDb` class as well as this
method, were all introduced in `pylatexenc 2.0`.
"""
db = macrospec.LatexContextDb()
from ._defaultspecs import specs
for cat, catspecs in specs:
db.add_context_category(cat,
macros=catspecs['macros'],
environments=catspecs['environments'],
specials=catspecs['specials'])
return db
def __init__(self, latex_context=None, **flags):
super(LatexNodes2Text, self).__init__()
if latex_context is None:
if 'macro_dict' in flags or 'env_dict' in flags:
# LEGACY -- build a latex context using the given macro_dict
_util.pylatexenc_deprecated_2(
"The `macro_dict=...` and `env_dict=...` options in LatexNodes2Text() are "
"obsolete since pylatexenc 2. They will still work, but please consider "
"using instead the more versatile option `latex_context=...`."
)
macro_dict = flags.pop('macro_dict', [])
env_dict = flags.pop('env_dict', [])
latex_context = macrospec.LatexContextDb()
latex_context.add_context_category('custom',
macros=macro_dict.values(),
environments=env_dict.values(),
specials=[])
else:
# default -- use default
latex_context = get_default_latex_context_db()
self.latex_context = latex_context
self.tex_input_directory = None
self.strict_input = True
if 'keep_inline_math' in flags:
if 'math_mode' in flags: