-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathzimrecreate.cpp
258 lines (228 loc) · 8.01 KB
/
zimrecreate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* Copyright (C) 2019-2020 Matthieu Gautier <[email protected]>
*
* 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 3 of the License, or
* 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.
*/
#include <iostream>
#include <sstream>
#include <vector>
#include <zim/writer/creator.h>
#include <zim/blob.h>
#include <zim/item.h>
#include <zim/archive.h>
#include <list>
#include <algorithm>
#include <sstream>
#include "tools.h"
#include "version.h"
/**
* A PatchItem. This patch html and css content to remove the namespcae from the links.
*/
class PatchItem : public zim::writer::Item
{
//article from an existing ZIM file.
zim::Item item;
public:
explicit PatchItem(const zim::Item item):
item(item)
{}
virtual std::string getPath() const
{
auto path = item.getPath();
if (path.length() > 2 && path[1] == '/') {
path = path.substr(2, std::string::npos);
}
return path;
}
virtual std::string getTitle() const
{
return item.getTitle();
}
virtual std::string getMimeType() const
{
return item.getMimetype();
}
std::unique_ptr<zim::writer::ContentProvider> getContentProvider() const
{
auto mimetype = getMimeType();
if ( mimetype.find("text/html") == std::string::npos
&& mimetype.find("text/css") == std::string::npos) {
return std::unique_ptr<zim::writer::ContentProvider>(new ItemProvider(item));
}
std::string content = item.getData();
// This is a really poor url rewriting to remove the starting "../<NS>/"
// and replace the "../../<NS/" by "../" :
// - Performance may be better
// - We only fix links in articles in "root" path (`foo.html`) and in one subdirectory (`bar/foo.hmtl`)
// Deeper articles are not fixed (`bar/baz/foo.html`).
// - We may change content starting by `'../A/` even if they are not links
// - We don't handle links where we go upper in the middle of the link : `../foo/../I/image.png`
// - ...
// However, this should patch most of the links in our zim files.
for (std::string prefix: {"'", "\""}) {
for (auto ns : {'A','I','J','-'}) {
replaceStringInPlace(content, prefix+"../../"+ns+"/", prefix+"../");
replaceStringInPlace(content, prefix+"../"+ns+"/", prefix);
}
}
return std::unique_ptr<zim::writer::ContentProvider>(new zim::writer::StringProvider(content));
}
zim::writer::Hints getHints() const {
return { { zim::writer::HintKeys::FRONT_ARTICLE, guess_is_front_article(item.getMimetype()) } };
}
};
void create(const std::string& originFilename, const std::string& outFilename, bool withFtIndexFlag, unsigned long nbThreads)
{
zim::Archive origin(originFilename);
zim::writer::Creator zimCreator;
zimCreator.configVerbose(true)
// [TODO] Use the correct language
.configIndexing(withFtIndexFlag, "eng")
.configClusterSize(2048*1024)
.configNbWorkers(nbThreads);
std::cout << "starting zim creation" << std::endl;
zimCreator.startZimCreation(outFilename);
auto fromNewNamespace = origin.hasNewNamespaceScheme();
try {
auto mainPath = origin.getMainEntry().getItem(true).getPath();
if (!fromNewNamespace) {
mainPath = mainPath.substr(2, std::string::npos);
}
zimCreator.setMainPath(mainPath);
} catch(...) {}
try {
auto illustration = origin.getIllustrationItem();
zimCreator.addIllustration(48, illustration.getData());
} catch(...) {}
for(auto& metakey:origin.getMetadataKeys()) {
if (metakey == "Counter" || metakey.find("Illustration_") == 0) {
// Counter is already added by libzim
// Illustration is already handled by `addIllustration`
continue;
}
auto metadata = origin.getMetadata(metakey);
auto metaProvider = std::unique_ptr<zim::writer::ContentProvider>(new zim::writer::StringProvider(metadata));
zimCreator.addMetadata(metakey, std::move(metaProvider), "text/plain");
}
for(auto& entry:origin.iterEfficient()) {
if (fromNewNamespace) {
//easy, just "copy" the item.
if (entry.isRedirect()) {
zimCreator.addRedirection(entry.getPath(), entry.getTitle(), entry.getRedirectEntry().getPath(), {{zim::writer::HintKeys::FRONT_ARTICLE, 1}});
} else {
auto tmpItem = std::shared_ptr<zim::writer::Item>(new CopyItem(entry.getItem()));
zimCreator.addItem(tmpItem);
}
continue;
}
// We have to adapt the content to drop the namespace.
auto path = entry.getPath();
if (path[0] == 'Z' || path[0] == 'X' || path[0] == 'M' || path[0] == 'W') {
// Index is recreated by zimCreator. Do not add it
continue;
}
path = path.substr(2, std::string::npos);
if (entry.isRedirect()) {
auto redirectPath = entry.getRedirectEntry().getPath();
redirectPath = redirectPath.substr(2, std::string::npos);
zimCreator.addRedirection(path, entry.getTitle(), redirectPath);
} else {
auto tmpItem = std::shared_ptr<zim::writer::Item>(new PatchItem(entry.getItem()));
zimCreator.addItem(tmpItem);
}
}
zimCreator.finishZimCreation();
}
void usage()
{
std::cout << "\nzimrecreate recreates a ZIM file from a existing ZIM.\n"
"\nUsage: zimrecreate ORIGIN_FILE OUTPUT_FILE [Options]"
"\nOptions:\n"
"\t-v, --version print software version\n"
"\t-j, --withoutFTIndex don't create and add a fulltext index of the content to the ZIM\n"
"\t-J, --threads <number> count of threads to utilize (default: 4)\n"
"\nReturn value:\n"
"- 0 if no error\n"
"- -1 if arguments are not valid\n"
"- -2 if zim creation fails\n";
return;
}
int main(int argc, char* argv[])
{
bool withFtIndexFlag = true;
unsigned long nbThreads = 4;
//Parsing arguments
//There will be only two arguments, so no detailed parsing is required.
for(int i=0;i<argc;i++)
{
if(std::string(argv[i])=="-H" ||
std::string(argv[i])=="--help" ||
std::string(argv[i])=="-h")
{
usage();
return 0;
}
if(std::string(argv[i])=="--version" ||
std::string(argv[i])=="-v")
{
printVersions();
return 0;
}
if(std::string(argv[i])=="--withoutFTIndex" ||
std::string(argv[i])=="-j")
{
withFtIndexFlag = false;
}
if(std::string(argv[i])=="-J" ||
std::string(argv[i])=="--threads")
{
if(argc<5)
{
std::cout << std::endl << "[ERROR] Not enough Arguments provided" << std::endl;
usage();
return -1;
}
try
{
nbThreads = std::stoul(argv[i+1]);
}
catch (...)
{
std::cerr << "The number of workers should be a number" << std::endl;
usage();
return -1;
}
}
}
if(argc<3)
{
std::cout << std::endl << "[ERROR] Not enough Arguments provided" << std::endl;
usage();
return -1;
}
std::string originFilename = argv[1];
std::string outputFilename = argv[2];
try
{
create(originFilename, outputFilename, withFtIndexFlag, nbThreads);
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
return -2;
}
return 0;
}