Back to index

scribus-ng  1.3.4.dfsg+svn20071115
textfilter.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #include "textfilter.h"
00008 #include <qcstring.h>
00009 #include <qfile.h>
00010 #include <qfileinfo.h>
00011 #include <qstring.h>
00012 #include <qstringlist.h>
00013 #include <qtextcodec.h>
00014 #include <qregexp.h> 
00015 #include <qmap.h>
00016 #include "prefsmanager.h"
00017 #include "prefsfile.h"
00018 #include <vector>
00019 #include "gtframestyle.h"
00020 #include "gtparagraphstyle.h"
00021 #include "tfdia.h"
00022 
00023 QString FileFormatName()
00024 {
00025     return QObject::tr("Text Filters");
00026 }
00027 
00028 QStringList FileExtensions()
00029 {
00030        return QStringList();
00031 }
00032 
00033 void GetText(QString filename, QString encoding, bool, gtWriter *writer)
00034 {
00035        TextFilter* tf = new TextFilter(filename, encoding, writer);
00036        delete tf;
00037 }
00038 
00039 /*********** Class TextFilter *************************************/
00040 
00041 TextFilter::TextFilter(const QString& fname, const QString& enc, gtWriter* w)
00042 {
00043        filename = fname;
00044        encoding = enc;
00045        writer = w;
00046        writer->setOverridePStyleFont(false);
00047        prefs = PrefsManager::instance()->prefsFile->getPluginContext("TextFilter");
00048        tfDia* tfdia = new tfDia();
00049        if (tfdia->exec())
00050        {
00051               filters = &(tfdia->filters);
00052               loadText();
00053               toUnicode();
00054               write();
00055        }
00056        delete tfdia;
00057 }
00058 
00059 
00060 void TextFilter::loadText()
00061 {
00062        text = "";
00063        QFile f(filename);
00064        QFileInfo fi(f);
00065        if (!fi.exists())
00066               return;
00067        uint posi;
00068 //     bool ret;
00069        QByteArray bb(f.size());
00070        if (f.open(IO_ReadOnly))
00071        {
00072               f.readBlock(bb.data(), f.size());
00073               f.close();
00074               for (posi = 0; posi < bb.size(); ++posi)
00075                      text += QChar(bb[posi]);
00076        }
00077 }
00078 
00079 void TextFilter::toUnicode()
00080 {
00081        QTextCodec *codec;
00082        if (encoding.isEmpty())
00083               codec = QTextCodec::codecForLocale();
00084        else
00085               codec = QTextCodec::codecForName(encoding);
00086        QString dec = codec->toUnicode(text);
00087        text = dec;
00088 }
00089 
00090 void TextFilter::write()
00091 {
00092        QMap<QString, gtParagraphStyle*> pstyles;
00093        gtFrameStyle* fstyle = writer->getDefaultStyle();
00094        for (int i = 0; i < static_cast<int>(filters->size()); ++i)
00095        {
00096               if ((*filters)[i]->isEnabled())
00097               {
00098                      int action = (*filters)[i]->getAction();
00099                      QString regExp = (*filters)[i]->regExp();
00100                      QString replaceWith = (*filters)[i]->replaceWith();
00101                      bool useRegexp = (*filters)[i]->isRegExp();
00102                      if (useRegexp)
00103                             replace(&replaceWith);
00104                      QString pstyle = (*filters)[i]->getPStyleName();
00105                      QRegExp rx = QRegExp(regExp);
00106                      rx.setMinimal(true);
00107                      switch (action)
00108                      {
00109                             case REMOVE: 
00110                                    if (useRegexp)
00111                                           text = text.remove(rx);
00112                                    else
00113                                           text = text.remove(regExp);
00114                                    break;
00115                             case REPLACE:
00116                                    if (useRegexp)
00117                                           text = text.replace(rx, replaceWith); 
00118                                    else
00119                                           text = text.replace(regExp, replaceWith);
00120                                    break;
00121                             case APPLY: 
00122                                    pstyles[pstyle] = new gtParagraphStyle(*fstyle);
00123                                    pstyles[pstyle]->setName(pstyle);
00124                                    break;
00125                      }
00126               }
00127        }
00128        if (pstyles.size() == 0)
00129               writer->append(text);
00130        else
00131        {
00132               QStringList list = QStringList::split("\n", text, true);
00133               gtParagraphStyle *useStyle = NULL;
00134               for (int i = 0; i < static_cast<int>(list.size()); ++i)
00135               {
00136                      QString tmpText = list[i];
00137                      QString tmpText2 = tmpText;
00138                      tmpText2.simplifyWhiteSpace();
00139                      int numberOfWords = tmpText2.contains(" ");
00140                      ++numberOfWords;
00141                      useStyle = NULL;
00142                      for (int j = 0; j < static_cast<int>(filters->size()); ++j)
00143                      {
00144                             if ((*filters)[j]->isEnabled())
00145                             {
00146                                    int action = (*filters)[j]->getAction();
00147                                    QString regExp = (*filters)[j]->regExp();
00148                                    QString replaceWith = (*filters)[j]->replaceWith();
00149                                    bool useRegexp = (*filters)[j]->isRegExp();
00150                                    if (useRegexp)
00151                                           replace(&replaceWith);
00152                                    QString pstyle = (*filters)[j]->getPStyleName();
00153                                    int lessThan = (*filters)[j]->getLessThan();
00154                                    int moreThan = (*filters)[j]->getMoreThan();
00155                                    int style = (*filters)[j]->getStyle();
00156                                    bool removeMatch = (*filters)[j]->removeMatch();
00157                                    QRegExp rx = QRegExp(regExp);
00158                                    if (!pstyle.isEmpty())
00159                                    {
00160                                           switch (action)
00161                                           {
00162                                                  case APPLY:
00163                                                         switch (style)
00164                                                         {
00165                                                         case ALL_PARAGRAPHS:
00166                                                                useStyle = pstyles[pstyle];
00167                                                                break;
00168                                                         case STARTS_WITH:
00169                                                                if (useRegexp)
00170                                                                {
00171                                                                       if (tmpText2.find(rx) == 0)
00172                                                                       {
00173                                                                              useStyle = pstyles[pstyle];
00174                                                                              if (removeMatch)
00175                                                                                     tmpText.remove(rx);
00176                                                                       }
00177                                                                }
00178                                                                else
00179                                                                {
00180                                                                       if (tmpText2.find(regExp) == 0)
00181                                                                       {
00182                                                                              useStyle = pstyles[pstyle];
00183                                                                              if (removeMatch)
00184                                                                                     tmpText.remove(regExp);
00185                                                                       }
00186                                                                }
00187                                                                break;
00188                                                         case LESS_THAN:
00189                                                                if ((lessThan != -1) && (numberOfWords < lessThan))
00190                                                                       useStyle = pstyles[pstyle];
00191                                                                break;
00192                                                         case MORE_THAN:
00193                                                                if ((moreThan != -1) && (numberOfWords > moreThan))
00194                                                                       useStyle = pstyles[pstyle];
00195                                                                break;
00196                                                         }
00197                                                         break;
00198                                           }
00199                                    }
00200                             }
00201                      }
00202                      if (i == static_cast<int>(list.size()) - 1)
00203                             writer->append(tmpText, useStyle);
00204                      else
00205                             writer->append(tmpText + "\n", useStyle);
00206               }
00207        }
00208 }
00209 
00210 void TextFilter::replace(QString* text)
00211 {
00212        text->replace("\\\\", "__SLASH_HERE__");
00213        text->replace("\\", "\\");
00214        text->replace("__SLASH_HERE__", "\\\\");
00215 
00216        text->replace("\\\\t", "__|TABCHAR|__");
00217        text->replace("\\t", "\t");
00218        text->replace("__|TABCHAR|__", "\\t");
00219 
00220        text->replace("\\\\n", "__|L-C|__");
00221        text->replace("\\n", "\n");
00222        text->replace("__|L-C|__", "\\n");
00223 
00224        text->replace("\\\\f", "__|F-CHAR|__");
00225        text->replace("\\f", "\f");
00226        text->replace("__|F-CHAR|__", "\\f");
00227 
00228        text->replace("\\\\r", "__|R-CHAR|__");
00229        text->replace("\\r", "\r");
00230        text->replace("__|R-CHAR|__", "\\r");
00231 
00232        text->replace("\\\\v", "__|V-CHAR|__");
00233        text->replace("\\v", "\v");
00234        text->replace("__|V-CHAR|__", "\\v");
00235 
00236        replaceHex(text);
00237 }
00238 
00239 void TextFilter::replaceHex(QString* text)
00240 {
00241        int index;
00242        int pos = 0;
00243        QString hexS;
00244        int hex;
00245        bool ok = false;
00246        do
00247        {
00248               index = text->find("\\x", pos);
00249               if (index != -1)
00250               {
00251                      if ((text->length() - index + 1) > 6)
00252                      {
00253                             hexS = text->mid(index + 2, 4);
00254                             hex = hexS.toInt(&ok, 16);
00255                             if (ok)
00256                             {
00257                                    text->replace("\\x" + hexS, QChar(hex));
00258                             }
00259                      }
00260                      else
00261                             index = -1;
00262                      pos += 2;
00263               }
00264        }
00265        while (index != -1);
00266 }
00267 
00268 TextFilter::~TextFilter()
00269 {
00270 
00271 }