/* 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 "sageexpression.h" #include "sagesession.h" #include "textresult.h" #include "imageresult.h" #include "animationresult.h" #include "helpresult.h" #include #include #include #include #include SageExpression::SageExpression( Cantor::Session* session ) : Cantor::Expression(session) { kDebug(); } SageExpression::~SageExpression() { } void SageExpression::evaluate() { kDebug()<<"evaluating "<(session())->appendExpressionToQueue(this); } void SageExpression::interrupt() { kDebug()<<"interrupting"; dynamic_cast(session())->sendSignalToProcess(2); dynamic_cast(session())->waitForNextPrompt(); setStatus(Cantor::Expression::Interrupted); } void SageExpression::parseOutput(const QString& text) { QString output=text; //remove carriage returns, we only use \n internally output.remove('\r'); //replace appearing backspaces, as they mess the whole output up output.remove(QRegExp(".\b")); //replace Escape sequences (only tested with `ls` command) const QChar ESC(0x1b); output.remove(QRegExp(QString(ESC)+"\\][^\a]*\a")); const QString promptRegexpBase("(^|\\n)%1"); const QRegExp promptRegexp(promptRegexpBase.arg(QRegExp::escape(SageSession::SagePrompt))); const QRegExp altPromptRegexp(promptRegexpBase.arg(QRegExp::escape(SageSession::SageAlternativePrompt))); bool endsWithAlternativePrompt=output.endsWith(SageSession::SageAlternativePrompt); //remove all prompts. we do this in a loop, because after we removed the first prompt, //there could be a second one, that isn't matched by promptRegexp in the first run, because //it originally isn't at the beginning of a line. int index=-1, index2=-1; while ( (index=output.indexOf(promptRegexp)) != -1 || (index2=output.indexOf(altPromptRegexp)) != -1 ) { kDebug()<<"got prompt"<name(); if(type->name().contains("image")) { kDebug()<<"adding file "<")); const bool isLatex=m_outputCache.contains("class=\"math\""); //Check if it's latex stuff if(isLatex) //It's latex stuff so encapsulate it into an eqnarray environment { stripped.replace("", "\\begin{eqnarray*}"); stripped.replace("", "\\end{eqnarray*}"); } //strip html tags if(isHtml) { stripped.remove( QRegExp( "<[a-zA-Z\\/][^>]*>" ) ); } else { //Replace < and > with their html code, so they won't be confused as html tags stripped.replace( '<' , "<"); stripped.replace( '>' , ">"); } if (stripped.endsWith('\n')) stripped.chop(1); if (m_isHelpRequest) { //make things quoted in `` `` bold stripped.replace(QRegExp("``([^`]*)``"), "\\1"); result=new Cantor::HelpResult(stripped); } else { result=new Cantor::TextResult(stripped); } if(isLatex) result->setFormat(Cantor::TextResult::LatexFormat); setResult(result); } else { KMimeType::Ptr type=KMimeType::findByUrl(m_imagePath); if(type->is("image/gif")) setResult( new Cantor::AnimationResult( KUrl(m_imagePath ),i18n("Result of %1" , command() ) ) ); else setResult( new Cantor::ImageResult( KUrl(m_imagePath ),i18n("Result of %1" , command() ) ) ); } setStatus(Cantor::Expression::Done); } void SageExpression::onProcessError(const QString& msg) { QString errMsg=i18n("%1\nThe last output was: \n %2", msg, m_outputCache.trimmed()); setErrorMessage(errMsg); setStatus(Cantor::Expression::Error); } QString SageExpression::additionalLatexHeaders() { //The LaTeX sage provides needs the amsmath package. So include it in the header return "\\usepackage{amsmath}\n"; } #include "sageexpression.moc"