Example #1
0
def test_latex_comments():

    # "normal" comment
    latex = 'a %x\nb\n'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'a \nb\n'
    assert nums == [1, 2, 5, 6, 7, 8]

    # join lines
    latex = 'a%x\n  b\n'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'ab\n'
    assert nums == [1, 7, 8, 9]

    # join lines: protect macro name
    latex = 'a\\aa%x\nb\n'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'aƄb\n'
    assert nums == [1, -2, 8, 9, 10]

    # do not join lines, if next line empty
    latex = 'a%x\n\nb\n'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'a\n\nb\n'
    assert nums == [1, 4, 5, 6, 7, 8]

    # remove pure comment lines
    latex = 'a %x\n %x\nb\n'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'a \nb\n'
    assert nums == [1, 2, 5, 10, 11, 12]
Example #2
0
def test_remove_blank_lines_left_by_macros():

    # a normal macro: \label
    latex = 'a\n\\label{x}\nb\n'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'a\nb\n'
    assert nums == [1, 2, 13, 14, 15]

    # macro plus comment
    latex = 'a\n\\label{x} %x\nb\n'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'a\nb\n'
    assert nums == [1, 2, 16, 17, 18]

    # \begin and \end
    latex = 'a\n\\begin{x}\nb\n\\end{x}\nc\n'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'a\nb\nc\n'
    assert nums == [1, 2, 13, 14, 23, 24, 25]

    # (actually no blank lines)
    latex = 'a\n\\begin{x}b\n\\end{x}c\n'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'a\nb\nc\n'
    assert nums == [1, 2, 12, 13, 21, 22, 23]
Example #3
0
def test_nested_table():

    latex = '\\begin{table}A\\begin{table}B\\end{table}C\\end{table}'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == '[Tabelle].'

    latex = '\\begin{table}A\\begin{tablx}B\\end{table}C\\end{table}'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == '[Tabelle].C'
Example #4
0
def test_cite():

    latex = '\\cite{x}'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == '[1]'

    latex = '\\cite[y]{x}'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == '[1, y]'
Example #5
0
def test_proof():

    latex = '\\begin{proof}'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'Proof.'

    latex = 'A \\begin{proof}[Test] B'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'A Test. B'
    assert nums == [1, 2, 17, 18, 19, 20, 21, 22, 23, 24]
Example #6
0
def test_unknown_macro():

    latex = 'a\\xxx b'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'ab'

    latex = 'a\\xxx \n{b} c'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'ab c'

    latex = 'a\\xxx\n\n{b} c'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'a\n\nb c'
Example #7
0
def test_renewcommand():

    latex = 'a\\newcommand\n{\\x}\n{y}b'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'ab'

    latex = 'a\\newcommand{\\x}[1][z]{y}b'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'ab'

    latex = 'a\\newcommand*{\\x}[1][z]{y}b'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'ab'

    latex = 'a\\renewcommand{\\x}[1][z]{y}b'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'ab'

    latex = 'a\\newcommand\n\n{\\x}[1]{y}b'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'a\n\n[1]yb'

    latex = 'a\\newcommand{\\x}\n\n[1]{y}b'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'a\n\n[1]yb'

    latex = 'a\\newcommand{\\x}[1]\n\n{y}b'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'a[1]\n\nyb'
Example #8
0
def test_footnotemark():

    latex = 'a\\footnotemark b'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'ab'

    latex = 'a\\footnotemark[1] b'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'a b'

    latex = 'a\\footnotemark\n[1]b'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'ab'

    # do not cross paragraph border
    latex = '\\footnotemark\n\n[1]'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == '\n[1]'
Example #9
0
def test_macro_arguments():

    # normal expansion
    latex = '\\textcolor\n{red}\n{blue}'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'blue'
    assert nums == [19, 20, 21, 22, 24]

    # no expansion: argument in next paragraph
    latex = '\\textcolor\n{red}\n \n{blue}'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'red\n \nblue'

    # no expansion: argument in next paragraph
    latex = '\\textcolor\n\n{red}\n{blue}'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == '\nred\nblue'

    # expansion: comment line
    latex = '\\textcolor\n %x\n{red}\n{blue}'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'blue'
Example #10
0
def run_proofreader(file):

    sys.stderr.write('=== ' + file + '\n')
    sys.stderr.flush()
    f = tex2txt.myopen(file, encoding=cmdline.encoding)
    tex = f.read()
    f.close()
    if not tex.endswith('\n'):
        tex += '\n'

    if cmdline.plain:
        (plain, charmap) = (tex, list(range(1, len(tex) + 1)))
    else:
        (plain, charmap) = tex2txt.tex2txt(tex, options)
        if cmdline.list_unknown:
            # only look for unknown macros and environemnts
            return (tex, plain, charmap, [])

    # here, we could dispatch to other tools, see for instance
    #   - https://textgears.com/api
    #   - Python package prowritingaid.python
    #
    if cmdline.textgears:
        matches = run_textgears(plain)
    else:
        matches = run_languagetool(plain)

    matches += create_single_letter_matches(plain)
    matches += create_equation_punct_messages(plain)

    # sort matches according to position in LaTeX text
    #
    def f(m):
        beg = json_get(m, 'offset', int)
        if beg < 0 or beg >= len(charmap):
            tex2txt.fatal('run_proofreader():' +
                          ' bad message read from proofreader')
        return abs(charmap[beg])

    matches.sort(key=f)

    return (tex, plain, charmap, matches)
Example #11
0
def test_system_macros():

    latex = 'a\\footnote[2]{x}b'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'ab\n\n\nx\n'
Example #12
0
def test_nested_macro():

    latex = '\\textcolor{x}{\\textcolor{y}{z}}'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == 'z'
Example #13
0
    return cmdline.skip and re.search(r'\A' + cmdline.skip + r'\Z', fn)


todo = cmdline.file
done = []
while todo:
    f = todo.pop(0)
    if f in done or skip_file(f):
        continue
    done.append(f)
    if not cmdline.include:
        continue
    fp = tex2txt.myopen(f, encoding=cmdline.encoding)
    tex = fp.read()
    fp.close()
    (plain, _) = tex2txt.tex2txt(tex, opts)
    for f in plain.split():
        if not f.endswith('.tex'):
            f += '.tex'
        if f not in done + todo and not skip_file(f):
            todo.append(f)

cmdline.file = done
if cmdline.include:
    sys.stderr.write(', '.join(cmdline.file) + '\n')
    sys.stderr.flush()

# prepare options for tex2txt()
#
options = tex2txt.Options(char=True,
                          repl=cmdline.replace,
Example #14
0
def test_verbatim():

    # extra blank lines: see LAB:VERBATIM in tex2txt.py
    latex = '\\begin{verbatim}\\verb?%\\x?\n\\end{verbatim}'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == '\n\n\\verb?%\\x?\n\n'
Example #15
0
    #                                           encoding=input_encoding),
    #           defs=tex2txt.read_definitions('Tools/LT/defs.py',
    #                                           encoding='utf-8'),
    lang='en')

for file in sys.argv[1:]:

    sys.stderr.write('=== ' + file + '\n')
    sys.stderr.flush()

    # read file and call tex2txt()
    #
    f = tex2txt.myopen(file, encoding=input_encoding)
    tex = f.read()
    f.close()
    (plain, charmap) = tex2txt.tex2txt(tex, options)
    starts = tex2txt.get_line_starts(plain)

    # call LanguageTool
    #
    out = subprocess.run(ltcmd,
                         input=plain.encode(encoding='utf-8'),
                         stdout=subprocess.PIPE)
    if 'Windows' in os.getenv('OS', default=''):
        # under Windows, LanguageTool produces Latin-1 output
        msg = out.stdout.decode(encoding='latin-1')
    else:
        msg = out.stdout.decode(encoding='utf-8')

    lines = msg.splitlines(keepends=True)
    if lines:
Example #16
0
\textcolor{red}{redx colour.}}
is lazy.
"""

plain_t = r"""
Only few people
is lazy.



We use
redx colour.
"""

nums_t = [
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 64, 65, 66, 67, 68,
    69, 70, 71, 72, 73, 27, 27, 27, 27, 28, 29, 30, 31, 32, 33, 50, 51, 52, 53,
    54, 55, 56, 57, 58, 59, 60, 61, 63, 74
]

options = tex2txt.Options(lang='en', char=True)
plain, nums = tex2txt.tex2txt(latex, options)


def test_text():
    assert plain == plain_t


def test_nums():
    assert nums == nums_t
Example #17
0
def test_verb():

    latex = '\\verb?%x\\y?\\label{z}?'
    plain, nums = tex2txt.tex2txt(latex, options)
    assert plain == '%x\\y?'