X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/fefdce4e24f9e397df5538fe6e7f54b5ece4d841..ac9ba215113d29655e142f23ab398c5c2765df1e:/src/librarian/embeds/latex.py?ds=sidebyside diff --git a/src/librarian/embeds/latex.py b/src/librarian/embeds/latex.py index 8425d03..4d3f1f0 100644 --- a/src/librarian/embeds/latex.py +++ b/src/librarian/embeds/latex.py @@ -1,6 +1,6 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - +# This file is part of Librarian, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Wolne Lektury. See NOTICE for more information. +# import os import shutil from subprocess import call, PIPE @@ -12,14 +12,16 @@ from . import DataEmbed, create_embed, downgrades_to class LaTeX(DataEmbed): @downgrades_to('image/png') def to_png(self): - tmpl = open(get_resource('res/embeds/latex/template.tex'), 'rb').read().decode('utf-8') + with open(get_resource('res/embeds/latex/template.tex'), 'rb') as f: + tmpl = f.read().decode('utf-8') tempdir = mkdtemp('-librarian-embed-latex') fpath = os.path.join(tempdir, 'doc.tex') with open(fpath, 'wb') as f: f.write((tmpl % {'code': self.data}).encode('utf-8')) - call(['xelatex', '-interaction=batchmode', '-output-directory', tempdir, fpath], stdout=PIPE, stderr=PIPE) - call(['convert', '-density', '150', os.path.join(tempdir, 'doc.pdf'), '-trim', - os.path.join(tempdir, 'doc.png')]) + call(['xelatex', '-interaction=batchmode', '-output-directory', + tempdir, fpath], stdout=PIPE, stderr=PIPE) + call(['convert', '-density', '150', os.path.join(tempdir, 'doc.pdf'), + '-trim', os.path.join(tempdir, 'doc.png')]) pngdata = open(os.path.join(tempdir, 'doc.png'), 'rb').read() shutil.rmtree(tempdir) return create_embed('image/png', data=pngdata)