First version

This commit is contained in:
Mauricio Baeza 2020-11-04 21:46:38 -06:00
commit 6c4c9b4945
22 changed files with 5643 additions and 1 deletions

56
README.es.md Normal file
View File

@ -0,0 +1,56 @@
# zaz-latex2svg
Compila ecuaciones Latex a SVG dentro de LibreOffice
Requerimientos:
* LibreOffice 7.0+
* Python 3.7+
* pdflatex
* pdfcrop
* pdf2svg
## Software libre, no gratis.
### Si no tienes dinero, no es problema, mandame una postal de tu ciudad :)
#### pero, no cometas el error de muchos de *pensar solo en software gratis* que tanto daño a hecho al **Software Libre**.
Esta extensión tiene un costo de mantenimiento de 5 euros al año.
BCH: `qztd3l00xle5tffdqvh2snvadkuau2ml0uqm4n875d`
BTC: `3FhiXcXmAesmQzrNEngjHFnvaJRhU1AGWV`
ETH: `0x61a4f614a30ff686445751ed8328b82b77ecfc69`
LTC: `MBcgQ3LQJA4W2wsXknTdm2fxRSysLaBJHS`
* En ArchLinux
```
sudo pacman -S texlive-core
sudo pacman -S pdf2svg
```
* En Ubuntu 20.04
```
sudo apt install texlive-latex-base
sudo apt install texlive-extra-utils
sudo apt install pdf2svg
```
* En OSx
```
brew install mactex
brew install tex-live-utility
sudo tlmgr install --reinstall pdfcrop
brew install pdf2svg
```

View File

@ -1,3 +1,48 @@
# zaz-latex2svg
Compile Latex equations to SVG into LibreOffice
[Leer en español](README.es.md)
Compile Latex equations to SVG into LibreOffice
Requirements:
* LibreOffice 7.0+
* Python 3.7+
* pdflatex
* pdfcrop
* pdf2svg
## Free Software, not gratis software
### If you don't have money, no problem, send me a postcard from your city :)
#### but, don't make the mistake of many of *thinking only in gratis software* that so much damage has done to **Free Software**.
This extension have a cost of maintenance of 5 euros every year.
BCH: `qztd3l00xle5tffdqvh2snvadkuau2ml0uqm4n875d`
BTC: `3FhiXcXmAesmQzrNEngjHFnvaJRhU1AGWV`
ETH: `0x61a4f614a30ff686445751ed8328b82b77ecfc69`
LTC: `MBcgQ3LQJA4W2wsXknTdm2fxRSysLaBJHS`
* For ArchLinux
```
sudo pacman -S texlive-core
sudo pacman -S pdf2svg
```
* For Ubuntu 20.04+
```
sudo apt install texlive-latex-base
sudo apt install texlive-extra-utils
sudo apt install pdf2svg
```

1
VERSION Normal file
View File

@ -0,0 +1 @@
0.1.0

440
conf.py Normal file
View File

@ -0,0 +1,440 @@
#!/usr/bin/env python3
# ~ This file is part of ZAZ.
# ~ ZAZ is free software: you can redistribute it and/or modify
# ~ it under the terms of the GNU General Public License as published by
# ~ the Free Software Foundation, either version 3 of the License, or
# ~ (at your option) any later version.
# ~ ZAZ is distributed in the hope that it will be useful,
# ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
# ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# ~ GNU General Public License for more details.
# ~ You should have received a copy of the GNU General Public License
# ~ along with ZAZ. If not, see <https://www.gnu.org/licenses/>.
import logging
# ~ Type extension:
# ~ 1 = normal extension
# ~ 2 = new component
# ~ 3 = Calc addin
TYPE_EXTENSION = 1
# ~ https://semver.org/
VERSION = '0.1.0'
# ~ Your great extension name, not used spaces or "-_"
NAME = 'ZAZLaTex2SVG'
# ~ Should be unique, used URL inverse
ID = 'net.elmau.zaz.latex2svg'
# ~ If you extension will be multilanguage set: True
# ~ This feature used gettext, set pythonpath and easymacro in True
# ~ You can used PoEdit for edit PO files and generate MO files.
# ~ https://poedit.net/
USE_LOCALES = True
DOMAIN = 'base'
PATH_LOCALES = 'locales'
PATH_PYGETTEXT = '/usr/lib/python3.8/Tools/i18n/pygettext.py'
PATH_MSGMERGE = 'msgmerge'
# ~ Show in extension manager
PUBLISHER = {
'en': {'text': 'El Mau', 'link': 'https://gitlab.com/mauriciobaeza'},
'es': {'text': 'El Mau', 'link': 'https://gitlab.com/mauriciobaeza'},
}
# ~ Name in this folder for copy
ICON = 'images/logo.png'
# ~ Name inside extensions
ICON_EXT = f'{NAME.lower()}.png'
# ~ For example
# ~ DEPENDENCIES_MINIMAL = '6.0'
DEPENDENCIES_MINIMAL = ''
# ~ Change for you favorite license
LICENSE_EN = f"""This file is part of {NAME}.
{NAME} is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
{NAME} is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with {NAME}. If not, see <https://www.gnu.org/licenses/>.
"""
LICENSE_ES = LICENSE_EN
INFO = {
'en': {
'display_name': NAME,
'description': 'Generate equations in SVG from LaTex',
'license': LICENSE_EN,
},
'es': {
'display_name': NAME,
'description': 'Genera ecuaciones en SVG desde LaTex',
'license': LICENSE_ES,
},
}
# ~ Menus, only for TYPE_EXTENSION = 1
# ~ Parent can be: AddonMenu or OfficeMenuBar
# ~ For icons con name: NAME_16.bmp, used only NAME
# ~ PARENT = ''
# ~ MENU_MAIN = {}
# ~ Shortcut: Key + "Modifier Keys"
# ~ Important: Not used any shortcuts used for LibreOffice
# ~ SHIFT is mapped to Shift on all platforms.
# ~ MOD1 is mapped to Ctrl on Windows/Linux, while it is mapped to Cmd on Mac.
# ~ MOD2 is mapped to Alt on all platforms.
# ~ For example: Shift+Ctrl+Alt+T -> T_SHIFT_MOD1_MOD2
PARENT = 'OfficeMenuBar'
MENU_MAIN = {
'en': 'ZAZ LaTex2Svg',
'es': 'ZAZ LaTex2Svg',
}
MENUS = (
{
'title': {'en': 'From selection', 'es': 'Desde selección'},
'argument': 'selection',
'context': 'calc,writer,draw,impress',
'icon': 'icon1',
'toolbar': False,
'shortcut': '',
},
{
'title': {'en': 'Insert...', 'es': 'Insertar...'},
'argument': 'dlg',
'context': 'calc,writer,draw,impress',
'icon': 'icon1',
'toolbar': False,
'shortcut': '',
},
{
'title': {'en': 'Validate applications', 'es': 'Validar aplicaciones'},
'argument': 'app',
'context': 'calc,writer,draw,impress',
'icon': 'icon2',
'toolbar': False,
'shortcut': '',
},
)
# ~ Functions, only for TYPE_EXTENSION = 3
FUNCTIONS = {
'test': {
'displayname': {'en': 'test', 'es': 'prueba'},
'description': {'en': 'My test', 'es': 'Mi prueba'},
'parameters': {
'value': {
'displayname': {'en': 'value', 'es': 'valor'},
'description': {'en': 'The value', 'es': 'El valor'},
},
},
},
}
# ~ FUNCTIONS = {}
EXTENSION = {
'version': VERSION,
'name': NAME,
'id': ID,
'icon': (ICON, ICON_EXT),
'languages': tuple(INFO.keys())
}
# ~ If used more libraries set python path in True and copy inside
# ~ If used easymacro pythonpath always is True, recommended
DIRS = {
'meta': 'META-INF',
'source': 'source',
'description': 'description',
'images': 'images',
'registration': 'registration',
'files': 'files',
'office': 'Office',
'locales': PATH_LOCALES,
'pythonpath': True,
}
FILES = {
'oxt': f'{NAME}_v{VERSION}.oxt',
'py': f'{NAME}.py',
'ext_desc': 'desc_{}.txt',
'manifest': 'manifest.xml',
'description': 'description.xml',
'idl': f'X{NAME}.idl',
'addons': 'Addons.xcu',
'urd': f'X{NAME}.urd',
'rdb': f'X{NAME}.rdb',
'update': f'{NAME.lower()}.update.xml',
'addin': 'CalcAddIn.xcu',
'shortcut': 'Accelerators.xcu',
'easymacro': True,
}
# ~ URLs for update for example
# ~ URL_XML_UPDATE = 'https://gitlab.com/USER/PROYECT/raw/BRANCH/FOLDERs/FILE_NAME'
URL_XML_UPDATE = ''
URL_OXT = ''
# ~ Default program for test: --calc, --writer, --draw
PROGRAM = '--calc'
# ~ Path to file for test
FILE_TEST = ''
PATHS = {
'idlc': '/usr/lib/libreoffice/sdk/bin/idlc',
'include': '/usr/share/idl/libreoffice',
'regmerge': '/usr/lib/libreoffice/program/regmerge',
'soffice': ('soffice', PROGRAM, FILE_TEST),
'install': ('unopkg', 'add', '-v', '-f', '-s'),
'profile': '/home/mau/.config/libreoffice/4/user',
'gettext': PATH_PYGETTEXT,
'msgmerge': PATH_MSGMERGE,
}
SERVICES = {
'job': "('com.sun.star.task.Job',)",
'addin': "('com.sun.star.sheet.AddIn',)",
}
FORMAT = '%(asctime)s - %(levelname)s - %(message)s'
DATE = '%d/%m/%Y %H:%M:%S'
LEVEL_ERROR = logging.getLevelName(logging.ERROR)
LEVEL_INFO = logging.getLevelName(logging.INFO)
logging.addLevelName(logging.ERROR, f'\033[1;41m{LEVEL_ERROR}\033[1;0m')
logging.addLevelName(logging.INFO, f'\x1b[32m{LEVEL_INFO}\033[1;0m')
logging.basicConfig(level=logging.DEBUG, format=FORMAT, datefmt=DATE)
log = logging.getLogger(NAME)
def _methods():
template = """ def {0}(self, {1}):
print({1})
return 'ok'\n"""
functions = ''
for k, v in FUNCTIONS.items():
args = ','.join(v['parameters'].keys())
functions += template.format(k, args)
return functions
SRV = SERVICES['job']
XSRV = 'XJobExecutor'
SRV_IMPORT = f'from com.sun.star.task import {XSRV}'
METHODS = """ def trigger(self, args='pyUNO'):
print('Hello World', args)
return\n"""
if TYPE_EXTENSION > 1:
MENUS = ()
XSRV = f'X{NAME}'
SRV_IMPORT = f'from {ID} import {XSRV}'
if TYPE_EXTENSION == 2:
SRV = f"('{ID}',)"
METHODS = """ def test(self, args='pyUNO'):
print('Hello World', args)
return\n"""
elif TYPE_EXTENSION == 3:
SRV = SERVICES['addin']
METHODS = _methods()
DATA_PY = f"""import uno
import unohelper
{SRV_IMPORT}
ID_EXTENSION = '{ID}'
SERVICE = {SRV}
class {NAME}(unohelper.Base, {XSRV}):
def __init__(self, ctx):
self.ctx = ctx
{METHODS}
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation({NAME}, ID_EXTENSION, SERVICE)
"""
def _functions():
a = '[in] any {}'
t = ' any {}({});'
f = ''
for k, v in FUNCTIONS.items():
args = ','.join([a.format(k) for k, v in v['parameters'].items()])
f += t.format(k, args)
return f
FILE_IDL = ''
if TYPE_EXTENSION > 1:
id_ext = ID.replace('.', '_')
interface = f'X{NAME}'
module = ''
for i, P in enumerate(ID.split('.')):
module += f'module {P} {{ '
close_module = '}; ' * (i + 1)
functions = ' void test([in] any argument);'
if TYPE_EXTENSION == 3:
functions = _functions()
FILE_IDL = f"""#ifndef __{id_ext}_idl__
#define __{id_ext}_idl__
#include <com/sun/star/uno/XInterface.idl>
{module}
interface {interface} : com::sun::star::uno::XInterface
{{
{functions}
}};
service {P} {{
interface {interface};
}};
{close_module}
#endif
"""
def _parameters(args):
NODE = """ <node oor:name="{name}" oor:op="replace">
<prop oor:name="DisplayName">
{displayname}
</prop>
<prop oor:name="Description">
{description}
</prop>
</node>"""
line = '{}<value xml:lang="{}">{}</value>'
node = ''
for k, v in args.items():
displayname = '\n'.join(
[line.format(' ' * 16, k, v) for k, v in v['displayname'].items()])
description = '\n'.join(
[line.format(' ' * 16, k, v) for k, v in v['description'].items()])
values = {
'name': k,
'displayname': displayname,
'description': description,
}
node += NODE.format(**values)
return node
NODE_FUNCTIONS = ''
if TYPE_EXTENSION == 3:
tmp = '{}<value xml:lang="{}">{}</value>'
NODE_FUNCTION = """ <node oor:name="{name}" oor:op="replace">
<prop oor:name="DisplayName">
{displayname}
</prop>
<prop oor:name="Description">
{description}
</prop>
<prop oor:name="Category">
<value>Add-In</value>
</prop>
<prop oor:name="CompatibilityName">
<value xml:lang="en">AutoAddIn.{name}</value>
</prop>
<node oor:name="Parameters">
{parameters}
</node>
</node>"""
for k, v in FUNCTIONS.items():
displayname = '\n'.join(
[tmp.format(' ' * 12, k, v) for k, v in v['displayname'].items()])
description = '\n'.join(
[tmp.format(' ' * 12, k, v) for k, v in v['description'].items()])
parameters = _parameters(v['parameters'])
values = {
'name': k,
'displayname': displayname,
'description': description,
'parameters': parameters,
}
NODE_FUNCTIONS += NODE_FUNCTION.format(**values)
FILE_ADDIN = f"""<?xml version="1.0" encoding="UTF-8"?>
<oor:component-data xmlns:oor="http://openoffice.org/2001/registry" xmlns:xs="http://www.w3.org/2001/XMLSchema" oor:name="CalcAddIns" oor:package="org.openoffice.Office">
<node oor:name="AddInInfo">
<node oor:name="{ID}" oor:op="replace">
<node oor:name="AddInFunctions">
{NODE_FUNCTIONS}
</node>
</node>
</node>
</oor:component-data>"""
DATA_MANIFEST = [FILES['py'], f"Office/{FILES['shortcut']}", 'Addons.xcu']
if TYPE_EXTENSION > 1:
DATA_MANIFEST.append(FILES['rdb'])
if TYPE_EXTENSION == 3:
DATA_MANIFEST.append('CalcAddIn.xcu')
DATA_DESCRIPTION = {
'identifier': {'value': ID},
'version': {'value': VERSION},
'display-name': {k: v['display_name'] for k, v in INFO.items()},
'icon': ICON_EXT,
'publisher': PUBLISHER,
'update': URL_XML_UPDATE,
}
DATA_ADDONS = {
'parent': PARENT,
'images': DIRS['images'],
'main': MENU_MAIN,
'menus': MENUS,
}
DATA = {
'py': DATA_PY,
'manifest': DATA_MANIFEST,
'description': DATA_DESCRIPTION,
'addons': DATA_ADDONS,
'update': URL_OXT,
'idl': FILE_IDL,
'addin': FILE_ADDIN,
}
with open('VERSION', 'w') as f:
f.write(VERSION)
# ~ LICENSE_ACCEPT_BY = 'user' # or admin
# ~ LICENSE_SUPPRESS_ON_UPDATE = True

1
easymacro2.py Symbolic link
View File

@ -0,0 +1 @@
/home/mau/Projects/libre_office/zaz/source/easymacro2.py

Binary file not shown.

BIN
images/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

72
source/Addons.xcu Normal file
View File

@ -0,0 +1,72 @@
<?xml version="1.0" encoding="utf-8"?>
<oor:component-data xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:oor="http://openoffice.org/2001/registry" oor:name="Addons" oor:package="org.openoffice.Office">
<node oor:name="AddonUI">
<node oor:name="OfficeMenuBar">
<node oor:name="net.elmau.zaz.latex2svg" oor:op="replace">
<prop oor:name="Title" oor:type="xs:string">
<value xml:lang="en">ZAZ LaTex2Svg</value>
<value xml:lang="es">ZAZ LaTex2Svg</value>
</prop>
<prop oor:name="Target" oor:type="xs:string">
<value>_self</value>
</prop>
<node oor:name="Submenu">
<node oor:name="m0" oor:op="replace">
<prop oor:name="Title" oor:type="xs:string">
<value xml:lang="en">From selection</value>
<value xml:lang="es">Desde selección</value>
</prop>
<prop oor:name="Context" oor:type="xs:string">
<value>com.sun.star.sheet.SpreadsheetDocument,com.sun.star.text.TextDocument,com.sun.star.drawing.DrawingDocument,com.sun.star.presentation.PresentationDocument</value>
</prop>
<prop oor:name="URL" oor:type="xs:string">
<value>service:net.elmau.zaz.latex2svg?selection</value>
</prop>
<prop oor:name="Target" oor:type="xs:string">
<value>_self</value>
</prop>
<prop oor:name="ImageIdentifier" oor:type="xs:string">
<value>%origin%/images/icon1</value>
</prop>
</node>
<node oor:name="m1" oor:op="replace">
<prop oor:name="Title" oor:type="xs:string">
<value xml:lang="en">Insert...</value>
<value xml:lang="es">Insertar...</value>
</prop>
<prop oor:name="Context" oor:type="xs:string">
<value>com.sun.star.sheet.SpreadsheetDocument,com.sun.star.text.TextDocument,com.sun.star.drawing.DrawingDocument,com.sun.star.presentation.PresentationDocument</value>
</prop>
<prop oor:name="URL" oor:type="xs:string">
<value>service:net.elmau.zaz.latex2svg?dlg</value>
</prop>
<prop oor:name="Target" oor:type="xs:string">
<value>_self</value>
</prop>
<prop oor:name="ImageIdentifier" oor:type="xs:string">
<value>%origin%/images/icon1</value>
</prop>
</node>
<node oor:name="m2" oor:op="replace">
<prop oor:name="Title" oor:type="xs:string">
<value xml:lang="en">Validate applications</value>
<value xml:lang="es">Validar aplicaciones</value>
</prop>
<prop oor:name="Context" oor:type="xs:string">
<value>com.sun.star.sheet.SpreadsheetDocument,com.sun.star.text.TextDocument,com.sun.star.drawing.DrawingDocument,com.sun.star.presentation.PresentationDocument</value>
</prop>
<prop oor:name="URL" oor:type="xs:string">
<value>service:net.elmau.zaz.latex2svg?app</value>
</prop>
<prop oor:name="Target" oor:type="xs:string">
<value>_self</value>
</prop>
<prop oor:name="ImageIdentifier" oor:type="xs:string">
<value>%origin%/images/icon2</value>
</prop>
</node>
</node>
</node>
</node>
</node>
</oor:component-data>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" manifest:version="1.2">
<manifest:file-entry manifest:full-path="ZAZLaTex2SVG.py" manifest:media-type="application/vnd.sun.star.uno-component;type=Python"/>
<manifest:file-entry manifest:full-path="Office/Accelerators.xcu" manifest:media-type="application/vnd.sun.star.configuration-data"/>
<manifest:file-entry manifest:full-path="Addons.xcu" manifest:media-type="application/vnd.sun.star.configuration-data"/>
</manifest:manifest>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<oor:component-data xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:oor="http://openoffice.org/2001/registry" oor:name="Accelerators" oor:package="org.openoffice.Office">
<node oor:name="PrimaryKeys"/>
</oor:component-data>

265
source/ZAZLaTex2SVG.py Normal file
View File

@ -0,0 +1,265 @@
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',)
TITLE = 'ZAZ Latex2SVG'
_ = app.install_locales(__file__)
TEMPLATE = """\documentclass{{article}}
\\usepackage[a5paper, landscape]{{geometry}}
\\usepackage{{xcolor}}
\\usepackage{{amssymb}}
\\usepackage{{amsmath}}
\pagestyle{{empty}}
\\begin{{document}}
\\begingroup
\Huge
\[ {} \]
\endgroup
\end{{document}}
"""
# ~ TEMPLATE = """\documentclass{{standalone}}
# ~ \\usepackage[a5paper, landscape]{{geometry}}
# ~ \\usepackage{{xcolor}}
# ~ \\usepackage{{amssymb}}
# ~ \\usepackage{{amsmath}}
# ~ \\usepackage{{tikz}}
# ~ \pagestyle{{empty}}
# ~ \\begin{{document}}
# ~ \\begin{{tikzpicture}}
# ~ \\node at (0, 0) {{
# ~ \\begin{{equation*}}
# ~ \[ {} \]
# ~ \end{{equation*}}
# ~ }};
# ~ \end{{tikzpicture}}
# ~ \end{{document}}
# ~ """
class Controllers(object):
def __init__(self, dlg):
self.d = dlg
self._path = ''
def cmd_close_action(self, event):
self.d.close()
return
def cmd_preview_action(self, event):
code = self.d.text.value
if not code:
msg = _('Write some code')
app.errorbox(msg)
return
self._path = _latex_to_svg(code)
self.d.image.url = self._path
return
def cmd_insert_action(self, event):
if not self._path:
msg = _('First, generate preview')
app.errorbox(msg)
return
attr = {}
sel = app.selection
if hasattr(sel, 'anchor'):
attr = sel.size
anchor = sel.anchor
anchor.dp.remove(sel)
sel = anchor
image = sel.insert_image(self._path, attr)
image.description = self.d.text.value
self.d.close()
return
def _latex_to_svg(code):
NAME = 'temp'
data = TEMPLATE.format(code)
dt = app.paths.dir_tmp()
path_tex = app._P.join(dt.name, f'{NAME}.tex')
path_pdf = app._P.join(dt.name, f'{NAME}.pdf')
path_svg = app._P.join(app._P.temp_dir, f'{NAME}.svg')
app.paths.save(path_tex, data)
cmd = f'pdflatex --interaction=batchmode -output-directory="{dt.name}" "{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)
if not app.paths.exists(path_svg):
path_svg = ''
dt.cleanup()
return path_svg
class ZAZLaTex2SVG(unohelper.Base, XJobExecutor):
NAME = 'temp'
_msg1 = _('Not found')
_msg2 = _('Found')
def __init__(self, ctx):
self.ctx = ctx
def trigger(self, args=''):
if args == 'app':
self._app()
return
if args == 'dlg':
self._dlg()
return
self._from_selection()
return
def _app(self):
result = self._msg1
if app.paths.exists_app('pdflatex'):
result = self._msg2
msg = f'pdflatex = {result}\n'
result = self._msg1
if app.paths.exists_app('pdfcrop'):
result = self._msg2
msg += f'pdfcrop = {result}\n'
result = self._msg1
if app.paths.exists_app('pdf2svg'):
result = self._msg2
msg += f'pdf2svg = {result}\n\n'
msg += _('Not used, if not found some application.')
app.msgbox(msg)
return
def _from_selection(self):
doc = app.active
sel = doc.selection
data = sel.value
path_svg = _latex_to_svg(data)
sel = sel.offset()
args = {}
if doc.type == 'writer':
args = {'Width': 5000, 'Height': 2000}
image = sel.insert_image(path_svg, args)
image.description = data
return
def _dlg(self):
dlg = self._create_dialog()
sel = app.selection
if hasattr(sel, 'description'):
dlg.text.value = sel.description
dlg.open()
return
def _create_dialog(self):
args = {
'Name': 'dialog',
'Title': TITLE,
'Width': 270,
'Height': 250,
}
dlg = app.create_dialog(args)
dlg.id = ID_EXTENSION
dlg.events = Controllers
args = {
'Type': 'Label',
'Name': 'lbl_code',
'Label': _('Latex code'),
'Width': 70,
'Height': 10,
'X': 10,
'Y': 5,
'VerticalAlign': 1,
}
dlg.add_control(args)
args = {
'Type': 'Text',
'Name': 'text',
'Width': 250,
'Height': 75,
'MultiLine': True,
'VScroll': True,
}
dlg.add_control(args)
args = {
'Type': 'Button',
'Name': 'cmd_preview',
'Label': _('Preview'),
'Width': 70,
'Height': 15,
'ImageURL': 'view.png',
'ImagePosition': 1,
}
dlg.add_control(args)
args = {
'Type': 'Image',
'Name': 'image',
'Width': 250,
'Height': 100,
}
dlg.add_control(args)
args = {
'Type': 'Button',
'Name': 'cmd_insert',
'Label': _('Insert'),
'Width': 70,
'Height': 15,
'ImageURL': 'insert.png',
'ImagePosition': 1,
}
dlg.add_control(args)
args = {
'Type': 'Button',
'Name': 'cmd_close',
'Label': _('Close'),
'Width': 70,
'Height': 15,
'ImageURL': 'close.png',
'ImagePosition': 1,
}
dlg.add_control(args)
dlg.text.move(dlg.lbl_code)
dlg.cmd_preview.move(dlg.text, center=True)
dlg.image.move(dlg.cmd_preview, center=True)
dlg.cmd_insert.move(dlg.image)
dlg.cmd_close.move(dlg.image)
controls = (dlg.cmd_insert, dlg.cmd_close)
dlg.center(controls)
return dlg
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(ZAZLaTex2SVG, ID_EXTENSION, SERVICE)

26
source/description.xml Normal file
View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<description xmlns="http://openoffice.org/extensions/description/2006" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:d="http://openoffice.org/extensions/description/2006">
<identifier value="net.elmau.zaz.latex2svg"/>
<version value="0.1.0"/>
<display-name>
<name lang="en">ZAZLaTex2SVG</name>
<name lang="es">ZAZLaTex2SVG</name>
</display-name>
<extension-description>
<src lang="en" xlink:href="description/desc_en.txt"/>
<src lang="es" xlink:href="description/desc_es.txt"/>
</extension-description>
<icon>
<default xlink:href="images/zazlatex2svg.png"/>
</icon>
<publisher>
<name xlink:href="https://gitlab.com/mauriciobaeza" lang="en">El Mau</name>
<name xlink:href="https://gitlab.com/mauriciobaeza" lang="es">El Mau</name>
</publisher>
<registration>
<simple-license accept-by="user" suppress-on-update="true">
<license-text xlink:href="registration/license_en.txt" lang="en"/>
<license-text xlink:href="registration/license_es.txt" lang="es"/>
</simple-license>
</registration>
</description>

View File

@ -0,0 +1 @@
Generate equations in SVG from LaTex

View File

@ -0,0 +1 @@
Genera ecuaciones en SVG desde LaTex

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

View File

@ -0,0 +1,30 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: 2020-10-30 12:59-0600\n"
"PO-Revision-Date: 2020-10-30 13:03-0600\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: pygettext.py 1.5\n"
"X-Generator: Poedit 2.4.1\n"
"Last-Translator: \n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"Language: en\n"
#: source/ZAZLaTex2SVG.py:15
msgid "Not found"
msgstr ""
#: source/ZAZLaTex2SVG.py:16
msgid "Found"
msgstr ""
#: source/ZAZLaTex2SVG.py:45
msgid "Not used, if not found some application."
msgstr ""

View File

@ -0,0 +1,56 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: 2020-11-03 21:36-0600\n"
"PO-Revision-Date: 2020-11-03 21:37-0600\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: es\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: pygettext.py 1.5\n"
"X-Generator: Poedit 2.4.1\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: source/ZAZLaTex2SVG.py:66
msgid "Write some code"
msgstr "Escribe algo de código"
#: source/ZAZLaTex2SVG.py:76
msgid "First, generate preview"
msgstr "Primero, genera la vista previa"
#: source/ZAZLaTex2SVG.py:116
msgid "Not found"
msgstr "No encontrado"
#: source/ZAZLaTex2SVG.py:117
msgid "Found"
msgstr "Encontrado"
#: source/ZAZLaTex2SVG.py:150
#, fuzzy
#| msgid "Not used, if not found some application"
msgid "Not used, if not found some application."
msgstr "No use, si no se encuentra alguna aplicación."
#: source/ZAZLaTex2SVG.py:177
msgid "Latex code"
msgstr "Código LaTex"
#: source/ZAZLaTex2SVG.py:199
msgid "Preview"
msgstr "Vista Previa"
#: source/ZAZLaTex2SVG.py:218
msgid "Insert"
msgstr "Insertar"
#: source/ZAZLaTex2SVG.py:228
msgid "Close"
msgstr "Cerrar"

View File

@ -0,0 +1,30 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: 2020-10-30 12:34-0600\n"
"PO-Revision-Date: 2020-10-30 12:58-0600\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: pygettext.py 1.5\n"
"X-Generator: Poedit 2.4.1\n"
"Last-Translator: \n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"Language: es\n"
#: source/ZAZLaTex2SVG.py:15
msgid "Not found"
msgstr "No encontrado"
#: source/ZAZLaTex2SVG.py:16
msgid "Found"
msgstr "Encontrado"
#: source/ZAZLaTex2SVG.py:45
msgid "Not used, if not found some application"
msgstr "No use, si no se encuentra alguna aplicación."

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,14 @@
This file is part of ZAZLaTex2SVG.
ZAZLaTex2SVG is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ZAZLaTex2SVG is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ZAZLaTex2SVG. If not, see <https://www.gnu.org/licenses/>.

View File

@ -0,0 +1,14 @@
This file is part of ZAZLaTex2SVG.
ZAZLaTex2SVG is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ZAZLaTex2SVG is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ZAZLaTex2SVG. If not, see <https://www.gnu.org/licenses/>.

785
zaz.py Executable file
View File

@ -0,0 +1,785 @@
#!/usr/bin/env python3
# == Rapid Develop Macros in LibreOffice ==
# ~ This file is part of ZAZ.
# ~ ZAZ is free software: you can redistribute it and/or modify
# ~ it under the terms of the GNU General Public License as published by
# ~ the Free Software Foundation, either version 3 of the License, or
# ~ (at your option) any later version.
# ~ ZAZ is distributed in the hope that it will be useful,
# ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
# ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# ~ GNU General Public License for more details.
# ~ You should have received a copy of the GNU General Public License
# ~ along with ZAZ. If not, see <https://www.gnu.org/licenses/>.
import argparse
import os
import py_compile
import re
import sys
import zipfile
from datetime import datetime
from pathlib import Path
from shutil import copyfile
from subprocess import call
from xml.etree import ElementTree as ET
from xml.dom.minidom import parseString
from conf import (
DATA,
DIRS,
DOMAIN,
EXTENSION,
FILES,
INFO,
PATHS,
TYPE_EXTENSION,
USE_LOCALES,
log)
class LiboXML(object):
CONTEXT = {
'calc': 'com.sun.star.sheet.SpreadsheetDocument',
'writer': 'com.sun.star.text.TextDocument',
'impress': 'com.sun.star.presentation.PresentationDocument',
'draw': 'com.sun.star.drawing.DrawingDocument',
'base': 'com.sun.star.sdb.OfficeDatabaseDocument',
'math': 'com.sun.star.formula.FormulaProperties',
'basic': 'com.sun.star.script.BasicIDE',
}
TYPES = {
'py': 'application/vnd.sun.star.uno-component;type=Python',
'pyc': 'application/binary',
'zip': 'application/binary',
'xcu': 'application/vnd.sun.star.configuration-data',
'rdb': 'application/vnd.sun.star.uno-typelibrary;type=RDB',
'xcs': 'application/vnd.sun.star.configuration-schema',
'help': 'application/vnd.sun.star.help',
'component': 'application/vnd.sun.star.uno-components',
}
NS_MANIFEST = {
'manifest_version': '1.2',
'manifest': 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0',
'xmlns:loext': 'urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0',
}
NS_DESCRIPTION = {
'xmlns': 'http://openoffice.org/extensions/description/2006',
'xmlns:xlink': 'http://www.w3.org/1999/xlink',
'xmlns:d': 'http://openoffice.org/extensions/description/2006',
}
NS_ADDONS = {
'xmlns:xs': 'http://www.w3.org/2001/XMLSchema',
'xmlns:oor': 'http://openoffice.org/2001/registry',
}
NS_UPDATE = {
'xmlns': 'http://openoffice.org/extensions/update/2006',
'xmlns:d': 'http://openoffice.org/extensions/description/2006',
'xmlns:xlink': 'http://www.w3.org/1999/xlink',
}
def __init__(self):
self._manifest = None
self._paths = []
self._path_images = ''
self._toolbars = []
def _save_path(self, attr):
self._paths.append(attr['{{{}}}full-path'.format(self.NS_MANIFEST['manifest'])])
return
def _clean(self, name, nodes):
has_words = re.compile('\\w')
if not re.search(has_words, str(nodes.tail)):
nodes.tail = ''
if not re.search(has_words, str(nodes.text)):
nodes.text = ''
for node in nodes:
if name == 'manifest':
self._save_path(node.attrib)
if not re.search(has_words, str(node.tail)):
node.tail = ''
if not re.search(has_words, str(node.text)):
node.text = ''
return
def new_manifest(self, data):
attr = {
'manifest:version': self.NS_MANIFEST['manifest_version'],
'xmlns:manifest': self.NS_MANIFEST['manifest'],
'xmlns:loext': self.NS_MANIFEST['xmlns:loext'],
}
self._manifest = ET.Element('manifest:manifest', attr)
return self.add_data_manifest(data)
def parse_manifest(self, data):
ET.register_namespace('manifest', self.NS_MANIFEST['manifest'])
self._manifest = ET.fromstring(data)
attr = {'xmlns:loext': self.NS_MANIFEST['xmlns:loext']}
self._manifest.attrib.update(**attr)
self._clean('manifest', self._manifest)
return
def add_data_manifest(self, data):
node_name = 'manifest:file-entry'
attr = {
'manifest:full-path': '',
'manifest:media-type': '',
}
for path in data:
if path in self._paths:
continue
ext = path.split('.')[-1]
attr['manifest:full-path'] = path
attr['manifest:media-type'] = self.TYPES.get(ext, '')
ET.SubElement(self._manifest, node_name, attr)
return self._get_xml(self._manifest)
def new_description(self, data):
doc = ET.Element('description', self.NS_DESCRIPTION)
key = 'identifier'
ET.SubElement(doc, key, data[key])
key = 'version'
ET.SubElement(doc, key, data[key])
key = 'display-name'
node = ET.SubElement(doc, key)
for k, v in data[key].items():
sn = ET.SubElement(node, 'name', {'lang': k})
sn.text = v
node = ET.SubElement(doc, 'extension-description')
for k in data[key].keys():
attr = {
'lang': k,
'xlink:href': f'description/desc_{k}.txt',
}
ET.SubElement(node, 'src', attr)
key = 'icon'
node = ET.SubElement(doc, key)
attr = {'xlink:href': f"images/{data[key]}"}
ET.SubElement(node, 'default', attr)
key = 'publisher'
node = ET.SubElement(doc, key)
for k, v in data[key].items():
attr = {
'xlink:href': v['link'],
'lang': k,
}
sn = ET.SubElement(node, 'name', attr)
sn.text = v['text']
key = 'display-name'
node = ET.SubElement(doc, 'registration')
attr = {
'accept-by': 'user',
'suppress-on-update': 'true',
}
node = ET.SubElement(node, 'simple-license', attr)
for k in data[key].keys():
attr = {
'xlink:href': f"{DIRS['registration']}/license_{k}.txt",
'lang': k
}
ET.SubElement(node, 'license-text', attr)
if data['update']:
node = ET.SubElement(doc, 'update-information')
ET.SubElement(node, 'src', {'xlink:href': data['update']})
return self._get_xml(doc)
def _get_context(self, args):
if not args:
return ''
context = ','.join([self.CONTEXT[v] for v in args.split(',')])
return context
def _add_node_value(self, node, name, value='_self'):
attr = {'oor:name': name, 'oor:type': 'xs:string'}
sn = ET.SubElement(node, 'prop', attr)
sn = ET.SubElement(sn, 'value')
sn.text = value
return
def _add_menu(self, id_extension, node, index, menu, in_menu_bar=True):
if in_menu_bar:
attr = {
'oor:name': index,
'oor:op': 'replace',
}
subnode = ET.SubElement(node, 'node', attr)
else:
subnode = node
attr = {'oor:name': 'Title', 'oor:type': 'xs:string'}
sn1 = ET.SubElement(subnode, 'prop', attr)
for k, v in menu['title'].items():
sn2 = ET.SubElement(sn1, 'value', {'xml:lang': k})
sn2.text = v
value = self._get_context(menu['context'])
self._add_node_value(subnode, 'Context', value)
if 'submenu' in menu:
sn = ET.SubElement(subnode, 'node', {'oor:name': 'Submenu'})
for i, m in enumerate(menu['submenu']):
self._add_menu(id_extension, sn, f'{index}.s{i}', m)
if m.get('toolbar', False):
self._toolbars.append(m)
return
value = f"service:{id_extension}?{menu['argument']}"
self._add_node_value(subnode, 'URL', value)
self._add_node_value(subnode, 'Target')
value = f"%origin%/{self._path_images}/{menu['icon']}"
self._add_node_value(subnode, 'ImageIdentifier', value)
return
def new_addons(self, id_extension, data):
in_menu_bar = data['parent'] == 'OfficeMenuBar'
self._path_images = data['images']
attr = {
'oor:name': 'Addons',
'oor:package': 'org.openoffice.Office',
}
attr.update(self.NS_ADDONS)
doc = ET.Element('oor:component-data', attr)
parent = ET.SubElement(doc, 'node', {'oor:name': 'AddonUI'})
node = ET.SubElement(parent, 'node', {'oor:name': data['parent']})
op = 'fuse'
if in_menu_bar:
op = 'replace'
attr = {'oor:name': id_extension, 'oor:op': op}
node = ET.SubElement(node, 'node', attr)
if in_menu_bar:
attr = {'oor:name': 'Title', 'oor:type': 'xs:string'}
subnode = ET.SubElement(node, 'prop', attr)
for k, v in data['main'].items():
sn = ET.SubElement(subnode, 'value', {'xml:lang': k})
sn.text = v
self._add_node_value(node, 'Target')
node = ET.SubElement(node, 'node', {'oor:name': 'Submenu'})
for i, menu in enumerate(data['menus']):
self._add_menu(id_extension, node, f'm{i}', menu, in_menu_bar)
if menu.get('toolbar', False):
self._toolbars.append(menu)
if self._toolbars:
attr = {'oor:name': 'OfficeToolBar'}
toolbar = ET.SubElement(parent, 'node', attr)
attr = {'oor:name': id_extension, 'oor:op': 'replace'}
toolbar = ET.SubElement(toolbar, 'node', attr)
for t, menu in enumerate(self._toolbars):
self._add_menu(id_extension, toolbar, f't{t}', menu)
return self._get_xml(doc)
def _add_shortcut(self, node, key, id_extension, arg):
attr = {'oor:name': key, 'oor:op': 'fuse'}
subnode = ET.SubElement(node, 'node', attr)
subnode = ET.SubElement(subnode, 'prop', {'oor:name': 'Command'})
subnode = ET.SubElement(subnode, 'value', {'xml:lang': 'en-US'})
subnode.text = f"service:{id_extension}?{arg}"
return
def _get_acceleartors(self, menu):
if 'submenu' in menu:
for m in menu['submenu']:
return self._get_acceleartors(m)
if not menu.get('shortcut', ''):
return ''
return menu
def new_accelerators(self, id_extension, menus):
attr = {
'oor:name': 'Accelerators',
'oor:package': 'org.openoffice.Office',
}
attr.update(self.NS_ADDONS)
doc = ET.Element('oor:component-data', attr)
parent = ET.SubElement(doc, 'node', {'oor:name': 'PrimaryKeys'})
data = []
for m in menus:
info = self._get_acceleartors(m)
if info:
data.append(info)
node_global = None
node_modules = None
for m in data:
if m['context']:
if node_modules is None:
node_modules = ET.SubElement(
parent, 'node', {'oor:name': 'Modules'})
for app in m['context'].split(','):
node = ET.SubElement(
node_modules, 'node', {'oor:name': self.CONTEXT[app]})
self._add_shortcut(
node, m['shortcut'], id_extension, m['argument'])
else:
if node_global is None:
node_global = ET.SubElement(
parent, 'node', {'oor:name': 'Global'})
self._add_shortcut(
node_global, m['shortcut'], id_extension, m['argument'])
return self._get_xml(doc)
def new_update(self, extension, url_oxt):
doc = ET.Element('description', self.NS_UPDATE)
ET.SubElement(doc, 'identifier', {'value': extension['id']})
ET.SubElement(doc, 'version', {'value': extension['version']})
node = ET.SubElement(doc, 'update-download')
ET.SubElement(node, 'src', {'xlink:href': url_oxt})
node = ET.SubElement(doc, 'release-notes')
return self._get_xml(doc)
def _get_xml(self, doc):
xml = parseString(ET.tostring(doc, encoding='utf-8'))
return xml.toprettyxml(indent=' ', encoding='utf-8').decode('utf-8')
def _exists(path):
return