Debian Codegraph @

src/file/ApvlvFb2.cc

raw
1 /*
2 * This file is part of the apvlv package
3 * Copyright (C) <2010> <Alf>
4 *
5 * Contact: Alf <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22 /* @CPPFILE ApvlvFb2.cc
23 *
24 * Author: Alf <[email protected]>
25 */
26
27 #include <QFile>
28 #include <cmath>
29 #include <sstream>
30
31 #include "ApvlvFb2.h"
32 #include "ApvlvUtil.h"
33 #include "ApvlvWebViewWidget.h"
34
35 namespace apvlv
36 {
37
38 using namespace std;
39
40 const string stylesheet_content = ".block_c {\n"
41 " display: block;\n"
42 " font-size: 2.5em;\n"
43 " font-weight: normal;\n"
44 " line-height: 33.6pt;\n"
45 " text-align: center;\n"
46 " text-indent: 0;\n"
47 " margin: 17pt 0;\n"
48 " padding: 0;\n"
49 "}\n"
50 ".block_ {\n"
51 " display: block;\n"
52 " font-size: 1.5em;\n"
53 " font-weight: normal;\n"
54 " line-height: 33.6pt;\n"
55 " text-align: justify;\n"
56 " text-indent: 0;\n"
57 " margin: 17pt 0;\n"
58 " padding: 0;\n"
59 "}\n"
60 ".block_1 {\n"
61 " display: block;\n"
62 " line-height: 1.2;\n"
63 " text-align: justify;\n"
64 " margin: 0 0 7pt;\n"
65 " padding: 0;\n"
66 "}\n";
67 const string title_template = "<?xml version='1.0' encoding='UTF-8'?>\n"
68 "<html xmlns=\"http://www.w3.org/1999/xhtml\" "
69 "lang=\"en\" xml:lang=\"en\">\n"
70 " <head>\n"
71 " <title></title>\n"
72 " <link rel=\"stylesheet\" type=\"text/css\" "
73 "href=\"stylesheet.css\"/>\n"
74 " <meta http-equiv=\"Content-Type\" "
75 "content=\"text/html; charset=utf-8\"/>\n"
76 " </head>\n"
77 " <body>\n"
78 " <br />\n"
79 " <br />\n"
80 " <br />\n"
81 " <br />\n"
82 " %s\n"
83 " </body>\n"
84 "</html>\n";
85 const string section_template = "<?xml version='1.0' encoding='UTF-8'?>\n"
86 "<html xmlns=\"http://www.w3.org/1999/xhtml\" "
87 "lang=\"en\" xml:lang=\"en\">\n"
88 " <head>\n"
89 " <title></title>\n"
90 " <link rel=\"stylesheet\" "
91 "type=\"text/css\" href=\"stylesheet.css\"/>\n"
92 " <meta http-equiv=\"Content-Type\" "
93 "content=\"text/html; charset=utf-8\"/>\n"
94 " </head>\n"
95 " <body>\n"
96 " %s\n"
97 " </body>\n"
98 "</html>\n";
99
100 FILE_TYPE_DEFINITION ("Web", ApvlvFB2, { ".fb2" });
101
102 bool
103 ApvlvFB2::load (const string &filename)
104 {
105 QFile file (QString::fromLocal8Bit (filename));
106 if (!file.open (QFile::ReadOnly | QFile::Text))
107 {
108 return false;
109 }
110
111 auto bytes = file.readAll ();
112 parseFb2 (bytes.constData (), bytes.length ());
113 return true;
114 }
115
116 bool
117 ApvlvFB2::parseFb2 (const char *content, size_t length)
118 {
119 parseDescription (content, length);
120 parseBinary (content, length);
121 parseBody (content, length);
122
123 generateIndex ();
124
125 return true;
126 }
127
128 bool
129 ApvlvFB2::parseDescription (const char *content, size_t length)
130 {
131 vector<string> keys{ "FictionBook", "description", "title-info", "coverpage",
132 "image" };
133 auto value = xmlContentGetAttributeValue (content, length, keys, "href");
134 mCoverHref = value;
135 return true;
136 }
137
138 bool
139 ApvlvFB2::parseBody (const char *content, size_t length)
140 {
141 vector<string> keys = { "FictionBook", "body" };
142 auto optxml = xmlContentGetElement (content, length, keys);
143 if (!optxml)
144 return false;
145
146 auto xml = optxml->get ();
147 while (!xml->atEnd ()
148 && !(xml->isEndElement () && xml->name ().toString () == "body"))
149 {
150 if (xml->isStartElement () && xml->name () == QString ("title"))
151 {
152 stringstream ss;
153 while (!xml->atEnd ()
154 && !(xml->isEndElement ()
155 && xml->name ().toString () == "title"))
156 {
157 if (xml->isStartElement ())
158 {
159 if (xml->name () == QString ("empty-line"))
160 {
161 ss << "<br />";
162 }
163 else if (xml->name () == QString ("p"))
164 {
165 ss << "<h1 class=\"block_c\"><span>";
166 auto xmltext = xml->readElementText ().trimmed ();
167 ss << xmltext.toStdString ();
168 ss << "</span></h1>";
169 ss << "<br />";
170 }
171 }
172
173 xml->readNext ();
174 }
175
176 auto htmlstr = templateBuild (title_template, "%s", ss.str ());
177 appendTitle (htmlstr, "application/xhtml+xml");
178 }
179 else if (xml->isStartElement () && xml->name ().toString () == "section")
180 {
181 stringstream ss;
182 string title;
183 while (!xml->atEnd ()
184 && !(xml->isEndElement ()
185 && xml->name ().toString () == "section"))
186 {
187 if (xml->isStartElement ())
188 {
189 if (xml->name ().toString () == "title")
190 {
191 auto xmltext
192 = xml->readElementText (
193 QXmlStreamReader::IncludeChildElements)
194 .trimmed ();
195 title = xmltext.toStdString ();
196 ss << "<h1 class=\"block_\"><span>";
197 ss << title;
198 ss << "</span></h1>";
199 ss << "<br />";
200 }
201 else if (xml->name ().toString () == "p")
202 {
203 ss << "<p class=\"block_1\"><span>";
204 auto xmltext = xml->readElementText ().trimmed ();
205 ss << xmltext.toStdString ();
206 ss << "</span></p>";
207 }
208 }
209
210 xml->readNext ();
211 }
212
213 auto htmlstr = templateBuild (section_template, "%s", ss.str ());
214 appendSection (title, htmlstr, "application/xhtml+xml");
215 }
216
217 xml->readNext ();
218 }
219
220 return true;
221 }
222
223 bool
224 ApvlvFB2::parseBinary (const char *content, size_t length)
225 {
226 string idstr;
227 vector<string> keys = { "FictionBook", "binary" };
228 auto optxml = xmlContentGetElement (content, length, keys);
229 if (!optxml)
230 return false;
231
232 auto xml = optxml->get ();
233 idstr = xmlStreamGetAttributeValue (xml, "id");
234
235 if (mCoverHref.empty () || idstr == mCoverHref.substr (1))
236 {
237 string mimetype = xmlStreamGetAttributeValue (xml, "content-type");
238 auto contents = xml->readElementText ().toStdString ();
239 QByteArray b64contents{ contents.c_str (),
240 (qsizetype)contents.length () };
241 auto bytes = QByteArray::fromBase64 (b64contents);
242 auto section = bytes.toStdString ();
243 appendCoverpage (section, mimetype);
244 }
245
246 return true;
247 }
248
249 void
250 ApvlvFB2::appendCoverpage (const string &section, const string &mime)
251 {
252 appendSection ("__cover__", section, mime);
253 }
254
255 void
256 ApvlvFB2::appendTitle (const string &section, const string &mime)
257 {
258 appendSection ("TITLE", section, mime);
259 }
260
261 void
262 ApvlvFB2::appendSection (const string &title, const string &section,
263 const string &mime)
264 {
265 stringstream uri;
266 uri << mPages.size ();
267 appendPage (uri.str (), title, section, mime);
268 }
269
270 void
271 ApvlvFB2::appendPage (const string &uri, const string &title,
272 const string &section, const string &mime)
273 {
274 srcPages[uri] = (int)mPages.size ();
275 mPages.push_back (uri);
276 titleSections.insert ({ uri, { title, section } });
277 srcMimeTypes.insert ({ uri, mime });
278 }
279
280 bool
281 ApvlvFB2::generateIndex ()
282 {
283 stringstream pagenum;
284
285 mIndex = { "", 0, getFilename (), FileIndexType::FILE };
286 for (int ind = 0; ind < (int)mPages.size (); ++ind)
287 {
288 pagenum << ind;
289 if (mPages[ind] == "__cover__")
290 continue;
291
292 auto title = titleSections[mPages[ind]].first;
293 auto chap = FileIndex (title, ind, pagenum.str (), FileIndexType::PAGE);
294 mIndex.mChildrenIndex.emplace_back (chap);
295 }
296
297 return true;
298 }
299
300 int
301 ApvlvFB2::sum ()
302 {
303 return (int)mPages.size ();
304 }
305
306 bool
307 ApvlvFB2::pageRenderToWebView (int pn, double zm, int rot, WebView *webview)
308 {
309 webview->setZoomFactor (zm);
310 QUrl url = QString ("apvlv:///") + QString::number (pn);
311 webview->load (url);
312 return true;
313 }
314
315 optional<QByteArray>
316 ApvlvFB2::pathContent (const string &uri)
317 {
318 if (uri == "stylesheet.css")
319 {
320 auto byte_array = QByteArray::fromStdString (stylesheet_content);
321 return byte_array;
322 }
323
324 auto byte_array = QByteArray::fromStdString (titleSections[uri].second);
325 return byte_array;
326 }
327
328 }
329
330 // Local Variables:
331 // mode: c++
332 // End: