File: latex-filter.cpp

package info (click to toggle)
ktp-text-ui 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,680 kB
  • ctags: 1,142
  • sloc: cpp: 9,451; sh: 15; makefile: 9
file content (186 lines) | stat: -rw-r--r-- 6,556 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 *    Copyright (C) 2012  Lasath Fernando <kde@lasath.org>
 *    Copyright (C) 2013  Andrea Scarpino <andrea@archlinux.org>
 *
 *    Copyright (c) 2004 by Duncan Mac-Vicar Prett   <duncan@kde.org>
 *    Copyright (c) 2004-2005 by Olivier Goffart  <ogoffart@kde. org>
 *    Kopete    (c) 2001-2004 by the Kopete developers  <kopete-devel@kde.org>
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
*/

#include "latex-filter.h"
// KConfigSkeleton
#include "latexconfig.h"

#include <QImage>
#include <QTextDocument>

#include <KPluginFactory>
#include <KDebug>
#include <KStandardDirs>
#include <KProcess>

LatexFilter::LatexFilter(QObject* parent, const QVariantList &)
    : AbstractMessageFilter(parent)
{
}

void LatexFilter::filterMessage(KTp::Message &message, const KTp::MessageContext &context)
{
    Q_UNUSED(context);

    QString messageText = message.mainMessagePart();
    if (!messageText.contains(QLatin1String("$$"))) {
        return;
    }

    QRegExp rg(QLatin1String("\\$\\$.+\\$\\$"));
    rg.setMinimal(true);

    int pos = 0;
    int numberOfFormula = 0;
    while ((pos = rg.indexIn(messageText, pos)) != -1) {
        QString formula = rg.cap();
        // remove the $$ delimiters on start/end
        formula.remove(QLatin1String("$$"));

        // we can skip totally empty/whitespace-only formulas
        formula = formula.trimmed();

        if (formula.isEmpty() || !isSafe(formula)) {
            continue;
        }

        numberOfFormula++;
        QString image(QLatin1Literal("<br/><img src=\"data:image/png;base64,") %
        handleLatex(formula) %
        QLatin1Literal("\" style=\"max-width:100%;margin-top:3px\"") %
        QLatin1Literal("alt=\"") %
        Qt::escape(formula) %
        QLatin1Literal("\" isEmotion=\"true\"/>"));

        int length = rg.matchedLength();
        messageText.replace(pos, length);

        pos += length;
        message.appendMessagePart(image);
    }
    message.setMainMessagePart(messageText);
}

QString LatexFilter::handleLatex(const QString &latexFormula)
{
    QString latexText;
    latexText.append(LatexConfig::latexHeader());
    latexText.append(QLatin1String("\\begin{document}\n"));
    latexText.append(QString(QLatin1String("$%1$\n"))
                    .arg(latexFormula));
    latexText.append(QLatin1String("\\end{document}"));

    KTemporaryFile texFile;
    texFile.setPrefix(QLatin1String("ktplatex-"));
    texFile.setSuffix(QLatin1String(".tex"));
    if (!texFile.open()) {
      kError() << "Cannot create the TeX file";
      return QString();
    }
    texFile.write(latexText.toAscii());
    texFile.close();

    if (LatexConfig::latexCmd().isEmpty()) {
        kError() << "No TeX compiler set!";
        return QString();
    }
    const QStringList latexCmd = LatexConfig::latexCmd().split(QRegExp(QLatin1String("\\s+")));

    QStringList latexArgs;
    Q_FOREACH(const QString &cmd, latexCmd.mid(1, latexCmd.size())) {
        latexArgs << cmd;
    }
    const KStandardDirs outputDir;
    latexArgs << QString(QLatin1String("-output-directory=%1")).arg(outputDir.resourceDirs("tmp").first());
    latexArgs << texFile.fileName();

    if (KStandardDirs::findExe(latexCmd.first()).isEmpty()) {
        kError() << "Cannot find the TeX" << latexCmd.first() << " program.\n;"
                 << "Please get the software from http://tug.org/texlive/"
                 << "or from your distribution's package manager.";
        return QString();
    }

    kDebug() << "Running " << latexCmd.first() << latexArgs;

    KProcess p;
    p.execute(latexCmd.first(), latexArgs);
    if (p.exitCode()) {
        kError() << "Error compiling the TeX text";
        return QString();
    }

    if (KStandardDirs::findExe(QLatin1String("dvipng")).isEmpty()) {
        kError() << "Cannot find the TeX 'dvipng' program.\n;"
                 << "Please get the software from http://tug.org/texlive/"
                 << "or from your distribution's package manager.";
        return QString();
    }

    const QString dviFile = texFile.fileName().replace(QLatin1String(".tex"), QLatin1String(".dvi"));
    const QString imageFile = texFile.fileName().replace(QLatin1String(".tex"), QLatin1String(".png"));

    QStringList dvipngArgs;
    dvipngArgs << QLatin1String("-D300");
    dvipngArgs << QLatin1String("-bgTransparent");
    dvipngArgs << QString(QLatin1String("-o%1")).arg(imageFile);
    dvipngArgs << dviFile;

    kDebug() << "Rendering dvipng" << dvipngArgs;

    p.execute(QLatin1String("dvipng"), dvipngArgs);
    if (p.exitCode()){
        kError() << "Error rendering the image to PNG";
        return QString();
    }

    // Encode the image to base64
    QFile file(imageFile);
    file.open(QIODevice::ReadOnly);
    QByteArray image = file.readAll();

    // We don't need the files anymore
    QFile::remove(dviFile);
    QFile::remove(imageFile);
    QFile::remove(texFile.fileName().replace(QLatin1String(".tex"), QLatin1String(".aux")));
    QFile::remove(texFile.fileName().replace(QLatin1String(".tex"), QLatin1String(".log")));

    return QString::fromAscii(image.toBase64());
}

bool LatexFilter::isSafe(const QString &latexFormula)
{
    return !latexFormula.contains(QRegExp(QLatin1String(
        "\\\\(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)[^a-zA-Z]"
    )));
}

LatexFilter::~LatexFilter()
{
}

K_PLUGIN_FACTORY(MessageFilterFactory, registerPlugin<LatexFilter>();)
K_EXPORT_PLUGIN(MessageFilterFactory("ktptextui_message_filter_latex"))