diff --git a/src/lib/textresult.cpp b/src/lib/textresult.cpp index 0f4f9a79..27a891c8 100644 --- a/src/lib/textresult.cpp +++ b/src/lib/textresult.cpp @@ -1,194 +1,198 @@ /* This program 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 2 of the License, or (at your option) any later version. This program 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 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. --- Copyright (C) 2009 Alexander Rieder */ #include "textresult.h" using namespace Cantor; #include #include #include #include #include QString rtrim(const QString& s) { QString result = s; while (result.count() > 0 && result[result.count()-1].isSpace() ) { result = result.left(result.count() -1 ); } return result; } class Cantor::TextResultPrivate { public: TextResultPrivate() { format=TextResult::PlainTextFormat; } QString data; QString plain; TextResult::Format format; }; TextResult::TextResult(const QString& data) : d(new TextResultPrivate) { d->data=rtrim(data); d->plain=d->data; } TextResult::TextResult(const QString& data, const QString& plain) : d(new TextResultPrivate) { d->data=rtrim(data); d->plain=rtrim(plain); } TextResult::~TextResult() { delete d; } QString TextResult::toHtml() { QString s=d->data.toHtmlEscaped(); s.replace(QLatin1Char('\n'), QLatin1String("
\n")); s.replace(QLatin1Char(' '), QLatin1String(" ")); return s; } QVariant TextResult::data() { return QVariant(d->data); } QString TextResult::plain() { return d->plain; } int TextResult::type() { return TextResult::Type; } QString TextResult::mimeType() { qDebug()<<"format: "<format; } void TextResult::setFormat(TextResult::Format f) { d->format=f; } QDomElement TextResult::toXml(QDomDocument& doc) { qDebug()<<"saving textresult "<format == LatexFormat) e.setAttribute(QStringLiteral("format"), QStringLiteral("latex")); QDomText txt=doc.createTextNode(data().toString()); e.appendChild(txt); return e; } QJsonValue Cantor::TextResult::toJupyterJson() { QJsonObject root; switch (d->format) { case PlainTextFormat: { root.insert(QLatin1String("output_type"), QLatin1String("stream")); root.insert(QLatin1String("name"), QLatin1String("stdout")); root.insert(QLatin1String("text"), jupyterText(d->data)); break; } case LatexFormat: { root.insert(QLatin1String("output_type"), QLatin1String("display_data")); QJsonObject data; data.insert(QLatin1String("text/latex"), jupyterText(d->data)); data.insert(QLatin1String("text/plain"), jupyterText(d->plain)); root.insert(QLatin1String("data"), data); root.insert(QLatin1String("metadata"), QJsonObject()); break; } } return root; } void TextResult::save(const QString& filename) { QFile file(filename); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return; QTextStream stream(&file); stream<data; file.close(); } QJsonArray TextResult::jupyterText(const QString& text) { QJsonArray array; const QStringList& lines = text.split(QLatin1Char('\n')); for (int i = 0; i < lines.size(); i++) { QString line = lines[i]; - if (i != lines.size() - 1) // not last - line.append(QLatin1Char('\n')); + // Jupyter don't support a few text result (it merges them into one text), so + // add additinoal \n to end + // See https://github.com/jupyter/notebook/issues/4699 + // If it will fixed, use code below + // if (i != lines.size() - 1) // not last + line.append(QLatin1Char('\n')); array.append(line); } return array; }