Exemplo n.º 1
0
def test_comment():
    parms = parameters.Parameters()
    p = parser.Parser(parms)
    toks = p.parse(latex_comment)
    plain_comment, pos = utils.get_txt_pos(toks)

    assert plain_comment_should_be == plain_comment
Exemplo n.º 2
0
def test_10():
    parms = parameters.Parameters()
    parms.math_displayed_simple = True
    p = parser.Parser(parms)
    toks = p.parse(latex_10)
    plain, pos = utils.get_txt_pos(toks)
    assert plain_10 == plain
Exemplo n.º 3
0
def test_1():
    # keep verbatim
    parms = parameters.Parameters()
    p = parser.Parser(parms)
    toks = p.parse(latex_1)
    plain, pos = utils.get_txt_pos(toks)
    assert plain == plain_1
Exemplo n.º 4
0
def test_6():
    parms = parameters.Parameters()
    parms.no_specials()
    p = parser.Parser(parms)
    toks = p.parse(latex_6)
    plain, pos = utils.get_txt_pos(toks)
    assert plain_6 == plain
Exemplo n.º 5
0
def test_3():
    parms = parameters.Parameters()
    parms.item_default_label = ['*']
    p = parser.Parser(parms)
    toks = p.parse(latex_3)
    plain, pos = utils.get_txt_pos(toks)
    assert plain_3 == plain
Exemplo n.º 6
0
def test_macro_in_arg():
    parms = parameters.Parameters()
    parms.environment_defs.append(
        defs.Environ(parms, 'XYZ', args='A', repl='#1', add_pars=False))
    p = parser.Parser(parms)
    toks = p.parse(latex_macro_in_arg)
    plain, pos = utils.get_txt_pos(toks)
    assert plain_macro_in_arg == plain
Exemplo n.º 7
0
def test_4(capsys):
    capsys.readouterr()
    p = parser.Parser(parameters.Parameters())
    toks = p.parse(latex_4, source='t.tex')
    plain, pos = utils.get_txt_pos(toks)
    cap = capsys.readouterr()
    assert plain_4 == plain
    assert cap.err == stderr_4
Exemplo n.º 8
0
def test_2():
    parms = parameters.Parameters()
    parms.item_default_label = ['*']
    parms.item_punctuation = []
    p = parser.Parser(parms)
    toks = p.parse(latex_2)
    plain, pos = utils.get_txt_pos(toks)
    assert plain_2 == plain
Exemplo n.º 9
0
def test_3():
    # replace verbatim
    parms = parameters.Parameters()
    parms.environment_defs.append(
        defs.Environ(parms, 'verbatim', repl='[verbatim]', remove=True))
    p = parser.Parser(parms)
    toks = p.parse(latex_1)
    plain, pos = utils.get_txt_pos(toks)
    assert plain == plain_3
Exemplo n.º 10
0
def test_5():
    def read(file):
        return True, r'\usepackage{amsmath}'

    parms = parameters.Parameters()
    p = parser.Parser(parms, read_macros=read)
    toks = p.parse(latex_5)
    plain, pos = utils.get_txt_pos(toks)
    assert plain_5 == plain
Exemplo n.º 11
0
def test_3():
    def read(file):
        return True, sed_3

    parms = parameters.Parameters()
    p = parser.Parser(parms, read_macros=read)
    toks = p.parse(latex_3)
    plain, pos = utils.get_txt_pos(toks)
    assert plain_3 == plain
Exemplo n.º 12
0
def test_2():
    def read(file):
        return True, r'\usepackage{xcolor}\newcommand{\xxx}[1]{ #1 #1}'

    parms = parameters.Parameters()
    p = parser.Parser(parms, read_macros=read)
    toks = p.parse(latex_2)
    plain, pos = utils.get_txt_pos(toks)
    assert plain_2 == plain
Exemplo n.º 13
0
def test_6(capsys):
    def read(file):
        return True, sed_6

    capsys.readouterr()
    parms = parameters.Parameters()
    p = parser.Parser(parms, read_macros=read)
    toks = p.parse(latex_6, source='t.tex')
    plain, pos = utils.get_txt_pos(toks)
    captured = capsys.readouterr()
    assert plain_6 == plain
    assert stderr_6 == captured.err
Exemplo n.º 14
0
def get_plain(latex):
    def read(file):
        try:
            with open(file) as f:
                return True, f.read()
        except:
            return False, ''
    parms = parameters.Parameters()
    p = parser.Parser(parms, read_macros=read)
    plain, nums = utils.get_txt_pos(p.parse(preamble + latex))
    assert len(plain) == len(nums)
    return plain
Exemplo n.º 15
0
def test_1():
    parms = parameters.Parameters()
    parms.macro_defs_python.append(
        defs.Macro(parms, '\\xxx', args='*OA', repl='#3'))
    p = parser.Parser(parms)

    toks = p.parse(latex_1)
    plain, pos = utils.get_txt_pos(toks)
    assert plain_1_1 == plain

    toks = p.parse(latex_1, extract=['\\xxx'])
    plain, pos = utils.get_txt_pos(toks)
    assert plain_1_2 == plain
Exemplo n.º 16
0
def test_2():
    parms = parameters.Parameters()
    parms.macro_defs_python.append(
        defs.Macro(parms,
                   '\\xxx',
                   args='OA',
                   repl='a#1b',
                   defaults=['X'],
                   extract='a#1b#2'))
    p = parser.Parser(parms)
    toks = p.parse(latex_2)
    plain, pos = utils.get_txt_pos(toks)
    assert plain_2 == plain
Exemplo n.º 17
0
def test_8(capsys):
    capsys.readouterr()

    def read(file):
        return False, ''

    parms = parameters.Parameters()
    p = parser.Parser(parms, read_macros=read)
    toks = p.parse(latex_8, source='t.tex')
    plain, pos = utils.get_txt_pos(toks)
    captured = capsys.readouterr()
    assert plain_8 == plain
    assert stderr_8 == captured.err
Exemplo n.º 18
0
def get_ml(latex, lang='de-DE'):
    parms = parameters.Parameters(language=lang)
    parms.multi_language = True

    babel.modify_language_map('lang1', 'de-DE')
    babel.modify_language_map('lang2', 'en-GB')
    babel.modify_language_map('lang3', 'ru-RU')

    p = parser.Parser(parms)
    toks = p.parse(prefix + latex)
    ml = utils.get_txt_pos_ml(toks, lang, parms)
    for lang in ml:
        for part in ml[lang]:
            assert len(part[0]) == len(part[1])
    return ml
Exemplo n.º 19
0
def test_7(capsys):
    capsys.readouterr()

    def read(file):
        return True, '\\LTinput{t.tex}\n'

    try:
        parms = parameters.Parameters()
        p = parser.Parser(parms, read_macros=read)
        toks = p.parse(latex_7)
    except SystemExit:
        # catch exit() in the error routine utils.fatal()
        pass
    captured = capsys.readouterr()
    # remove first message line: it contains the name of Python
    err = '\n'.join(captured.err.split('\n')[1:])
    assert stderr_7 == err
Exemplo n.º 20
0
def get_plain(latex):
    parms = parameters.Parameters()
    p = parser.Parser(parms)
    plain, nums = utils.get_txt_pos(p.parse(preamble + latex))
    assert len(plain) == len(nums)
    return plain
Exemplo n.º 21
0
#
#   - test  of \newcommand with default value for optional argument
#   - test of \def
#

from yalafi import parameters, parser, utils

p = parser.Parser(parameters.Parameters())

latex1 = r"""
\newcommand{\xxx}[1][X]{#1Z}
\xxx
\xxx
[A]
"""
plain1_should_be = r"""
XZAZ
"""


def test_1():
    toks = p.parse(latex1)
    plain1, pos1 = utils.get_txt_pos(toks)

    assert plain1_should_be == plain1


latex2 = r"""
\newcommand{\xxx}[2][X]{#2#1}
\xxx{ A }
\xxx[B]C
Exemplo n.º 22
0
def test_2():
    p = parser.Parser(parameters.Parameters())
    toks = p.parse(latex_2)
    plain, pos = utils.get_txt_pos(toks)
    assert plain_2 == plain
Exemplo n.º 23
0
#
#   test  of environments
#

from yalafi import defs, parameters, parser, utils

parms = parameters.Parameters('de')
parms.environment_defs.append(
    defs.Environ(parms, 'table', repl='[Tabelle]', remove=True))
p = parser.Parser(parms)

latex_proof = r"""
\usepackage{amsthm}
1\begin{proof}
2\end{proof}3

A\begin{proof}B\end{proof}C

X\begin{proof}[Proof]Y\end{proof}Z
"""
plain_proof_should_be = r"""
1

Beweis.
2

3

A

Beweis.
Exemplo n.º 24
0
from yalafi import parameters, parser, utils

add_macros = r"""
\newcommand{\swap}[2]{#2#1}
"""

parms = parameters.Parameters()
parms.macro_defs_latex += add_macros
p = parser.Parser(parms)

latex1 = r"""
\swap
{12}
\textbackslash
"""
plain1_should_be = r"""
\12
"""


def test_1():
    toks = p.parse(latex1)
    plain1, pos1 = utils.get_txt_pos(toks)

    assert plain1_should_be == plain1

    assert pos1[0] == 0  # initial '\n' in plain
    assert pos1[1] == 12  # \, position of \textbackslash
    assert pos1[2] == 8  # 1 of 12
    assert pos1[3] == 9  # 2 of 12
Exemplo n.º 25
0
def test_4():
    p = parser.Parser(parameters.Parameters(language='ru'))
    toks = p.parse(latex_4)
    plain, pos = utils.get_txt_pos(toks)
    assert plain_4 == plain
Exemplo n.º 26
0
if not cmdline.lt_directory:
    if cmdline.lt_command:
        cmdline.lt_directory = '.'
    else:
        cmdline.lt_directory = default_option_lt_directory

if cmdline.context < 0:
    # huge context: display whole text
    cmdline.context = int(1e8)
if not (cmdline.file or cmdline.as_server or cmdline.server == 'stop'):
    tex2txt.fatal('no input file given')
if cmdline.plain_input and (cmdline.include or cmdline.replace):
    tex2txt.fatal('cannot handle --plain-input together with'
                                        + ' --include or --replace')

lc = parameters.Parameters(cmdline.language).lang_context
equation_replacements_display = r'|'.join(set(
                    lc.math_repl_display + lc.math_repl_display_vowel))
equation_replacements_inline = r'|'.join(set(
                    lc.math_repl_inline + lc.math_repl_inline_vowel))
equation_replacements = r'|'.join(set(
                    lc.math_repl_display + lc.math_repl_display_vowel
                    + lc.math_repl_inline + lc.math_repl_inline_vowel))
if cmdline.single_letters and cmdline.single_letters.endswith('||'):
    repls = (lc.math_repl_display + lc.math_repl_display_vowel
                    + lc.math_repl_inline + lc.math_repl_inline_vowel)
    if cmdline.multi_language:
        repls += lc.lang_change_repl + lc.lang_change_repl_vowel
    cmdline.single_letters += r'|'.join(set(repls))

if cmdline.replace:
Exemplo n.º 27
0
#
#   - test of nesting for macros
#   - test of nesting for environments with replacement
#

from yalafi import defs, parameters, parser, utils

parms = parameters.Parameters('en')
parms.environment_defs.append(
    defs.Environ(parms, 'table', repl='[Tabelle]', remove=True))
p = parser.Parser(parms)


def test_nested_macro():

    latex = '\\usepackage{xcolor}\\textcolor{x}{\\textcolor{y}{z}}'
    toks = p.parse(latex)
    plain, pos = utils.get_txt_pos(toks)
    assert plain == 'z'


def test_nested_table():

    latex = '\\begin{table}A\\begin{table}B\\end{table}C\\end{table}'
    toks = p.parse(latex)
    plain, pos = utils.get_txt_pos(toks)
    assert plain == '\n\n[Tabelle]\n\n'

    latex = '\\begin{table}A\\begin{tablx}B\\end{table}C\\end{table}'
    toks = p.parse(latex)
    plain, pos = utils.get_txt_pos(toks)
Exemplo n.º 28
0
def test_figure():
    p = parser.Parser(parameters.Parameters())
    toks = p.parse(latex_figure)
    plain, pos = utils.get_txt_pos(toks)
    assert plain_figure == plain
Exemplo n.º 29
0
def get_plain_pos(latex, preamble='', lang='en-GB'):
    parms = parameters.Parameters(language=lang)
    p = parser.Parser(parms)
    plain, nums = utils.get_txt_pos(p.parse(preamble + latex))
    assert len(plain) == len(nums)
    return plain, nums