[latex] Fix plugin, cleanup code
This commit is contained in:
53
latex/config_dialog.py
Normal file
53
latex/config_dialog.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
# This file is part of Gajim.
|
||||||
|
#
|
||||||
|
# Gajim 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; version 3 only.
|
||||||
|
#
|
||||||
|
# Gajim 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 Gajim. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
from gi.repository import GObject
|
||||||
|
from gi.repository import Gtk
|
||||||
|
|
||||||
|
from gajim.gtk.settings import SettingsDialog
|
||||||
|
from gajim.gtk.settings import SpinSetting
|
||||||
|
from gajim.gtk.const import Setting
|
||||||
|
from gajim.gtk.const import SettingType
|
||||||
|
|
||||||
|
from gajim.plugins.plugins_i18n import _
|
||||||
|
|
||||||
|
|
||||||
|
class LatexPluginConfiguration(SettingsDialog):
|
||||||
|
def __init__(self, plugin, parent):
|
||||||
|
self.plugin = plugin
|
||||||
|
|
||||||
|
settings = [
|
||||||
|
Setting('LatexDPISpinSetting', _('PNG DPI'),
|
||||||
|
SettingType.VALUE, int(self.plugin.config['png_dpi']),
|
||||||
|
callback=self.on_setting, data='png_dpi',
|
||||||
|
desc=_('Scale of the rendered PNG file'),
|
||||||
|
props={'range_': (72, 300)}),
|
||||||
|
]
|
||||||
|
|
||||||
|
SettingsDialog.__init__(self, parent, _('Latex Configuration'),
|
||||||
|
Gtk.DialogFlags.MODAL, settings, None,
|
||||||
|
extend=[('LatexDPISpinSetting',
|
||||||
|
DPISpinSetting)])
|
||||||
|
|
||||||
|
def on_setting(self, value, data):
|
||||||
|
self.plugin.config[data] = value
|
||||||
|
|
||||||
|
class DPISpinSetting(SpinSetting):
|
||||||
|
|
||||||
|
__gproperties__ = {
|
||||||
|
"setting-value": (int, 'Size', '', 72, 300, 108,
|
||||||
|
GObject.ParamFlags.READWRITE), }
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
SpinSetting.__init__(self, *args, **kwargs)
|
||||||
@@ -1,89 +0,0 @@
|
|||||||
<?xml version="1.0"?>
|
|
||||||
<interface>
|
|
||||||
<requires lib="gtk+" version="2.16"/>
|
|
||||||
<!-- interface-naming-policy toplevel-contextual -->
|
|
||||||
<object class="GtkTextBuffer" id="textbuffer1">
|
|
||||||
<property name="text">Plug-in description should be displayed here. This text will be erased during PluginsWindow initialization.</property>
|
|
||||||
</object>
|
|
||||||
<object class="GtkWindow" id="window1">
|
|
||||||
<child>
|
|
||||||
<object class="GtkVBox" id="vbox1">
|
|
||||||
<property name="visible">True</property>
|
|
||||||
<property name="orientation">vertical</property>
|
|
||||||
<property name="spacing">6</property>
|
|
||||||
<child>
|
|
||||||
<object class="GtkHBox" id="hbox111">
|
|
||||||
<property name="visible">True</property>
|
|
||||||
<property name="border_width">3</property>
|
|
||||||
<property name="spacing">6</property>
|
|
||||||
<child>
|
|
||||||
<object class="GtkLabel" id="label1">
|
|
||||||
<property name="visible">True</property>
|
|
||||||
<property name="xalign">0</property>
|
|
||||||
<property name="label" translatable="yes">PNG dpi:</property>
|
|
||||||
</object>
|
|
||||||
<packing>
|
|
||||||
<property name="expand">False</property>
|
|
||||||
<property name="fill">False</property>
|
|
||||||
<property name="position">0</property>
|
|
||||||
</packing>
|
|
||||||
</child>
|
|
||||||
<child>
|
|
||||||
<object class="GtkEntry" id="png_dpi_label">
|
|
||||||
<property name="visible">True</property>
|
|
||||||
<property name="can_focus">True</property>
|
|
||||||
<property name="invisible_char">●</property>
|
|
||||||
<signal name="changed" handler="on_png_dpi_label_changed"/>
|
|
||||||
</object>
|
|
||||||
<packing>
|
|
||||||
<property name="position">1</property>
|
|
||||||
</packing>
|
|
||||||
</child>
|
|
||||||
</object>
|
|
||||||
<packing>
|
|
||||||
<property name="expand">False</property>
|
|
||||||
<property name="position">0</property>
|
|
||||||
</packing>
|
|
||||||
</child>
|
|
||||||
<child>
|
|
||||||
<object class="GtkHBox" id="hbox1">
|
|
||||||
<property name="visible">True</property>
|
|
||||||
<child>
|
|
||||||
<placeholder/>
|
|
||||||
</child>
|
|
||||||
<child>
|
|
||||||
<object class="GtkButton" id="test_button">
|
|
||||||
<property name="label" translatable="yes">Test Latex Configuration</property>
|
|
||||||
<property name="visible">True</property>
|
|
||||||
<property name="can_focus">True</property>
|
|
||||||
<property name="receives_default">True</property>
|
|
||||||
<signal name="clicked" handler="on_test_button_clicked"/>
|
|
||||||
</object>
|
|
||||||
<packing>
|
|
||||||
<property name="expand">False</property>
|
|
||||||
<property name="position">1</property>
|
|
||||||
</packing>
|
|
||||||
</child>
|
|
||||||
<child>
|
|
||||||
<placeholder/>
|
|
||||||
</child>
|
|
||||||
</object>
|
|
||||||
<packing>
|
|
||||||
<property name="expand">False</property>
|
|
||||||
<property name="position">1</property>
|
|
||||||
</packing>
|
|
||||||
</child>
|
|
||||||
<child>
|
|
||||||
<object class="GtkLabel" id="result_label">
|
|
||||||
<property name="visible">True</property>
|
|
||||||
<property name="label" translatable="yes">Result:
|
|
||||||
</property>
|
|
||||||
</object>
|
|
||||||
<packing>
|
|
||||||
<property name="position">2</property>
|
|
||||||
</packing>
|
|
||||||
</child>
|
|
||||||
</object>
|
|
||||||
</child>
|
|
||||||
</object>
|
|
||||||
</interface>
|
|
||||||
431
latex/latex.py
431
latex/latex.py
@@ -1,303 +1,83 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# Copyright (C) 2010-2011 Yves Fischer <yvesf AT xapek.org>
|
||||||
|
# Copyright (C) 2011 Yann Leboulanger <asterix AT lagaule.org>
|
||||||
#
|
#
|
||||||
## plugins/latex/latex.py
|
# This file is part of Gajim.
|
||||||
##
|
#
|
||||||
## Copyright (C) 2010-2011 Yves Fischer <yvesf AT xapek.org>
|
# Gajim is free software; you can redistribute it and/or modify
|
||||||
## Copyright (C) 2011 Yann Leboulanger <asterix AT lagaule.org>
|
# it under the terms of the GNU General Public License as published
|
||||||
##
|
# by the Free Software Foundation; version 3 only.
|
||||||
## This file is part of Gajim.
|
#
|
||||||
##
|
# Gajim is distributed in the hope that it will be useful,
|
||||||
## Gajim is free software; you can redistribute it and/or modify
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
## it under the terms of the GNU General Public License as published
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
## by the Free Software Foundation; version 3 only.
|
# GNU General Public License for more details.
|
||||||
##
|
#
|
||||||
## Gajim is distributed in the hope that it will be useful,
|
# You should have received a copy of the GNU General Public License
|
||||||
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
||||||
## 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 Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
##
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import random
|
import logging
|
||||||
from tempfile import mkstemp, mkdtemp
|
from functools import partial
|
||||||
from threading import Thread
|
|
||||||
from subprocess import Popen, PIPE
|
|
||||||
|
|
||||||
from gi.repository import Gtk
|
from gi.repository import Gtk
|
||||||
from gi.repository import Gdk
|
from gi.repository import Gdk
|
||||||
from gi.repository import GdkPixbuf
|
from gi.repository import GLib
|
||||||
from gi.repository import Pango
|
|
||||||
from gi.repository import GObject
|
from gi.repository import Pango
|
||||||
|
|
||||||
from gajim.common import app
|
|
||||||
from gajim.plugins import GajimPlugin
|
from gajim.plugins import GajimPlugin
|
||||||
from gajim.plugins.helpers import log, log_calls
|
|
||||||
from gajim.plugins.gui import GajimPluginConfigDialog
|
|
||||||
from gajim.plugins.plugins_i18n import _
|
from gajim.plugins.plugins_i18n import _
|
||||||
|
|
||||||
def latex_template(code):
|
from latex.latex_renderer import LatexRenderer
|
||||||
return '''\\documentclass[12pt]{article}
|
from latex.config_dialog import LatexPluginConfiguration
|
||||||
\\usepackage[dvips]{graphicx}
|
from latex.util import try_run
|
||||||
\\usepackage{amsmath}
|
|
||||||
\\usepackage{amssymb}
|
|
||||||
\\pagestyle{empty}
|
|
||||||
\\begin{document}
|
|
||||||
\\begin{large}
|
|
||||||
\\begin{gather*}
|
|
||||||
%s
|
|
||||||
\\end{gather*}
|
|
||||||
\\end{large}
|
|
||||||
\\end{document}''' % (code)
|
|
||||||
|
|
||||||
def write_latex(filename, str_):
|
log = logging.getLogger('gajim.p.latex')
|
||||||
texstr = latex_template(str_)
|
|
||||||
|
|
||||||
file_ = open(filename, "w+")
|
|
||||||
file_.write(texstr)
|
|
||||||
file_.flush()
|
|
||||||
file_.close()
|
|
||||||
|
|
||||||
def popen_nt_friendly(command, directory):
|
|
||||||
if os.name == 'nt':
|
|
||||||
# CREATE_NO_WINDOW
|
|
||||||
return Popen(command, creationflags=0x08000000, cwd=directory,
|
|
||||||
stdout=PIPE)
|
|
||||||
else:
|
|
||||||
return Popen(command, cwd=directory, stdout=PIPE)
|
|
||||||
|
|
||||||
def try_run(argv, directory):
|
|
||||||
try:
|
|
||||||
p = popen_nt_friendly(argv, directory)
|
|
||||||
out = p.communicate()[0]
|
|
||||||
log.info(out)
|
|
||||||
return p.wait()
|
|
||||||
except Exception as e:
|
|
||||||
return _('Error executing "%(command)s": %(error)s') % {
|
|
||||||
'command': " ".join(argv),
|
|
||||||
'error': str(e)}
|
|
||||||
|
|
||||||
BLACKLIST = ['\def', '\\let', '\\futurelet', '\\newcommand', '\\renewcomment',
|
|
||||||
'\\else', '\\fi', '\\write', '\\input', '\\include', '\\chardef',
|
|
||||||
'\\catcode', '\\makeatletter', '\\noexpand', '\\toksdef', '\\every',
|
|
||||||
'\\errhelp', '\\errorstopmode', '\\scrollmode', '\\nonstopmode',
|
|
||||||
'\\batchmode', '\\read', '\\csname', '\\newhelp', '\\relax', '\\afterground',
|
|
||||||
'\\afterassignment', '\\expandafter', '\\noexpand', '\\special', '\\command',
|
|
||||||
'\\loop', '\\repeat', '\\toks', '\\output', '\\line', '\\mathcode', '\\name',
|
|
||||||
'\\item', '\\section', '\\mbox', '\\DeclareRobustCommand', '\\[', '\\]'
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
class LatexRenderer(Thread):
|
|
||||||
def __init__(self, iter_start, iter_end, buffer_, widget, png_dpi):
|
|
||||||
Thread.__init__(self)
|
|
||||||
|
|
||||||
self.code = iter_start.get_text(iter_end)
|
|
||||||
self.mark_name = 'LatexRendererMark%s' % str(random.randint(0,1000))
|
|
||||||
self.mark = buffer_.create_mark(self.mark_name, iter_start, True)
|
|
||||||
|
|
||||||
self.buffer_ = buffer_
|
|
||||||
self.widget = widget
|
|
||||||
self.png_dpi = png_dpi
|
|
||||||
|
|
||||||
# delete code and show message 'processing'
|
|
||||||
self.buffer_.delete(iter_start, iter_end)
|
|
||||||
# iter_start.forward_char()
|
|
||||||
self.buffer_.insert(iter_start, _('Processing LaTeX'))
|
|
||||||
|
|
||||||
self.start() # start background processing
|
|
||||||
|
|
||||||
def run(self):
|
|
||||||
try:
|
|
||||||
if self.check_code():
|
|
||||||
self.show_image()
|
|
||||||
else:
|
|
||||||
self.show_error(_('There are bad commands!'))
|
|
||||||
except Exception:
|
|
||||||
self.show_error(_('Error processing LaTeX'))
|
|
||||||
finally:
|
|
||||||
self.buffer_.delete_mark(self.mark)
|
|
||||||
|
|
||||||
def show_error(self, message):
|
|
||||||
"""
|
|
||||||
String -> TextBuffer
|
|
||||||
"""
|
|
||||||
Gdk.threads_enter()
|
|
||||||
iter_mark = self.buffer_.get_iter_at_mark(self.mark)
|
|
||||||
iter_end = iter_mark.copy().forward_search(_('Processing LaTeX'),
|
|
||||||
Gtk.TextSearchFlags.TEXT_ONLY, None)[1]
|
|
||||||
self.buffer_.delete(iter_mark, iter_end)
|
|
||||||
|
|
||||||
pixbuf = self.widget.render_icon(Gtk.STOCK_STOP, Gtk.IconSize.BUTTON)
|
|
||||||
self.buffer_.insert_pixbuf(iter_end, pixbuf)
|
|
||||||
self.buffer_.insert(iter_end, message)
|
|
||||||
Gdk.threads_leave()
|
|
||||||
|
|
||||||
@log_calls('LatexRenderer')
|
|
||||||
def show_image(self):
|
|
||||||
"""
|
|
||||||
Latex -> PNG -> TextBuffer
|
|
||||||
"""
|
|
||||||
|
|
||||||
def fg_str(fmt):
|
|
||||||
try:
|
|
||||||
return [{'hex' : '+level-colors', 'tex' : '-fg'}[fmt],
|
|
||||||
app.interface.get_fg_color(fmt)]
|
|
||||||
except KeyError:
|
|
||||||
# interface may not be available when we test latex at startup
|
|
||||||
return []
|
|
||||||
except AttributeError:
|
|
||||||
# interface may not be available when we test latext at startup
|
|
||||||
return {'hex': ['+level-colors', '0x000000'],
|
|
||||||
'tex': ['-fg', 'rgb 0.0 0.0 0.0']}[fmt]
|
|
||||||
|
|
||||||
try:
|
|
||||||
tmpdir = mkdtemp(prefix='gajim_tex')
|
|
||||||
tmpfd, tmppng = mkstemp(prefix='gajim_tex', suffix='.png')
|
|
||||||
os.close(tmpfd)
|
|
||||||
except Exception:
|
|
||||||
msg = 'Could not create temporary files for Latex plugin'
|
|
||||||
log.debug(msg)
|
|
||||||
self.show_error(_('latex error: %(error)s\n===ORIGINAL CODE====\n'
|
|
||||||
'%(code)s') % {'error': msg, 'code': self.code[2:len(self.code)-2]})
|
|
||||||
return False
|
|
||||||
|
|
||||||
tmpfile = os.path.join(tmpdir, 'gajim_tex')
|
|
||||||
|
|
||||||
# build latex string
|
|
||||||
write_latex(tmpfile + '.tex', self.code[2:len(self.code)-2])
|
|
||||||
|
|
||||||
# convert TeX to dvi
|
|
||||||
exitcode = try_run(['latex', '--interaction=nonstopmode',
|
|
||||||
tmpfile + '.tex'], tmpdir)
|
|
||||||
|
|
||||||
if exitcode == 0:
|
|
||||||
# convert dvi to png
|
|
||||||
log.debug('DVI OK')
|
|
||||||
exitcode = try_run(['dvipng', '-bg', 'Transparent'] + fg_str('tex')\
|
|
||||||
+ ['-T', 'tight', '-D', self.png_dpi, tmpfile + '.dvi', '-o',
|
|
||||||
tmpfile + '.png'], tmpdir)
|
|
||||||
|
|
||||||
if exitcode:
|
|
||||||
# dvipng failed, try convert
|
|
||||||
log.debug('dvipng failed, try convert')
|
|
||||||
exitcode = try_run(['convert'] + fg_str('hex') + ['-trim',
|
|
||||||
'-density', self.png_dpi, tmpfile + '.dvi',
|
|
||||||
tmpfile + '.png'], tmpdir)
|
|
||||||
|
|
||||||
# remove temp files created by us and TeX
|
|
||||||
extensions = ['.tex', '.log', '.aux', '.dvi']
|
|
||||||
for ext in extensions:
|
|
||||||
try:
|
|
||||||
os.remove(tmpfile + ext)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if exitcode == 0:
|
|
||||||
log.debug('PNG OK')
|
|
||||||
os.rename(tmpfile + '.png', tmppng)
|
|
||||||
else:
|
|
||||||
log.debug('PNG FAILED')
|
|
||||||
os.remove(tmppng)
|
|
||||||
os.rmdir(tmpdir)
|
|
||||||
self.show_error(_('Convertion to image failed\n===ORIGINAL CODE===='
|
|
||||||
'\n%s') % self.code[2:len(self.code)-2])
|
|
||||||
return False
|
|
||||||
|
|
||||||
log.debug('Loading PNG %s' % tmppng)
|
|
||||||
try:
|
|
||||||
Gdk.threads_enter()
|
|
||||||
pixbuf = GdkPixbuf.Pixbuf.new_from_file(tmppng)
|
|
||||||
log.debug('png loaded')
|
|
||||||
iter_mark = self.buffer_.get_iter_at_mark(self.mark)
|
|
||||||
iter_end = iter_mark.copy().forward_search('Processing LaTeX',
|
|
||||||
Gtk.TextSearchFlags.TEXT_ONLY, None)[1]
|
|
||||||
log.debug('Delete old Text')
|
|
||||||
self.buffer_.delete(iter_mark, iter_end)
|
|
||||||
log.debug('Insert pixbuf')
|
|
||||||
self.buffer_.insert_pixbuf(iter_end, pixbuf)
|
|
||||||
except GObject.GError:
|
|
||||||
self.show_error(_('Cannot open %s for reading') % tmppng)
|
|
||||||
log.debug('Cant open %s for reading' % tmppng)
|
|
||||||
finally:
|
|
||||||
Gdk.threads_leave()
|
|
||||||
os.remove(tmppng)
|
|
||||||
|
|
||||||
def check_code(self):
|
|
||||||
for bad_cmd in BLACKLIST:
|
|
||||||
if self.code.find(bad_cmd) != -1:
|
|
||||||
# Found bad command
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
class LatexPluginConfiguration(GajimPluginConfigDialog):
|
|
||||||
def init(self):
|
|
||||||
self.GTK_BUILDER_FILE_PATH = self.plugin.local_file_path(
|
|
||||||
'config_dialog.ui')
|
|
||||||
self.xml = Gtk.Builder()
|
|
||||||
self.xml.set_translation_domain('gajim_plugins')
|
|
||||||
self.xml.add_objects_from_file(self.GTK_BUILDER_FILE_PATH, ['vbox1'])
|
|
||||||
hbox = self.xml.get_object('vbox1')
|
|
||||||
self.get_child().pack_start(hbox, False, False, 0)
|
|
||||||
self.result_label = self.xml.get_object('result_label')
|
|
||||||
|
|
||||||
self.xml.connect_signals(self)
|
|
||||||
|
|
||||||
def on_run(self):
|
|
||||||
widget = self.xml.get_object('png_dpi_label')
|
|
||||||
widget.set_text(str(self.plugin.config['png_dpi']))
|
|
||||||
|
|
||||||
def show_result(self, msg):
|
|
||||||
self.result_label.set_text(self.result_label.get_text() + '\n' + msg)
|
|
||||||
|
|
||||||
def on_test_button_clicked(self,widget):
|
|
||||||
"""
|
|
||||||
performs very simple checks (check if executable is in PATH)
|
|
||||||
"""
|
|
||||||
self.show_result(_('Test Latex Binary'))
|
|
||||||
exitcode = try_run(['latex', '-version'], None)
|
|
||||||
if exitcode != 0:
|
|
||||||
self.show_result(_(' No LaTeX binary found in PATH'))
|
|
||||||
else:
|
|
||||||
self.show_result(_(' OK'))
|
|
||||||
|
|
||||||
self.show_result(_('Test dvipng'))
|
|
||||||
exitcode = try_run(['dvipng', '--version'], None)
|
|
||||||
if exitcode != 0:
|
|
||||||
self.show_result(_(' No dvipng binary found in PATH'))
|
|
||||||
else:
|
|
||||||
self.show_result(_(' OK'))
|
|
||||||
|
|
||||||
self.show_result(_('Test ImageMagick'))
|
|
||||||
exitcode = try_run(['convert', '-version'], None)
|
|
||||||
if exitcode != 0:
|
|
||||||
self.show_result(_(' No convert binary found in PATH'))
|
|
||||||
else:
|
|
||||||
self.show_result(_(' OK'))
|
|
||||||
|
|
||||||
def on_png_dpi_label_changed(self, label):
|
|
||||||
self.plugin.config['png_dpi'] = label.get_text()
|
|
||||||
|
|
||||||
class LatexPlugin(GajimPlugin):
|
class LatexPlugin(GajimPlugin):
|
||||||
def init(self):
|
def init(self):
|
||||||
self.description = _('Invoke Latex to render $$foobar$$ sourrounded ' \
|
self.description = _(
|
||||||
'Latex equations. Needs latex and dvipng or ImageMagick.')
|
'Render LaTeX markup for $$foobar$$ sourrounded LaTeX equations.')
|
||||||
self.config_dialog = LatexPluginConfiguration(self)
|
self.config_dialog = partial(LatexPluginConfiguration, self)
|
||||||
self.config_default_values = {'png_dpi': ('108', '')}
|
|
||||||
|
|
||||||
self.gui_extension_points = {
|
self.config_default_values = {
|
||||||
'chat_control_base': (self.connect_with_chat_control_base,
|
'png_dpi': ('108', '')
|
||||||
self.disconnect_from_chat_control_base)
|
}
|
||||||
|
self.gui_extension_points = {
|
||||||
|
'chat_control_base': (
|
||||||
|
self._connect_chat_control_base,
|
||||||
|
self._disconnect_chat_control_base)
|
||||||
}
|
}
|
||||||
self.test_activatable()
|
|
||||||
self.timeout_id = None
|
|
||||||
self.last_eol_offset = -1
|
|
||||||
|
|
||||||
def test_activatable(self):
|
self._test_activatable()
|
||||||
|
self._timeout_id = None
|
||||||
|
self._last_eol_offset = -1
|
||||||
|
|
||||||
|
def _connect_chat_control_base(self, chat_control):
|
||||||
|
d = {}
|
||||||
|
tv = chat_control.conv_textview.tv
|
||||||
|
tb = tv.get_buffer()
|
||||||
|
|
||||||
|
self._latex_tag = Gtk.TextTag.new('latex')
|
||||||
|
self._latex_tag.set_property('foreground', 'blue')
|
||||||
|
self._latex_tag.set_property('underline', Pango.Underline.SINGLE)
|
||||||
|
d['tag_id'] = self._latex_tag.connect('event', self._textview_event_after)
|
||||||
|
tb.get_tag_table().add(self._latex_tag)
|
||||||
|
|
||||||
|
d['h_id'] = tb.connect('changed', self._textbuffer_live_latex_expander)
|
||||||
|
chat_control.latexs_expander_plugin_data = d
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _disconnect_chat_control_base(self, chat_control):
|
||||||
|
d = chat_control.latexs_expander_plugin_data
|
||||||
|
tv = chat_control.conv_textview.tv
|
||||||
|
|
||||||
|
tv.get_buffer().disconnect(d['h_id'])
|
||||||
|
self._latex_tag.disconnect(d['tag_id'])
|
||||||
|
|
||||||
|
def _test_activatable(self):
|
||||||
"""
|
"""
|
||||||
performs very simple checks (check if executable is in PATH)
|
performs very simple checks (check if executable is in PATH)
|
||||||
"""
|
"""
|
||||||
@@ -329,6 +109,7 @@ class LatexPlugin(GajimPlugin):
|
|||||||
pkgs = 'texlive-latex-base'
|
pkgs = 'texlive-latex-base'
|
||||||
self.available_text = _('LaTeX is not available')
|
self.available_text = _('LaTeX is not available')
|
||||||
self.activatable = False
|
self.activatable = False
|
||||||
|
|
||||||
if not dvipng_available and not imagemagick_available:
|
if not dvipng_available and not imagemagick_available:
|
||||||
if os.name == 'nt':
|
if os.name == 'nt':
|
||||||
if not pkgs:
|
if not pkgs:
|
||||||
@@ -345,85 +126,65 @@ class LatexPlugin(GajimPlugin):
|
|||||||
self.activatable = False
|
self.activatable = False
|
||||||
self.available_text += _('. Install %s') % pkgs
|
self.available_text += _('. Install %s') % pkgs
|
||||||
|
|
||||||
def textview_event_after(self, tag, widget, event, iter_):
|
def _textview_event_after(self, tag, widget, event, iter_):
|
||||||
"""
|
"""
|
||||||
start rendering if clicked on a link
|
start rendering if clicked on a link
|
||||||
"""
|
"""
|
||||||
if tag.get_property('name') != 'latex' or \
|
if tag.get_property('name') != 'latex' or \
|
||||||
event.type != Gdk.EventType.BUTTON_PRESS:
|
event.type != Gdk.EventType.BUTTON_PRESS:
|
||||||
return
|
return
|
||||||
dollar_start, iter_start = iter_.backward_search('$$',
|
dollar_start, _iter_start = iter_.backward_search(
|
||||||
|
'$$',
|
||||||
Gtk.TextSearchFlags.TEXT_ONLY, None)
|
Gtk.TextSearchFlags.TEXT_ONLY, None)
|
||||||
iter_end, dollar_end = iter_.forward_search('$$',
|
_iter_end, dollar_end = iter_.forward_search(
|
||||||
|
'$$',
|
||||||
Gtk.TextSearchFlags.TEXT_ONLY, None)
|
Gtk.TextSearchFlags.TEXT_ONLY, None)
|
||||||
LatexRenderer(dollar_start, dollar_end, widget.get_buffer(), widget,
|
LatexRenderer(dollar_start, dollar_end, widget, self.config['png_dpi'])
|
||||||
self.config['png_dpi'])
|
|
||||||
|
|
||||||
def textbuffer_live_latex_expander(self, tb):
|
def _textbuffer_live_latex_expander(self, tb):
|
||||||
"""
|
"""
|
||||||
called when conversation text widget changes
|
called when conversation text widget changes
|
||||||
"""
|
"""
|
||||||
def split_list(list_):
|
def _split_list(list_):
|
||||||
newlist = []
|
newlist = []
|
||||||
for i in range(0, len(list_)-1, 2):
|
for i in range(0, len(list_)-1, 2):
|
||||||
newlist.append( [ list_[i], list_[i+1], ] )
|
newlist.append([ list_[i], list_[i+1], ])
|
||||||
return newlist
|
return newlist
|
||||||
|
|
||||||
def detect_tags(tb, start_it=None, end_it=None):
|
def _detect_tags(tb, start_it=None, end_it=None):
|
||||||
self.timeout_id = None
|
self._timeout_id = None
|
||||||
if not end_it:
|
if not end_it:
|
||||||
end_it = tb.get_end_iter()
|
end_it = tb.get_end_iter()
|
||||||
if not start_it:
|
if not start_it:
|
||||||
eol_tag = tb.get_tag_table().lookup('eol')
|
eol_tag = tb.get_tag_table().lookup('eol')
|
||||||
start_it = end_it.copy()
|
start_it = end_it.copy()
|
||||||
start_it.backward_to_tag_toggle(eol_tag)
|
start_it.backward_to_tag_toggle(eol_tag)
|
||||||
points = []
|
points = []
|
||||||
tuple_found = start_it.forward_search('$$',
|
tuple_found = start_it.forward_search(
|
||||||
|
'$$',
|
||||||
Gtk.TextSearchFlags.TEXT_ONLY, None)
|
Gtk.TextSearchFlags.TEXT_ONLY, None)
|
||||||
while tuple_found != None:
|
while tuple_found is not None:
|
||||||
points.append(tuple_found)
|
points.append(tuple_found)
|
||||||
tuple_found = tuple_found[1].forward_search('$$',
|
tuple_found = tuple_found[1].forward_search(
|
||||||
|
'$$',
|
||||||
Gtk.TextSearchFlags.TEXT_ONLY, None)
|
Gtk.TextSearchFlags.TEXT_ONLY, None)
|
||||||
|
|
||||||
for pair in split_list(points):
|
for pair in _split_list(points):
|
||||||
tb.apply_tag_by_name('latex', pair[0][1], pair[1][0])
|
tb.apply_tag_by_name('latex', pair[0][1], pair[1][0])
|
||||||
|
|
||||||
end_iter = tb.get_end_iter()
|
end_iter = tb.get_end_iter()
|
||||||
eol_tag = tb.get_tag_table().lookup('eol')
|
eol_tag = tb.get_tag_table().lookup('eol')
|
||||||
it = end_iter.copy()
|
it = end_iter.copy()
|
||||||
it.backward_to_tag_toggle(eol_tag)
|
it.backward_to_tag_toggle(eol_tag)
|
||||||
if it.get_offset() == self.last_eol_offset:
|
if it.get_offset() == self._last_eol_offset:
|
||||||
if self.timeout_id:
|
if self._timeout_id:
|
||||||
GObject.source_remove(self.timeout_id)
|
GLib.source_remove(self._timeout_id)
|
||||||
self.timeout_id = GObject.timeout_add(100, detect_tags, tb, it, end_iter)
|
self._timeout_id = GLib.timeout_add(100, _detect_tags, tb, it, end_iter)
|
||||||
else:
|
else:
|
||||||
if self.timeout_id:
|
if self._timeout_id:
|
||||||
GObject.source_remove(self.timeout_id)
|
GLib.source_remove(self._timeout_id)
|
||||||
it1 = it.copy()
|
it1 = it.copy()
|
||||||
it1.backward_char()
|
it1.backward_char()
|
||||||
it1.backward_to_tag_toggle(eol_tag)
|
it1.backward_to_tag_toggle(eol_tag)
|
||||||
detect_tags(tb, it1, it)
|
_detect_tags(tb, it1, it)
|
||||||
self.last_eol_offset = it.get_offset()
|
self._last_eol_offset = it.get_offset()
|
||||||
|
|
||||||
def connect_with_chat_control_base(self, chat_control):
|
|
||||||
d = {}
|
|
||||||
tv = chat_control.conv_textview.tv
|
|
||||||
tb = tv.get_buffer()
|
|
||||||
|
|
||||||
self.latex_tag = Gtk.TextTag.new('latex')
|
|
||||||
self.latex_tag.set_property('foreground', 'blue')
|
|
||||||
self.latex_tag.set_property('underline', Pango.Underline.SINGLE)
|
|
||||||
d['tag_id'] = self.latex_tag.connect('event', self.textview_event_after)
|
|
||||||
tb.get_tag_table().add(self.latex_tag)
|
|
||||||
|
|
||||||
d['h_id'] = tb.connect('changed', self.textbuffer_live_latex_expander)
|
|
||||||
chat_control.latexs_expander_plugin_data = d
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
def disconnect_from_chat_control_base(self, chat_control):
|
|
||||||
d = chat_control.latexs_expander_plugin_data
|
|
||||||
tv = chat_control.conv_textview.tv
|
|
||||||
|
|
||||||
tv.get_buffer().disconnect(d['h_id'])
|
|
||||||
self.latex_tag.disconnect(d['tag_id'])
|
|
||||||
|
|||||||
175
latex/latex_renderer.py
Normal file
175
latex/latex_renderer.py
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
# Copyright (C) 2010-2011 Yves Fischer <yvesf AT xapek.org>
|
||||||
|
# Copyright (C) 2011 Yann Leboulanger <asterix AT lagaule.org>
|
||||||
|
#
|
||||||
|
# This file is part of Gajim.
|
||||||
|
#
|
||||||
|
# Gajim 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; version 3 only.
|
||||||
|
#
|
||||||
|
# Gajim 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 Gajim. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import random
|
||||||
|
import os
|
||||||
|
import logging
|
||||||
|
from tempfile import mkstemp
|
||||||
|
from tempfile import mkdtemp
|
||||||
|
|
||||||
|
from gi.repository import Gtk
|
||||||
|
from gi.repository import GdkPixbuf
|
||||||
|
from gi.repository import GLib
|
||||||
|
|
||||||
|
from gajim.common import app
|
||||||
|
|
||||||
|
from gajim.plugins.plugins_i18n import _
|
||||||
|
|
||||||
|
from latex.util import try_run
|
||||||
|
from latex.util import write_latex
|
||||||
|
from latex.util import BLACKLIST
|
||||||
|
|
||||||
|
log = logging.getLogger('gajim.p.latex')
|
||||||
|
|
||||||
|
|
||||||
|
class LatexRenderer:
|
||||||
|
def __init__(self, iter_start, iter_end, widget, png_dpi):
|
||||||
|
self.widget = widget
|
||||||
|
self.buffer = widget.get_buffer()
|
||||||
|
self.code = iter_start.get_text(iter_end)
|
||||||
|
self.mark_name = 'LatexRendererMark%s' % str(random.randint(0, 1000))
|
||||||
|
self.mark = self.buffer.create_mark(self.mark_name, iter_start, True)
|
||||||
|
self.png_dpi = str(png_dpi)
|
||||||
|
|
||||||
|
# delete code and show message 'processing'
|
||||||
|
self.buffer.delete(iter_start, iter_end)
|
||||||
|
self.buffer.insert(iter_start, _('Processing LaTeX'))
|
||||||
|
self._start_processing()
|
||||||
|
|
||||||
|
def _start_processing(self):
|
||||||
|
try:
|
||||||
|
if self._check_code():
|
||||||
|
self._show_image()
|
||||||
|
else:
|
||||||
|
self._show_error(_('There are bad commands!'))
|
||||||
|
except Exception as err:
|
||||||
|
self._show_error(_('Error processing LaTeX: %s' % err))
|
||||||
|
finally:
|
||||||
|
self.buffer.delete_mark(self.mark)
|
||||||
|
|
||||||
|
def _check_code(self):
|
||||||
|
for bad_cmd in BLACKLIST:
|
||||||
|
if self.code.find(bad_cmd) != -1:
|
||||||
|
# Found bad command
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _show_error(self, message):
|
||||||
|
"""
|
||||||
|
String -> TextBuffer
|
||||||
|
"""
|
||||||
|
iter_mark = self.buffer.get_iter_at_mark(self.mark)
|
||||||
|
iter_end = iter_mark.copy().forward_search(
|
||||||
|
_('Processing LaTeX'),
|
||||||
|
Gtk.TextSearchFlags.TEXT_ONLY, None)[1]
|
||||||
|
self.buffer.delete(iter_mark, iter_end)
|
||||||
|
self.buffer.insert(iter_end, message)
|
||||||
|
|
||||||
|
def _show_image(self):
|
||||||
|
"""
|
||||||
|
Latex -> PNG -> TextBuffer
|
||||||
|
"""
|
||||||
|
|
||||||
|
def _fg_str(fmt):
|
||||||
|
try:
|
||||||
|
return [
|
||||||
|
{'hex' : '+level-colors', 'tex' : '-fg'}[fmt],
|
||||||
|
app.interface.get_fg_color(fmt)]
|
||||||
|
except KeyError:
|
||||||
|
# interface may not be available when we test latex at startup
|
||||||
|
return []
|
||||||
|
except AttributeError:
|
||||||
|
# interface may not be available when we test latext at startup
|
||||||
|
return {'hex': ['+level-colors', '0x000000'],
|
||||||
|
'tex': ['-fg', 'rgb 0.0 0.0 0.0']}[fmt]
|
||||||
|
|
||||||
|
try:
|
||||||
|
tmpdir = mkdtemp(prefix='gajim_tex')
|
||||||
|
tmpfd, tmppng = mkstemp(prefix='gajim_tex', suffix='.png')
|
||||||
|
os.close(tmpfd)
|
||||||
|
except Exception:
|
||||||
|
msg = 'Could not create temporary files for Latex plugin'
|
||||||
|
log.debug(msg)
|
||||||
|
self._show_error(
|
||||||
|
_('latex error: %(error)s\n===ORIGINAL CODE====\n'
|
||||||
|
'%(code)s') % {
|
||||||
|
'error': msg,
|
||||||
|
'code': self.code[2:len(self.code)-2]})
|
||||||
|
return False
|
||||||
|
|
||||||
|
tmpfile = os.path.join(tmpdir, 'gajim_tex')
|
||||||
|
|
||||||
|
# build latex string
|
||||||
|
write_latex(tmpfile + '.tex', self.code[2:len(self.code)-2])
|
||||||
|
|
||||||
|
# convert TeX to dvi
|
||||||
|
exitcode = try_run(
|
||||||
|
['latex', '--interaction=nonstopmode', tmpfile + '.tex'], tmpdir)
|
||||||
|
|
||||||
|
if exitcode == 0:
|
||||||
|
# convert dvi to png
|
||||||
|
log.debug('DVI OK')
|
||||||
|
exitcode = try_run(
|
||||||
|
['dvipng', '-bg', 'Transparent'] + _fg_str('tex') + \
|
||||||
|
['-T', 'tight', '-D', self.png_dpi, tmpfile + '.dvi', '-o',
|
||||||
|
tmpfile + '.png'], tmpdir)
|
||||||
|
|
||||||
|
if exitcode:
|
||||||
|
# dvipng failed, try convert
|
||||||
|
log.debug('dvipng failed, try convert')
|
||||||
|
exitcode = try_run(
|
||||||
|
['convert'] + _fg_str('hex') + \
|
||||||
|
['-trim', '-density', self.png_dpi,
|
||||||
|
tmpfile + '.dvi', tmpfile + '.png'], tmpdir)
|
||||||
|
|
||||||
|
# remove temp files created by us and TeX
|
||||||
|
extensions = ['.tex', '.log', '.aux', '.dvi']
|
||||||
|
for ext in extensions:
|
||||||
|
try:
|
||||||
|
os.remove(tmpfile + ext)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if exitcode == 0:
|
||||||
|
log.debug('PNG OK')
|
||||||
|
os.rename(tmpfile + '.png', tmppng)
|
||||||
|
else:
|
||||||
|
log.debug('PNG FAILED')
|
||||||
|
os.remove(tmppng)
|
||||||
|
os.rmdir(tmpdir)
|
||||||
|
self._show_error(
|
||||||
|
_('Convertion to image failed\n===ORIGINAL CODE===='
|
||||||
|
'\n%s') % self.code[2:len(self.code)-2])
|
||||||
|
return False
|
||||||
|
|
||||||
|
log.debug('Loading PNG %s', tmppng)
|
||||||
|
try:
|
||||||
|
pixbuf = GdkPixbuf.Pixbuf.new_from_file(tmppng)
|
||||||
|
log.debug('png loaded')
|
||||||
|
iter_mark = self.buffer.get_iter_at_mark(self.mark)
|
||||||
|
iter_end = iter_mark.copy().forward_search(
|
||||||
|
_('Processing LaTeX'),
|
||||||
|
Gtk.TextSearchFlags.TEXT_ONLY, None)[1]
|
||||||
|
log.debug('Delete old Text')
|
||||||
|
self.buffer.delete(iter_mark, iter_end)
|
||||||
|
log.debug('Insert pixbuf')
|
||||||
|
self.buffer.insert_pixbuf(iter_end, pixbuf)
|
||||||
|
except GLib.GError:
|
||||||
|
self._show_error(_('Cannot open %s for reading') % tmppng)
|
||||||
|
log.debug('Cant open %s for reading', tmppng)
|
||||||
|
finally:
|
||||||
|
os.remove(tmppng)
|
||||||
115
latex/util.py
Normal file
115
latex/util.py
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
# This file is part of Gajim.
|
||||||
|
#
|
||||||
|
# Gajim 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; version 3 only.
|
||||||
|
#
|
||||||
|
# Gajim 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 Gajim. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
from subprocess import Popen
|
||||||
|
from subprocess import PIPE
|
||||||
|
|
||||||
|
from gajim.plugins.plugins_i18n import _
|
||||||
|
|
||||||
|
log = logging.getLogger('gajim.p.latex')
|
||||||
|
|
||||||
|
BLACKLIST = [
|
||||||
|
'\def',
|
||||||
|
'\\let',
|
||||||
|
'\\futurelet',
|
||||||
|
'\\newcommand',
|
||||||
|
'\\renewcomment',
|
||||||
|
'\\else',
|
||||||
|
'\\fi',
|
||||||
|
'\\write',
|
||||||
|
'\\input',
|
||||||
|
'\\include',
|
||||||
|
'\\chardef',
|
||||||
|
'\\catcode',
|
||||||
|
'\\makeatletter',
|
||||||
|
'\\noexpand',
|
||||||
|
'\\toksdef',
|
||||||
|
'\\every',
|
||||||
|
'\\errhelp',
|
||||||
|
'\\errorstopmode',
|
||||||
|
'\\scrollmode',
|
||||||
|
'\\nonstopmode',
|
||||||
|
'\\batchmode',
|
||||||
|
'\\read',
|
||||||
|
'\\csname',
|
||||||
|
'\\newhelp',
|
||||||
|
'\\relax',
|
||||||
|
'\\afterground',
|
||||||
|
'\\afterassignment',
|
||||||
|
'\\expandafter',
|
||||||
|
'\\noexpand',
|
||||||
|
'\\special',
|
||||||
|
'\\command',
|
||||||
|
'\\loop',
|
||||||
|
'\\repeat',
|
||||||
|
'\\toks',
|
||||||
|
'\\output',
|
||||||
|
'\\line',
|
||||||
|
'\\mathcode',
|
||||||
|
'\\name',
|
||||||
|
'\\item',
|
||||||
|
'\\section',
|
||||||
|
'\\mbox',
|
||||||
|
'\\DeclareRobustCommand',
|
||||||
|
'\\[',
|
||||||
|
'\\]',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def try_run(argv, directory):
|
||||||
|
try:
|
||||||
|
proc = popen_nt_friendly(argv, directory)
|
||||||
|
out = proc.communicate()[0]
|
||||||
|
log.info(out)
|
||||||
|
return proc.wait()
|
||||||
|
except Exception as err:
|
||||||
|
return _('Error executing "%(command)s": %(error)s') % {
|
||||||
|
'command': " ".join(argv),
|
||||||
|
'error': str(err)}
|
||||||
|
|
||||||
|
|
||||||
|
def popen_nt_friendly(command, directory):
|
||||||
|
if os.name == 'nt':
|
||||||
|
# CREATE_NO_WINDOW
|
||||||
|
return Popen(command, creationflags=0x08000000, cwd=directory,
|
||||||
|
stdout=PIPE)
|
||||||
|
return Popen(command, cwd=directory, stdout=PIPE)
|
||||||
|
|
||||||
|
|
||||||
|
def write_latex(filename, string):
|
||||||
|
texstr = _get_latex_template(string)
|
||||||
|
|
||||||
|
file_ = open(filename, 'w+')
|
||||||
|
file_.write(texstr)
|
||||||
|
file_.flush()
|
||||||
|
file_.close()
|
||||||
|
|
||||||
|
|
||||||
|
def _get_latex_template(code):
|
||||||
|
template = '''
|
||||||
|
\\documentclass[12pt]{article}
|
||||||
|
\\usepackage[dvips]{graphicx}
|
||||||
|
\\usepackage{amsmath}
|
||||||
|
\\usepackage{amssymb}
|
||||||
|
\\pagestyle{empty}
|
||||||
|
\\begin{document}
|
||||||
|
\\begin{large}
|
||||||
|
\\begin{gather*}
|
||||||
|
%s
|
||||||
|
\\end{gather*}
|
||||||
|
\\end{large}
|
||||||
|
\\end{document}''' % (code)
|
||||||
|
return template
|
||||||
Reference in New Issue
Block a user