90 lines
2.3 KiB
Python
90 lines
2.3 KiB
Python
import uno
|
|
import unohelper
|
|
from com.sun.star.task import XJobExecutor
|
|
import easymacro2 as app
|
|
|
|
|
|
ID_EXTENSION = 'net.elmau.zaz.latex2svg'
|
|
SERVICE = ('com.sun.star.task.Job',)
|
|
|
|
|
|
class ZAZLaTex2SVG(unohelper.Base, XJobExecutor):
|
|
|
|
def __init__(self, ctx):
|
|
self.ctx = ctx
|
|
|
|
def trigger(self, args='pyUNO'):
|
|
if args == 'app':
|
|
self._app()
|
|
return
|
|
|
|
self._from_selection()
|
|
return
|
|
|
|
def _app(self):
|
|
result = 'No encontrado'
|
|
if app.paths.exists_app('pdflatex'):
|
|
result = 'Encontrado'
|
|
msg = f'pdflatex = {result}\n'
|
|
|
|
result = 'No encontrado'
|
|
if app.paths.exists_app('pdfcrop'):
|
|
result = 'Encontrado'
|
|
msg += f'pdfcrop = {result}\n'
|
|
|
|
result = 'No encontrado'
|
|
if app.paths.exists_app('pdf2svg'):
|
|
result = 'Encontrado'
|
|
msg += f'pdf2svg = {result}\n\n'
|
|
|
|
msg += 'No continues hasta tener las tres aplicaciones detectadas'
|
|
|
|
app.msgbox(msg)
|
|
return
|
|
|
|
@app.catch_exception
|
|
def _from_selection(self):
|
|
template = """\documentclass{{article}}
|
|
\\usepackage[a5paper, landscape]{{geometry}}
|
|
\\usepackage{{xcolor}}
|
|
\\usepackage{{amssymb}}
|
|
\\usepackage{{amsmath}}
|
|
\pagestyle{{empty}}
|
|
\\begin{{document}}
|
|
|
|
\[ {} \]
|
|
|
|
\end{{document}}
|
|
"""
|
|
doc = app.active
|
|
sel = doc.selection
|
|
if doc.type == 'writer':
|
|
sel = sel[0]
|
|
data = sel.value
|
|
|
|
data = template.format(data)
|
|
path_tmp = '/tmp'
|
|
path_tex = '/tmp/test.tex'
|
|
path_pdf = '/tmp/test.pdf'
|
|
path_svg = '/tmp/test.svg'
|
|
|
|
app.paths.save(path_tex, data)
|
|
cmd = f'pdflatex --interaction=batchmode -output-directory={path_tmp} {path_tex}'
|
|
app.run(cmd)
|
|
cmd = f'pdfcrop {path_pdf} {path_pdf}'
|
|
app.run(cmd)
|
|
cmd = f'pdf2svg {path_pdf} {path_svg}'
|
|
app.run(cmd)
|
|
|
|
sel = sel.offset()
|
|
args = {}
|
|
if doc.type == 'writer':
|
|
args = {'Width': 5000, 'Height': 2000}
|
|
sel.insert_image(path_svg, args)
|
|
|
|
return
|
|
|
|
|
|
g_ImplementationHelper = unohelper.ImplementationHelper()
|
|
g_ImplementationHelper.addImplementation(ZAZLaTex2SVG, ID_EXTENSION, SERVICE)
|