Exemplo n.º 1
0
def test_charsub():
    config = base_config.copy()

    doc = TeXDocument(config=config)
    tex = TeX(doc)

    p = tex.input(r'''{``'' '---}''').parse()[0]
    p.paragraphs()
    assert p.textContent == "“” ’—"
Exemplo n.º 2
0
def test_modify_charsub():
    config = base_config.copy()
    config["document"]["disable-charsub"] = "'"

    doc = TeXDocument(config=config)
    tex = TeX(doc)

    p = tex.input(r'''{``'' '---}''').parse()[0]
    p.paragraphs()
    assert p.textContent == "“” '—"
Exemplo n.º 3
0
    def doc_fun(compiler=None, converter=None, template=None):
        conf = config.copy()
        if compiler:
            conf['html5']['tikz-compiler'] = compiler
        if converter:
            conf['html5']['tikz-converter'] = converter
        if template:
            conf['html5']['tikz-cd-template'] = template

        doc = TeXDocument(config=conf)
        tex = TeX(doc)
        tex.input(
            "\\documentclass{article}\\usepackage{tikz-cd}\n\\begin{document}\\begin{tikzcd}A \\rar  & B\\end{tikzcd}\n\\end{document}"
        )
        doc = tex.parse()
        doc.userdata['working-dir'] = '.'
        doc.rendererdata['html5'] = {}
        return doc
Exemplo n.º 4
0
    def doc_fun(compiler=None, converter=None, template=None):
        conf = config.copy()
        if compiler:
            conf['html5']['tikz-compiler'] = compiler
        if converter:
            conf['html5']['tikz-converter'] = converter
        if template:
            conf['html5']['tikz-template'] = template

        doc = TeXDocument(config=conf)
        tex = TeX(doc)
        tex.input(
            r"\documentclass{article}\usepackage{tikz}\begin{document}\begin{tikzpicture}\draw (0,0) -- (1,0);\end{tikzpicture}\end{document}"
        )
        doc = tex.parse()
        doc.userdata['working-dir'] = '.'
        doc.rendererdata['html5'] = {}
        return doc
Exemplo n.º 5
0
def run(f: Path, target_dir: str, options={}):
    f = f.absolute() # We will later change directories
    source_dir = f.parent

    if f.suffix != ".tex":
        raise ValueError("{} does not have extension .tex".format(filename))

    print("Compiling {}".format(f.name))

    renderer = str(Path("NotesRenderer").absolute())

    cwd = Path.cwd()
    os.chdir(str(source_dir))

    with TemporaryDirectory() as tmp_dir:
        config = config_base.copy() + config_extra

        config.set("images", "imager", "gspdfpng")
        config.set("images", "vector-imager", "pdf2svg")
        config.set("images", "scale-factor", 1.4)
        config.set("general", "renderer", renderer)
        config.set("files", "directory", tmp_dir)

        for key, value in options.items():
            config.set("notes", key, value)

        run_(f.name, config)

        mkpath(target_dir)
        rm_tree(target_dir)
        copy_tree(tmp_dir, target_dir)
        shutil.copy(f.name, target_dir)
        try:
            shutil.copy(f.stem + ".pdf", target_dir)
        except OSError:
            pass

        try:
            os.remove(f.stem + ".paux")
        except OSError:
            pass

    os.chdir(str(cwd))
Exemplo n.º 6
0
def test_tikz(tmpdir):
    try:
        tmpdir = Path(tmpdir)
    except TypeError: # Fallback for older python
        tmpdir = Path(str(tmpdir))

    root = Path(__file__).parent
    test_id = "tikz-pdf2svg-gspdfpng"

    benchdir = root / "benchmarks" / test_id
    newdir = root / "new" / test_id

    images = ["images/img-0001.svg", "images/img-0002.svg", "images/img-0001.png", "images/img-0002.png"]

    config = base_config.copy()
    config['images']['vector-imager'] = 'pdf2svg'
    config['images']['imager'] = 'gspdfpng'

    doc = TeXDocument(config=config)
    tex = TeX(doc)

    tex.input(r'''
    \documentclass{article}
    \usepackage{tikz}
    \usepackage{tikz-cd}
    \begin{document}
    \begin{tikzpicture}
        \draw (0, 0) -- (0, 2) -- (2, 0) -- (0, 0);
    \end{tikzpicture}

    \begin{tikzcd}
        A \ar[r, "i"] & B \ar[d, "p"] \\ & C
    \end{tikzcd}
    \end{document}
    ''')

    renderer = Renderer()

    def render_image(obj):
        return "{}\n{}\n".format(obj.vectorImage.url, obj.image.url)

    renderer["tikzpicture"] = render_image
    renderer["tikzcd"] = render_image

    cwd = os.getcwd()
    os.chdir(str(tmpdir))
    renderer.render(tex.parse())
    os.chdir(cwd)

    def new_file(base: str):
        newfile = newdir / base
        newfile.parent.mkdir(parents=True, exist_ok=True)
        shutil.copy(str(tmpdir / base), str(newfile))

    # Compare image output
    error = False

    try:
        if not filecmp.cmp(str(tmpdir / "index"), str(benchdir / "index"), shallow=False):
            print('Differences were found: index', file=sys.stderr)
            error = True
            new_file("index")

    except FileNotFoundError as e:
        error = True
        if e.filename == str(tmpdir / "index"):
            print("Missing output file: index", file=sys.stderr)
        else:
            print("Missing benchmark file: index", file=sys.stderr)
            new_file("index")

    for f in images:
        if not (tmpdir / f).exists():
            error = True
            print('Missing output file: %s' % f, file=sys.stderr)
        elif not (benchdir / f).exists():
            error = True
            print('Missing benchmark file: %s' % f, file=sys.stderr)
            new_file(f)

        diff = cmp_img(str(tmpdir / f), str(benchdir / f))
        if diff > 0.0001:
            error = True
            print('Differences were found: {} ({})'.format(f, diff), file=sys.stderr)
            new_file(f)

    if error:
        assert False