diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 227f9bcf..b63269bc 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -1,103 +1,103 @@ set( cantor_LIB_SRCS session.cpp expression.cpp backend.cpp result.cpp textresult.cpp imageresult.cpp mimeresult.cpp epsresult.cpp latexresult.cpp latexrenderer.cpp helpresult.cpp animationresult.cpp htmlresult.cpp extension.cpp assistant.cpp completionobject.cpp syntaxhelpobject.cpp defaulthighlighter.cpp defaultvariablemodel.cpp panelplugin.cpp panelpluginhandler.cpp worksheetaccess.cpp directives/plotdirectives.cpp ) Set( cantor_LIB_HDRS cantor_macros.h #base classes backend.h session.h expression.h extension.h syntaxhelpobject.h completionobject.h #results animationresult.h epsresult.h helpresult.h imageresult.h latexresult.h result.h textresult.h mimeresult.h htmlresult.h #helper classes defaulthighlighter.h defaultvariablemodel.h worksheetaccess.h ) ki18n_wrap_ui(cantor_LIB_SRCS directives/axisrange.ui directives/plottitle.ui) kconfig_add_kcfg_files(cantor_LIB_SRCS settings.kcfgc) install(FILES cantor_libs.kcfg DESTINATION ${KDE_INSTALL_KCFGDIR}) configure_file (config-cantorlib.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-cantorlib.h ) add_library( cantorlibs SHARED ${cantor_LIB_SRCS} ) generate_export_header(cantorlibs BASE_NAME cantor) kcoreaddons_desktop_to_json(cantorlibs cantor_assistant.desktop DEFAULT_SERVICE_TYPE) kcoreaddons_desktop_to_json(cantorlibs cantor_backend.desktop DEFAULT_SERVICE_TYPE) kcoreaddons_desktop_to_json(cantorlibs cantor_panelplugin.desktop DEFAULT_SERVICE_TYPE) target_link_libraries( cantorlibs KF5::Completion KF5::IconThemes KF5::KIOCore KF5::KIOFileWidgets KF5::KIOWidgets KF5::Archive KF5::ConfigCore KF5::ConfigGui KF5::I18n KF5::XmlGui ${QT5_LIBRARIES} Qt5::Xml ) -set (CANTORLIBS_SOVERSION 23) +set (CANTORLIBS_SOVERSION 24) set_target_properties( cantorlibs PROPERTIES VERSION ${KDE_APPLICATIONS_VERSION} SOVERSION ${CANTORLIBS_SOVERSION}) ecm_setup_version(${KDE_APPLICATIONS_VERSION} VARIABLE_PREFIX CANTOR SOVERSION ${CANTORLIBS_SOVERSION} VERSION_HEADER ${CMAKE_CURRENT_BINARY_DIR}/cantorlibs_version.h ) install( TARGETS cantorlibs EXPORT CantorTargets ${KDE_INSTALL_TARGETS_DEFAULT_ARGS} ) install( FILES ${cantor_LIB_HDRS} ${CMAKE_CURRENT_BINARY_DIR}/cantor_export.h ${CMAKE_CURRENT_BINARY_DIR}/cantorlibs_version.h DESTINATION ${KDE_INSTALL_INCLUDEDIR}/cantor COMPONENT Devel ) if(BUILD_TESTING) add_subdirectory(test) endif() diff --git a/src/lib/textresult.cpp b/src/lib/textresult.cpp index 39968f75..e9e05304 100644 --- a/src/lib/textresult.cpp +++ b/src/lib/textresult.cpp @@ -1,218 +1,217 @@ /* 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: { if (executionIndex() != -1) { root.insert(QLatin1String("output_type"), QLatin1String("execute_result")); root.insert(QLatin1String("execution_count"), executionIndex()); QJsonObject data; data.insert(QLatin1String("text/plain"), jupyterText(d->data)); root.insert(QLatin1String("data"), data); root.insert(QLatin1String("metadata"), jupyterMetadata()); } else { root.insert(QLatin1String("output_type"), QLatin1String("stream")); root.insert(QLatin1String("name"), QLatin1String("stdout")); - root.insert(QLatin1String("text"), jupyterText(d->data)); + // 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 + root.insert(QLatin1String("text"), jupyterText(d->data, true)); } break; } case LatexFormat: { if (executionIndex() != -1) { root.insert(QLatin1String("output_type"), QLatin1String("execute_result")); root.insert(QLatin1String("execution_count"), executionIndex()); } else 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"), jupyterMetadata()); 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 TextResult::jupyterText(const QString& text, bool addEndNewLine) { QJsonArray array; const QStringList& lines = text.split(QLatin1Char('\n')); for (int i = 0; i < lines.size(); i++) { QString line = lines[i]; - // 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')); + if (i != lines.size() - 1 || addEndNewLine) + line.append(QLatin1Char('\n')); array.append(line); } return array; } diff --git a/src/lib/textresult.h b/src/lib/textresult.h index b2508700..081cd6e4 100644 --- a/src/lib/textresult.h +++ b/src/lib/textresult.h @@ -1,65 +1,65 @@ /* 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 */ #ifndef _TEXTRESULT_H #define _TEXTRESULT_H #include "result.h" #include "cantor_export.h" namespace Cantor { class TextResultPrivate; class CANTOR_EXPORT TextResult : public Result { public: enum { Type=1 }; enum Format { PlainTextFormat, LatexFormat}; TextResult(const QString& text); TextResult(const QString& text, const QString& plain); ~TextResult() override; QString toHtml() override; QVariant data() override; QString plain(); int type() override; QString mimeType() override; Format format(); void setFormat(Format f); QDomElement toXml(QDomDocument& doc) override; QJsonValue toJupyterJson() override; void save(const QString& filename) override; private: - QJsonArray jupyterText(const QString& text); + QJsonArray jupyterText(const QString& text, bool addEndNewLine = false); private: TextResultPrivate* d; }; } #endif /* _TEXTRESULT_H */