Skip to content

Commit 3faab3f

Browse files
jdcaballerovmgautierfr
authored andcommitted
Add content reference to article
1 parent 3729ae9 commit 3faab3f

File tree

4 files changed

+40
-50
lines changed

4 files changed

+40
-50
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ class ZimTestArticle(ZimArticle):
3434
def is_redirect(self):
3535
return False
3636

37-
@property
38-
def can_write(self):
39-
return True
40-
4137
def get_url(self):
4238
return "A/Monadical_SAS"
4339

@@ -77,7 +73,7 @@ zim_creator.add_article(article)
7773

7874

7975
# Set mandatory metadata
80-
if not zim_creator.mandatory_metadata_ok:
76+
if not zim_creator.mandatory_metadata_ok():
8177
zim_creator.update_metadata(creator='python-libzim',description='Created in python',name='Hola',publisher='Monadical',title='Test Zim')
8278

8379
# Write article to zim file

libzim/examples.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
from libzim import ZimArticle, ZimBlob, ZimCreator
22

33
class ZimTestArticle(ZimArticle):
4-
content = '''<!DOCTYPE html>
5-
<html class="client-js">
6-
<head><meta charset="UTF-8">
7-
<title>Monadical</title>
8-
</head>
9-
<h1> ñññ Hello, it works ñññ </h1></html>'''
104

11-
def __init__(self):
5+
def __init__(self, url, title, content):
126
ZimArticle.__init__(self)
7+
self.url = url
8+
self.title = title
9+
self.content = content
1310

1411
def is_redirect(self):
1512
return False
1613

17-
@property
18-
def can_write(self):
19-
return True
20-
2114
def get_url(self):
22-
return "A/Monadical_SAS"
15+
return f"A/{self.url}"
2316

2417
def get_title(self):
25-
return "Monadical SAS"
18+
return f"{self.title}"
2619

2720
def get_mime_type(self):
2821
return "text/html"
@@ -41,7 +34,23 @@ def get_data(self):
4134

4235
# Create a ZimTestArticle article
4336

44-
article = ZimTestArticle()
37+
content = '''<!DOCTYPE html>
38+
<html class="client-js">
39+
<head><meta charset="UTF-8">
40+
<title>Monadical</title>
41+
</head>
42+
<h1> ñññ Hello, it works ñññ </h1></html>'''
43+
44+
content2 = '''<!DOCTYPE html>
45+
<html class="client-js">
46+
<head><meta charset="UTF-8">
47+
<title>Monadical 2</title>
48+
</head>
49+
<h1> ñññ Hello, it works 2 ñññ </h1></html>'''
50+
51+
article = ZimTestArticle("Monadical_SAS", "Monadical", content)
52+
article2 = ZimTestArticle("Monadical_2", "Monadical 2", content2)
53+
4554
print(article.content)
4655

4756
# Write the article
@@ -50,17 +59,19 @@ def get_data(self):
5059

5160
test_zim_file_path = "/opt/python-libzim/tests/kiwix-test"
5261

53-
zim_creator = ZimCreator(test_zim_file_path + '-' + rnd_str + '.zim',main_page = "welcome",index_language= "eng", min_chunk_size= 2048)
62+
zim_creator = ZimCreator(test_zim_file_path + '-' + rnd_str + '.zim',main_page = "Monadical",index_language= "eng", min_chunk_size= 2048)
5463

55-
# Add article to zim file
64+
# Add articles to zim file
5665
zim_creator.add_article(article)
66+
zim_creator.add_article(article2)
5767

5868
# Set mandatory metadata
59-
if not zim_creator.mandatory_metadata_ok:
69+
if not zim_creator.mandatory_metadata_ok():
6070
zim_creator.update_metadata(creator='python-libzim',description='Created in python',name='Hola',publisher='Monadical',title='Test Zim')
6171

72+
print(zim_creator._get_metadata())
73+
6274
zim_creator.write_metadata(zim_creator._get_metadata())
6375

6476
# Write articles to zim file
65-
zim_creator.finalize()
66-
77+
zim_creator.finalize()

libzim/lib.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ ZimArticleWrapper::getRedirectUrl() const
147147
zim::Blob
148148
ZimArticleWrapper::getData() const
149149
{
150-
return callCythonReturnBlob("get_data");
150+
return callCythonReturnBlob("_get_data");
151151
}
152152

153153
zim::size_type

libzim/libzim.pyx

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ from collections import defaultdict
1717

1818
cdef class ZimBlob:
1919
cdef Blob* c_blob
20-
20+
2121
def __init__(self, content):
2222

2323
if isinstance(content, str):
@@ -38,6 +38,7 @@ cdef class ZimBlob:
3838

3939
cdef class ZimArticle:
4040
cdef ZimArticleWrapper* c_article
41+
cdef ZimBlob blob
4142

4243
def __init__(self):
4344
self.c_article = new ZimArticleWrapper(<cpy_ref.PyObject*>self)
@@ -66,27 +67,16 @@ cdef class ZimArticle:
6667
def redirect_url(self):
6768
raise NotImplementedError
6869

70+
def _get_data(self):
71+
self.blob = self.get_data()
72+
return self.blob
73+
6974
def get_data(self):
7075
raise NotImplementedError
7176

7277
@property
7378
def mimetype(self):
7479
return self.c_article.getMimeType().decode('UTF-8')
75-
76-
@property
77-
def content(self):
78-
blob = self.c_article.getData()
79-
content = blob.data()[:blob.size()]
80-
try:
81-
return content.decode('UTF-8')
82-
except UnicodeDecodeError:
83-
return content
84-
85-
# This changes with implementation
86-
@property
87-
def can_write(self):
88-
raise NotImplementedError
89-
9080

9181
#------ Helper for pure virtual methods --------
9282

@@ -144,15 +134,11 @@ class ZimMetadataArticle(ZimArticle):
144134
def is_redirect(self):
145135
return False
146136

147-
@property
148-
def can_write(self):
149-
return True
150-
151137
def get_url(self):
152138
return f"M/{self.url}"
153139

154140
def get_title(self):
155-
return f"{self.url}"
141+
return ""
156142

157143
def get_mime_type(self):
158144
return "text/plain"
@@ -164,7 +150,7 @@ class ZimMetadataArticle(ZimArticle):
164150
return True
165151

166152
def should_index(self):
167-
return True
153+
return False
168154

169155
def get_data(self):
170156
return ZimBlob(self.metadata_content)
@@ -324,9 +310,6 @@ cdef class ZimCreator:
324310
if self._finalized:
325311
raise RuntimeError("ZimCreator already finalized")
326312

327-
if not article.can_write:
328-
raise RuntimeError("Article is not good for writing")
329-
330313
# Make a shared pointer to ZimArticleWrapper from the ZimArticle object (dereference internal c_article)
331314
cdef shared_ptr[ZimArticleWrapper] art = make_shared[ZimArticleWrapper](dereference(article.c_article));
332315
try:

0 commit comments

Comments
 (0)