-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyas.h
123 lines (95 loc) · 2.6 KB
/
pyas.h
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
#include <zim/blob.h>
#include <zim/writer/articlesource.h>
#include <zim/writer/zimcreator.h>
#include <Python.h>
#include <algorithm>
class PyArticle: public zim::writer::Article {
public:
PyArticle(char ns, const std::string& url, const std::string& title,
const std::string& aid, const std::string& redirectAid,
const std::string& mimetype)
: namespace_(ns),
url_(url),
title_(title),
aid_(aid),
redirectAid_(redirectAid),
mimetype_(mimetype) {
}
std::string getAid() const {
return aid_;
}
char getNamespace() const {
return namespace_;
}
std::string getUrl() const {
return url_;
}
std::string getTitle() const {
return title_;
}
bool isRedirect() const {
return !redirectAid_.empty();
}
std::string getMimeType() const {
return mimetype_;
}
bool shouldCompress() const {
return mimetype_ == "text/html";
}
std::string getRedirectAid() const {
return redirectAid_;
}
private:
char namespace_;
std::string url_;
std::string title_;
std::string aid_;
std::string redirectAid_;
std::string mimetype_;
std::string data_;
};
class AidCmp {
public:
AidCmp(const std::string& aid) : aid_(aid) {}
bool operator()(const PyArticle* article) {
return aid_ == article->getAid();
}
private:
const std::string& aid_;
};
class PyArticleSource: public zim::writer::ArticleSource {
public:
typedef PyArticle* (*GetNextArticle)(PyObject* obj);
typedef std::string (*GetData)(PyObject* obj, std::string aid);
PyArticleSource(PyObject* pyObj, GetNextArticle getNextArticle, GetData getData)
: pyObj_(pyObj),
getNextArticle_(getNextArticle),
getData_(getData),
currentData_("") {
Py_XINCREF(pyObj_);
}
~PyArticleSource() {
Py_XDECREF(pyObj_);
}
virtual const zim::writer::Article* getNextArticle() {
return getNextArticle_(pyObj_);
}
zim::Blob getData(const std::string& aid) {
currentData_ = getData_(pyObj_, aid);
return zim::Blob(currentData_.data(), currentData_.length());
}
std::string getMainPage() {
return mainPage;
}
std::string mainPage;
private:
PyObject* pyObj_;
GetNextArticle getNextArticle_;
GetData getData_;
std::string currentData_;
};
void create(const std::string& fname, PyArticleSource* src) {
int argc = 0;
zim::writer::ZimCreator creator(argc, (char**)0);
creator.create(fname, *src);
}