def test_package_resource(tmpdir): config = defaultConfig() config['general'].data['packages-dirs'].value = [ str(Path(__file__).parent) ] addConfig(config) doc = TeXDocument(config=config) tex = TeX(doc) tex.input(""" \\documentclass{article} \\usepackage{examplePackage} \\begin{document} \\emph{Hello} \\end{document}""") doc = tex.parse() doc.userdata['working-dir'] = os.path.dirname(__file__) with tmpdir.as_cwd(): Renderer().render(doc) assert tmpdir.join('styles', 'test.css').isfile() assert tmpdir.join('js', 'test.js').isfile() assert 'class="em"' in tmpdir.join('index.html').read() assert doc.userdata['testing'] == 'test'
def test_plugin_packages(tmpdir): tmpdir = Path(str(tmpdir)) (tmpdir/'my_plugin'/'Packages').mkdir(parents=True) (tmpdir/'my_plugin'/'__init__.py').touch() (tmpdir/'my_plugin'/'Packages'/'mypkg.py').write_text( r""" from plasTeX import Command class mycmd(Command): my_var = 'ok' """) sys.path.append(str(tmpdir)) doc = TeXDocument() doc.config['general'].data['plugins'].value = ['my_plugin'] tex = TeX(doc) tex.input(r""" \documentclass{article} \usepackage{mypkg} \begin{document} \mycmd \end{document} """) tex.parse() sys.path.pop() assert doc.getElementsByTagName('mycmd') node = doc.getElementsByTagName('mycmd')[0] assert node.my_var == 'ok'
def test_packages_dirs(tmpdir): tmpdir = Path(str(tmpdir)) (tmpdir / "mypkg.py").write_text( r""" from plasTeX import Command class mycmd(Command): my_var = 'ok' """) doc = TeXDocument() doc.config['general'].data['packages-dirs'].value = [str(tmpdir)] tex = TeX(doc) tex.input(r""" \documentclass{article} \usepackage{mypkg} \begin{document} \mycmd \end{document} """) tex.parse() assert doc.getElementsByTagName('mycmd') node = doc.getElementsByTagName('mycmd')[0] assert node.my_var == 'ok'
def test_builtin_packages(): doc = TeXDocument() tex = TeX(doc) tex.input(r""" \documentclass{article} \usepackage{float} \begin{document} \floatstyle{ruled} \end{document} """) tex.parse() assert doc.getElementsByTagName('floatstyle')
def test_package_resource(tmpdir): doc = TeXDocument(config=config+html5_config) tex = TeX(doc) tex.input(""" \\documentclass{article} \\usepackage{examplePackage} \\begin{document} \\emph{Hello} \\end{document}""") doc = tex.parse() doc.userdata['working-dir'] = os.path.dirname(__file__) os.chdir(str(tmpdir)) Renderer().render(doc) assert tmpdir.join('styles', 'test.css').isfile() assert tmpdir.join('js', 'test.js').isfile() assert 'class="em"' in tmpdir.join('index.html').read() assert doc.userdata['testing'] == 'test'
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
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
def test_package_resource(tmpdir): doc = TeXDocument(config=config+html5_config) tex = TeX(doc) tex.input(""" \documentclass{article} \usepackage{examplePackage} \\begin{document} \emph{Hello} \end{document}""") doc = tex.parse() doc.userdata['working-dir'] = os.path.dirname(__file__) os.chdir(str(tmpdir)) Renderer().render(doc) assert tmpdir.join('styles', 'test.css').isfile() assert tmpdir.join('js', 'test.js').isfile() assert 'class="em"' in tmpdir.join('index.html').read() assert doc.userdata['testing'] == 'test'
def runDocument(packages='', content=''): """ Compile a document with the given content Arguments: packages - string containing comma separated packages to use content - string containing the content of the document Returns: TeX document """ doc = TeXDocument(config=config) tex = TeX(doc) tex.disableLogging() tex.input(r''' \documentclass{article} \usepackage{%s} \begin{document}%s\end{document}''' % (packages, content)) doc = tex.parse() doc.userdata['working-dir'] = os.path.dirname(__file__) return doc
def test_packages_dirs_name_clash(tmpdir): tmpdir = Path(str(tmpdir)) (tmpdir / "plasTeX.py").write_text( r""" from plasTeX import Command class mycmd(Command): my_var = 'ok' """) doc = TeXDocument() doc.config['general'].data['packages-dirs'].value = [str(tmpdir)] tex = TeX(doc) assert not doc.context.loadPackage(tex, 'plasTeX')
def test_amsthm(tmpdir): root = Path(__file__).parent config = defaultConfig() addConfig(config) config['files']['split-level'] = -100 tex = TeX(TeXDocument(config=config)) tex.input((root / 'source.tex').read_text()) doc = tex.parse() doc.userdata['working-dir'] = Path(__file__).parent with tmpdir.as_cwd(): Renderer().render(doc) css = Path(tmpdir) / 'styles' / 'amsthm.css' css_bench = root / 'benchmark.css' html = Path(tmpdir) / 'index.html' html_bench = root / 'benchmark.html' assert html.exists() text = re.sub('id="[^"]*"', '', html.read_text()) bench = re.sub('id="[^"]*"', '', html_bench.read_text()) assert text.strip() == bench.strip() assert css.exists() assert css.read_text() == css_bench.read_text()
def doc(): doc = TeXDocument() doc.rendererdata['html'] = {} return doc
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
def test_load_class(doc_class): doc = TeXDocument() tex = TeX(doc) doc.context.loadPackage(tex, doc_class)