Warning, file /education/cantor/src/lib/textresult.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 SPDX-License-Identifier: GPL-2.0-or-later 0003 SPDX-FileCopyrightText: 2009 Alexander Rieder <alexanderrieder@gmail.com> 0004 SPDX-FileCopyrightText: 2022 Alexander Semke <alexander.semke@web.de> 0005 */ 0006 0007 #include "textresult.h" 0008 using namespace Cantor; 0009 0010 #include <QDebug> 0011 0012 #include <QFile> 0013 #include <QTextStream> 0014 #include <QJsonArray> 0015 #include <QJsonObject> 0016 0017 QString rtrim(const QString& s) 0018 { 0019 QString result = s; 0020 while (result.count() > 0 && result[result.count()-1].isSpace() ) 0021 { 0022 result = result.left(result.count() -1 ); 0023 } 0024 return result; 0025 } 0026 0027 class Cantor::TextResultPrivate 0028 { 0029 public: 0030 QString data; 0031 QString plain; 0032 TextResult::Format format{TextResult::PlainTextFormat}; 0033 bool isStderr{false}; 0034 bool isWarning{false}; 0035 }; 0036 0037 TextResult::TextResult(const QString& data) : d(new TextResultPrivate) 0038 { 0039 d->data=rtrim(data); 0040 d->plain=d->data; 0041 } 0042 0043 TextResult::TextResult(const QString& data, const QString& plain) : d(new TextResultPrivate) 0044 { 0045 d->data=rtrim(data); 0046 d->plain=rtrim(plain); 0047 } 0048 0049 TextResult::~TextResult() 0050 { 0051 delete d; 0052 } 0053 0054 void TextResult::setIsWarning(bool value) 0055 { 0056 d->isWarning = value; 0057 } 0058 0059 bool TextResult::isWarning() const 0060 { 0061 return d->isWarning; 0062 } 0063 0064 QString TextResult::toHtml() 0065 { 0066 QString s=d->data.toHtmlEscaped(); 0067 s.replace(QLatin1Char('\n'), QLatin1String("<br/>\n")); 0068 s.replace(QLatin1Char(' '), QLatin1String(" ")); 0069 return s; 0070 } 0071 0072 QVariant TextResult::data() 0073 { 0074 return QVariant(d->data); 0075 } 0076 0077 QString TextResult::plain() 0078 { 0079 return d->plain; 0080 } 0081 0082 int TextResult::type() 0083 { 0084 return TextResult::Type; 0085 } 0086 0087 QString TextResult::mimeType() 0088 { 0089 qDebug()<<"format: "<<format(); 0090 switch(format()) 0091 { 0092 case TextResult::PlainTextFormat: 0093 return QStringLiteral("text/plain"); 0094 0095 case TextResult::LatexFormat: 0096 return QStringLiteral("text/x-tex"); 0097 0098 default: 0099 return QString(); 0100 } 0101 } 0102 0103 TextResult::Format TextResult::format() 0104 { 0105 return d->format; 0106 } 0107 0108 void TextResult::setFormat(TextResult::Format f) 0109 { 0110 d->format=f; 0111 } 0112 0113 QDomElement TextResult::toXml(QDomDocument& doc) 0114 { 0115 qDebug()<<"saving textresult "<<toHtml(); 0116 QDomElement e=doc.createElement(QStringLiteral("Result")); 0117 e.setAttribute(QStringLiteral("type"), QStringLiteral("text")); 0118 e.setAttribute(QStringLiteral("stderr"), d->isStderr); 0119 if (d->format == LatexFormat) 0120 e.setAttribute(QStringLiteral("format"), QStringLiteral("latex")); 0121 QDomText txt=doc.createTextNode(data().toString()); 0122 e.appendChild(txt); 0123 0124 return e; 0125 } 0126 0127 QJsonValue Cantor::TextResult::toJupyterJson() 0128 { 0129 QJsonObject root; 0130 0131 switch (d->format) 0132 { 0133 case PlainTextFormat: 0134 { 0135 if (executionIndex() != -1) 0136 { 0137 root.insert(QLatin1String("output_type"), QLatin1String("execute_result")); 0138 root.insert(QLatin1String("execution_count"), executionIndex()); 0139 0140 QJsonObject data; 0141 data.insert(QLatin1String("text/plain"), jupyterText(d->data)); 0142 root.insert(QLatin1String("data"), data); 0143 0144 root.insert(QLatin1String("metadata"), jupyterMetadata()); 0145 } 0146 else 0147 { 0148 root.insert(QLatin1String("output_type"), QLatin1String("stream")); 0149 if (d->isStderr) 0150 root.insert(QLatin1String("name"), QLatin1String("stderr")); 0151 else 0152 root.insert(QLatin1String("name"), QLatin1String("stdout")); 0153 0154 // Jupyter don't support a few text result (it merges them into one text), 0155 // so add additional \n to end 0156 // See https://github.com/jupyter/notebook/issues/4699 0157 root.insert(QLatin1String("text"), jupyterText(d->data, true)); 0158 } 0159 break; 0160 } 0161 0162 case LatexFormat: 0163 { 0164 if (executionIndex() != -1) 0165 { 0166 root.insert(QLatin1String("output_type"), QLatin1String("execute_result")); 0167 root.insert(QLatin1String("execution_count"), executionIndex()); 0168 } 0169 else 0170 root.insert(QLatin1String("output_type"), QLatin1String("display_data")); 0171 0172 QJsonObject data; 0173 data.insert(QLatin1String("text/latex"), jupyterText(d->data)); 0174 data.insert(QLatin1String("text/plain"), jupyterText(d->plain)); 0175 root.insert(QLatin1String("data"), data); 0176 0177 root.insert(QLatin1String("metadata"), jupyterMetadata()); 0178 break; 0179 } 0180 } 0181 0182 return root; 0183 } 0184 0185 void TextResult::save(const QString& filename) 0186 { 0187 QFile file(filename); 0188 0189 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) 0190 return; 0191 0192 QTextStream stream(&file); 0193 0194 stream<<d->data; 0195 0196 file.close(); 0197 } 0198 0199 QJsonArray TextResult::jupyterText(const QString& text, bool addEndNewLine) 0200 { 0201 QJsonArray array; 0202 0203 const QStringList& lines = text.split(QLatin1Char('\n')); 0204 for (int i = 0; i < lines.size(); i++) 0205 { 0206 QString line = lines[i]; 0207 if (i != lines.size() - 1 || addEndNewLine) 0208 line.append(QLatin1Char('\n')); 0209 array.append(line); 0210 } 0211 0212 return array; 0213 } 0214 0215 bool Cantor::TextResult::isStderr() const 0216 { 0217 return d->isStderr; 0218 } 0219 0220 void Cantor::TextResult::setStdErr(bool value) 0221 { 0222 d->isStderr = value; 0223 }