Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def do_test(tex, uni, math_mode=None):
kwargs = {}
if math_mode is not None:
kwargs['math_mode'] = math_mode
self.assertEqual(
LatexNodes2Text(strict_latex_spaces=True, **kwargs).latex_to_text(tex),
uni,
msg="For TeX=r'{}'".format(tex)
)
def do_test(tex, uni):
self.assertEqual(LatexNodes2Text().latex_to_text(tex), uni,
msg="For TeX=r'{}'".format(tex))
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, '')
# this should no longer exist
self.assertEqual(db2.get_macro_spec('ddd').macroname, '')
self.assertEqual(db2.get_macro_spec('ddd').args_parser.argspec, '')
# this should give 'eccc' from cat2, not cat3
self.assertEqual(db2.get_environment_spec('eccc').environmentname, 'eccc')
self.assertEqual(db2.get_environment_spec('eccc').args_parser.argspec, '')
# this should no longer exist
self.assertEqual(db2.get_environment_spec('eddd').environmentname, '')
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, '*{')
self.assertEqual(db.get_macro_spec('ddd').macroname, 'ddd')
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
self.assertEqual(db2.get_macro_spec('aaa').macroname, '')
self.assertEqual(db2.get_macro_spec('aaa').args_parser.argspec, '')
self.assertEqual(db2.get_macro_spec('ddd').macroname, '')
self.assertEqual(db2.get_macro_spec('ddd').args_parser.argspec, '')
# this should give 'eaaa' from cat1
self.assertEqual(db2.get_environment_spec('eaaa').environmentname, 'eaaa')
self.assertEqual(db2.get_environment_spec('eaaa').args_parser.argspec, '{')
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')
self.assertEqual(db2.get_macro_spec('aaa').args_parser.argspec, '{')
# this should no longer exist
self.assertEqual(db2.get_macro_spec('ddd').macroname, '')
self.assertEqual(db2.get_macro_spec('ddd').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')
self.assertEqual(db2.get_macro_spec('aaa').args_parser.argspec, '{')
# this should no longer exist
self.assertEqual(db2.get_macro_spec('ddd').macroname, '')
self.assertEqual(db2.get_macro_spec('ddd').args_parser.argspec, '')
# no environments should exist any longer
self.assertEqual(db2.get_environment_spec('eaaa').environmentname, '')
def test_can_get_environment_spec(self):
db = LatexContextDb()
db.set_unknown_environment_spec(EnvironmentSpec(''))
db.add_context_category('cat1', [],
[ std_environment('eaaa', '{'), std_environment('ebbb', '[{[') ])
db.add_context_category('cat2', [],
[ std_environment('eaaa', '[{'), std_environment('eccc', None) ])
db.add_context_category('cat3', [],
[ std_environment('eccc', '*{'), std_environment('eddd', '{{[') ],
prepend=True)
self.assertEqual(list(db.categories()), ['cat3', 'cat1', 'cat2'])
self.assertEqual(db.get_environment_spec('eaaa').environmentname, 'eaaa')
self.assertEqual(db.get_environment_spec('eaaa').args_parser.argspec, '{')
self.assertEqual(db.get_environment_spec('ebbb').environmentname, 'ebbb')
self.assertEqual(db.get_environment_spec('ebbb').args_parser.argspec, '[{[')
self.assertEqual(db.get_environment_spec('eccc').environmentname, 'eccc')
self.assertEqual(db.get_environment_spec('eccc').args_parser.argspec, '*{')
def test_accents(self):
self.assertEqual(
LatexNodes2Text().nodelist_to_text(LatexWalker(r"Fran\c cais").get_latex_nodes()[0]),
'''Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais'''
)
self.assertEqual(
LatexNodes2Text().nodelist_to_text(LatexWalker(r"Fr\'en{\'{e}}tique").get_latex_nodes()[0]),
'''Fr\N{LATIN SMALL LETTER E WITH ACUTE}n\N{LATIN SMALL LETTER E WITH ACUTE}tique'''
)
self.assertEqual(
LatexNodes2Text(math_mode='with-delimiters')
.nodelist_to_text(LatexWalker(r"$1 \not= 2$").get_latex_nodes()[0]),
'''$1 {} 2$'''.format(unicodedata.normalize('NFC', "=\N{COMBINING LONG SOLIDUS OVERLAY}"))
)
correct_text = r'''ABCDEF fdksanfkld safnkd anfklsa
hi there! This is an equation:
x + y i = 0
where i is the imaginary unit.
MORENKFDNSN'''
testdir = os.path.realpath(os.path.abspath(os.path.dirname(__file__)))
l2t = LatexNodes2Text()
l2t.set_tex_input_directory(testdir)
output = l2t.nodelist_to_text(LatexWalker(latex).get_latex_nodes()[0])
self.assertEqualUpToWhitespace(
output,
correct_text
)
latex = r'''ABCDEF fdksanfkld safnkd anfklsa
\input{test_input_1}
MORENKFDNSN'''
self.assertEqualUpToWhitespace(
l2t.nodelist_to_text(LatexWalker(latex).get_latex_nodes()[0]),
correct_text
)