KDocTools

docbookl10nhelper.cpp
1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2010 Luigi Toscano <luigi.toscano@tiscali.it>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include "loggingcategory.h"
9
10#include <QCoreApplication>
11#include <QDir>
12#include <QFile>
13#include <QIODevice>
14#include <QList>
15#include <QPair>
16#include <QRegularExpression>
17#include <QStringList>
18#include <QTextStream>
19
20class LangListType : public QList<QPair<QString, QString>>
21{
22public:
23 int searchLang(const QString &el)
24 {
25 for (int i = 0; i < size(); ++i) {
26 if (at(i).first == el) {
27 return i;
28 }
29 }
30 return -1;
31 }
32};
33
34int writeLangFile(const QString &fname, const QString &dtdPath, const LangListType &langMap)
35{
36 QFile outFile(fname);
37 if (!outFile.open(QIODevice::WriteOnly)) {
38 qCCritical(KDocToolsLog) << QStringLiteral("Could not write %1").arg(outFile.fileName());
39 return (1);
40 }
41
42 QTextStream outStream(&outFile);
43 outStream << "<?xml version='1.0'?>\n";
44 outStream << QStringLiteral("<!DOCTYPE l:i18n SYSTEM \"%1\">").arg(dtdPath) << QLatin1Char('\n');
45
46 if (!langMap.isEmpty()) {
47 outStream << "<l:i18n xmlns:l=\"http://docbook.sourceforge.net/xmlns/l10n/1.0\">" << QLatin1Char('\n');
48 LangListType::const_iterator i = langMap.constBegin();
49 while (i != langMap.constEnd()) {
50 outStream << QStringLiteral("<l:l10n language=\"%1\" href=\"%2\"/>").arg((*i).first).arg((*i).second) << QLatin1Char('\n');
51 ++i;
52 }
53 outStream << "</l:i18n>\n";
54 }
55
56 outFile.close();
57
58 return (0);
59}
60
61inline const QString addTrailingSlash(const QString &p)
62{
63 return p.endsWith(QStringLiteral("/")) ? p : p + QStringLiteral("/");
64}
65
66int main(int argc, char **argv)
67{
68 QCoreApplication app(argc, argv);
69
70 const QStringList arguments = app.arguments();
71 if (arguments.count() != 4) {
72 qCCritical(KDocToolsLog) << "wrong argument count";
73 return (1);
74 }
75
76 const QString l10nDir = addTrailingSlash(arguments[1]);
77 const QString l10nCustomDir = addTrailingSlash(arguments[2]);
78 const QString destDir = addTrailingSlash(arguments[3]);
79
80 QFile i18nFile(l10nDir + QStringLiteral("common/l10n.xml"));
81
82 if (!i18nFile.open(QIODevice::ReadOnly)) {
83 qCCritical(KDocToolsLog) << i18nFile.fileName() << " not found";
84 return (1);
85 }
86
87 /*
88 * for each language defined in the original l10n.xml, copy
89 * it into all-l10n.xml and store it in a list;
90 **/
91 const QRegularExpression rxDocType(QStringLiteral("^\\s*<!DOCTYPE\\s+l:i18n\\s+SYSTEM\\s+\"l10n\\.dtd\"\\s*>$"));
92 const QRegularExpression rxEntity(QStringLiteral("^\\s*<l:l10n language=\"([^\\s]+)\"\\s+href=\"([^\\s]+)\"/>\\s*$"));
93 QTextStream inStream(&i18nFile);
94 int parsingState = 0;
95
96 LangListType allLangs;
97
98 while (!inStream.atEnd()) {
99 QString line = inStream.readLine();
100
101 switch (parsingState) {
102 case 0:
103 if (rxDocType.match(line).hasMatch()) {
104 parsingState = 1;
105 // qCDebug(KDocToolsLog) << "DTD found";
106 }
107 break;
108 case 1:
109 const QRegularExpressionMatch match = rxEntity.match(line);
110 if (match.hasMatch()) {
111 const QString langCode = match.captured(1);
112 const QString langFile = l10nDir + QStringLiteral("common/") + match.captured(2);
113 allLangs += qMakePair(langCode, langFile);
114 // qCDebug(KDocToolsLog) << langCode << " - " << langFile;
115 }
116 break;
117 }
118 }
119 i18nFile.close();
120
121 /* read the list of locally-available custom languages */
122 QDir outDir(l10nCustomDir);
123
124 QStringList dirFileFilters;
125 dirFileFilters << QStringLiteral("*.xml");
126 QStringList customLangFiles = outDir.entryList(dirFileFilters, QDir::Files, QDir::Name);
127 /* the following two calls to removeOne should not be needed, as
128 * the customization directory from the sources should not contain
129 * those files
130 */
131 customLangFiles.removeOne(QStringLiteral("all-l10n.xml"));
132 customLangFiles.removeOne(QStringLiteral("kde-custom-l10n.xml"));
133 // qCDebug(KDocToolsLog) << "customLangFiles:" << customLangFiles;
134
135 /*
136 * for each custom language (from directory listing), if it is not
137 * in the list of upstream languages, add it to all-l10n.xml,
138 * otherwise add it to kde-custom-l10n.xml
139 */
140 QStringList::const_iterator i = customLangFiles.constBegin();
141 while (i != customLangFiles.constEnd()) {
142 QString langFile = (*i);
143 /* remove trailing .xml */
144 QString langCode = langFile.left(langFile.length() - 4);
145
146 QPair<QString, QString> cl = qMakePair(langCode, langFile);
147 if ((allLangs.searchLang(langCode)) < 0) {
148 /* custom language not found in upstream list */
149 allLangs += cl;
150 }
151 ++i;
152 }
153
154 const QString all10nFName = destDir + QStringLiteral("all-l10n.xml");
155 return writeLangFile(all10nFName, l10nDir + QStringLiteral("common/l10n.dtd"), allLangs);
156}
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
const_reference at(qsizetype i) const const
const_iterator constBegin() const const
const_iterator constEnd() const const
qsizetype count() const const
bool isEmpty() const const
bool removeOne(const AT &t)
qsizetype size() const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
QString left(qsizetype n) const const
qsizetype length() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:55:57 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.