diff --git a/src/docbookl10nhelper.cpp b/src/docbookl10nhelper.cpp index a271579..766828c 100644 --- a/src/docbookl10nhelper.cpp +++ b/src/docbookl10nhelper.cpp @@ -1,256 +1,255 @@ /* This file is part of the KDE project Copyright (C) 2010 Luigi Toscano This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include "loggingcategory.h" #include #include #include #include #include #include #include #include #include class LangListType: public QList > { public: int searchLang(const QString &el) { for (int i = 0; i < size(); ++i) { if (at(i).first == el) { return i; } } return -1; } }; int writeLangFile(const QString &fname, const QString &dtdPath, const LangListType &langMap) { QFile outFile(fname); if (! outFile.open(QIODevice::WriteOnly)) { qCCritical(KDocToolsLog) << QStringLiteral("Could not write %1") .arg(outFile.fileName()); return (1); } QTextStream outStream(&outFile); - outStream << "" << endl; + outStream << "\n"; outStream << QStringLiteral("") - .arg((*i).first).arg((*i).second) << endl; + .arg((*i).first).arg((*i).second) << QLatin1Char('\n'); ++i; } - outStream << "]>" << endl; + outStream << "]>\n"; if (!langMap.isEmpty()) { outStream - << "" - << endl; + << "\n"; i = langMap.constBegin(); while (i != langMap.constEnd()) { outStream << QStringLiteral("&%1;") - .arg((*i).first) << endl; + .arg((*i).first) << QLatin1Char('\n'); ++i; } - outStream << "" << endl; + outStream << "\n"; } outFile.close(); return (0); } int writeLangFileNew(const QString &fname, const QString &dtdPath, const LangListType &langMap) { QFile outFile(fname); if (! outFile.open(QIODevice::WriteOnly)) { qCCritical(KDocToolsLog) << QStringLiteral("Could not write %1") .arg(outFile.fileName()); return (1); } QTextStream outStream(&outFile); - outStream << "" << endl; + outStream << "\n"; outStream << QStringLiteral("") - .arg(dtdPath) << endl; + .arg(dtdPath) << QLatin1Char('\n'); if (!langMap.isEmpty()) { outStream << "" - << endl; + << QLatin1Char('\n'); LangListType::const_iterator i = langMap.constBegin(); while (i != langMap.constEnd()) { outStream << QStringLiteral("") - .arg((*i).first).arg((*i).second) << endl; + .arg((*i).first).arg((*i).second) << QLatin1Char('\n'); ++i; } - outStream << "" << endl; + outStream << "\n"; } outFile.close(); return (0); } inline const QString addTrailingSlash(const QString &p) { return p.endsWith(QStringLiteral("/")) ? p : p + QStringLiteral("/"); } int main(int argc, char **argv) { QCoreApplication app(argc, argv); const QStringList arguments = app.arguments(); if (arguments.count() != 4) { qCCritical(KDocToolsLog) << "wrong argument count"; return (1); } const QString l10nDir = addTrailingSlash(arguments[1]); const QString l10nCustomDir = addTrailingSlash(arguments[2]); const QString destDir = addTrailingSlash(arguments[3]); QFile i18nFile(l10nDir + QStringLiteral("common/l10n.xml")); if (! i18nFile.open(QIODevice::ReadOnly)) { qCCritical(KDocToolsLog) << i18nFile.fileName() << " not found"; return (1); } const QString all10nFName = destDir + QStringLiteral("all-l10n.xml"); const QString customl10nFName = destDir + QStringLiteral("kde-custom-l10n.xml"); /* * for each language defined in the original l10n.xml, copy * it into all-l10n.xml and store it in a list; **/ const QRegularExpression rxDocType(QStringLiteral("^\\s*$")); const QRegularExpression rxEntity(QStringLiteral("^\\s*\\s*$")); const QRegularExpression rxEntity2(QStringLiteral("^\\s*\\s*$")); QTextStream inStream(&i18nFile); int parsingState = 0; LangListType allLangs, customLangs; bool foundRxEntity = false; bool foundRxEntity2 = false; while (! inStream.atEnd()) { QString line = inStream.readLine(); switch (parsingState) { case 0: if (rxDocType.match(line).hasMatch()) { parsingState = 1; //qCDebug(KDocToolsLog) << "DTD found"; } else if (rxDocType2.match(line).hasMatch()) { parsingState = 1; //qCDebug(KDocToolsLog) << "DTD found"; } break; case 1: QString langCode, langFile; QRegularExpressionMatch match = rxEntity.match(line); if (match.hasMatch() && !foundRxEntity2) { foundRxEntity = true; langCode = match.captured(1); langFile = l10nDir + QStringLiteral("common/") + match.captured(2); allLangs += qMakePair(langCode, langFile); //qCDebug(KDocToolsLog) << langCode << " - " << langFile; } else if (!foundRxEntity) { match = rxEntity2.match(line); if (match.hasMatch()) { foundRxEntity2 = true; langCode = match.captured(1); langFile = l10nDir + QStringLiteral("common/") + match.captured(2); allLangs += qMakePair(langCode, langFile); //qCDebug(KDocToolsLog) << langCode << " - " << langFile; } } break; } } i18nFile.close(); /* read the list of locally-available custom languages */ QDir outDir(l10nCustomDir); QStringList dirFileFilters; dirFileFilters << QStringLiteral("*.xml"); QStringList customLangFiles = outDir.entryList(dirFileFilters, QDir::Files, QDir::Name); /* the following two calls to removeOne should not be needed, as * the customization directory from the sources should not contain * those files */ customLangFiles.removeOne(QStringLiteral("all-l10n.xml")); customLangFiles.removeOne(QStringLiteral("kde-custom-l10n.xml")); //qCDebug(KDocToolsLog) << "customLangFiles:" << customLangFiles; /* * for each custom language (from directory listing), if it is not * in the list of upstream languages, add it to all-l10n.xml, * otherwise add it to kde-custom-l10n.xml */ QStringList::const_iterator i = customLangFiles.constBegin(); while (i != customLangFiles.constEnd()) { QString langFile = (*i); /* remove trailing .xml */ QString langCode = langFile.left(langFile.length() - 4); QPair cl = qMakePair(langCode, langFile); if ((allLangs.searchLang(langCode)) > 0) { /* custom language found in upstream list */ customLangs += cl; } else { /* custom language not found in upstream list */ allLangs += cl; } ++i; } int res = 0; if (foundRxEntity) { /* old style (docbook-xsl<=1.75) */ res = writeLangFile(all10nFName, l10nDir + QStringLiteral("common/l10n.dtd"), allLangs); } else { res = writeLangFileNew(all10nFName, l10nDir + QStringLiteral("common/l10n.dtd"), allLangs); } return (res); }