diff --git a/src/catalog/gettext/gettextexport.h b/src/catalog/gettext/gettextexport.h --- a/src/catalog/gettext/gettextexport.h +++ b/src/catalog/gettext/gettextexport.h @@ -71,6 +71,11 @@ */ void writeKeyword(QTextStream& stream, const QString& keyword, QString text, bool containsHtml = true, bool startedWithEmptyLine = false) const; + /** + * Tokenize text into list of tokens and delimiters. List may contain empty tokens. + */ + static void tokenize(const QString& text, QStringList& tokens, const QRegExp& delimiters); + public: /** * @brief Width of the wrap diff --git a/src/catalog/gettext/gettextexport.cpp b/src/catalog/gettext/gettextexport.cpp --- a/src/catalog/gettext/gettextexport.cpp +++ b/src/catalog/gettext/gettextexport.cpp @@ -142,6 +142,21 @@ return OK; } +void GettextExportPlugin::tokenize(const QString& text, QStringList& tokens, const QRegExp& delimiters) +{ + int lastpos = 0; + int pos = text.indexOf(delimiters, lastpos); + while (pos != -1 && lastpos != -1) { + tokens.append(text.mid(lastpos, pos - lastpos)); + // Add delimiter. + // FIXME fixed length + tokens.append(text.mid(pos, 1)); + lastpos = pos + 1; + pos = text.indexOf(delimiters, lastpos); + } + tokens.append(text.mid(lastpos, pos - lastpos)); +} + void GettextExportPlugin::writeComment(QTextStream& stream, const QString& comment) const { if (!comment.isEmpty()) { @@ -228,74 +243,56 @@ list.prepend(QString()); stream << keyword << QStringLiteral(" "); - QStringList::const_iterator it; - for (it = list.constBegin(); it != list.constEnd(); ++it) + for (QStringList::const_iterator it = list.constBegin(); it != list.constEnd(); ++it) stream << QStringLiteral("\"") << (*it) << QStringLiteral("\"\n"); return; } - // lazy wrapping - QStringList list = text.split('\n', QString::SkipEmptyParts); - - if (text.startsWith('\n')) - list.prepend(QString()); - - if (list.isEmpty()) - list.append(QString()); - - //static QRegExp breakStopReForHtml("[ >.%/:,]", Qt::CaseSensitive, QRegExp::Wildcard); - //static QRegExp breakStopReForText("[ .%/:,]", Qt::CaseSensitive, QRegExp::Wildcard); - static QRegExp breakStopReForHtml(QStringLiteral("[ >%]"), Qt::CaseSensitive, QRegExp::Wildcard); - static QRegExp breakStopReForText(QStringLiteral("[ &%]"), Qt::CaseSensitive, QRegExp::Wildcard); - QRegExp breakStopRe = containsHtml ? breakStopReForHtml : breakStopReForText; + static QRegExp reForHtml(QStringLiteral("[ >%]"), Qt::CaseSensitive, QRegExp::Wildcard); + static QRegExp reForText(QStringLiteral("[ &%]"), Qt::CaseSensitive, QRegExp::Wildcard); + QRegExp re = containsHtml ? reForHtml : reForText; int max = m_wrapWidth - 2; - bool prependedEmptyLine = false; - QStringList::iterator itm; - for (itm = list.begin(); itm != list.end(); ++itm) { - if (list.count() == 1 && keyword.length() + 1 + itm->length() >= max) { - prependedEmptyLine = true; - itm = list.insert(itm, QString()); - } - if (itm->length() > max) { - int pos = itm->lastIndexOf(breakStopRe, max - 1); - if (pos > (max / 2)) { - int pos2 = itm->indexOf(QLatin1Char('<'), pos); - if (pos2 > 0 && pos2 < max - 1) { - pos = itm->indexOf(QLatin1Char('<'), pos); - ++pos; - } - } else { - if (itm->at(max - 1) == QLatin1Char('\\')) { - do { - --max; - } while (max >= 2 && itm->at(max - 1) == QLatin1Char('\\')); - } - pos = max; - //Restore the max variable to the m_wordWrap - 2 value - max = m_wrapWidth - 2; + // Remove newlines and re-add them where they needed + text.remove(QStringLiteral("\n")); + text.replace(QStringLiteral("\\n"), QStringLiteral("\\n\n")); + QStringList list = text.split(QStringLiteral("\n"), QString::KeepEmptyParts); + QStringList wrapped_list; + + // Iterate each newline string + for (QStringList::const_iterator it = list.constBegin(); it != list.constEnd(); ++it) { + QStringList words; + QString wrapped_string; + tokenize((*it), words, re); + if (!words.isEmpty()) { + wrapped_string = words.takeFirst(); + } + while (!words.isEmpty()) { + QString delimiter = words.takeFirst(); + QString word; + if (!words.isEmpty()) { + word = words.takeFirst(); } - //itm=list.insert(itm,itm->left(pos)); - QString t = *itm; - itm = list.insert(itm, t); - ++itm; - if (itm != list.end()) { - (*itm) = itm->remove(0, pos); - --itm; - if (itm != list.end()) - itm->truncate(pos); + if (wrapped_string.length() + delimiter.length() + word.length() < max) { + wrapped_string += delimiter + word; + } else { + wrapped_list.append(wrapped_string + delimiter); + wrapped_string = word; } } + if (!wrapped_string.isEmpty()) { + wrapped_list.append(wrapped_string); + } } - if (!prependedEmptyLine && list.count() > 1) - list.prepend(QString()); + if (wrapped_list.count() > 1 || keyword.length() + 1 + wrapped_list.first().length() >= max) { + wrapped_list.prepend(QString()); + } stream << keyword << QStringLiteral(" "); - - QStringList::const_iterator it; - for (it = list.constBegin(); it != list.constEnd(); ++it) + for (QStringList::const_iterator it = wrapped_list.constBegin(); it != wrapped_list.constEnd(); ++it) { stream << QStringLiteral("\"") << (*it) << QStringLiteral("\"\n"); + } }