1 /* Written by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
2 * Understanding is not required. Only obedience.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 module booktext
/*is aliced*/;
34 // ////////////////////////////////////////////////////////////////////////// //
40 string diskfile
; // not set by `loadBookInfo()`!
44 BookInfo
loadBookInfo (const(char)[] fname
) {
45 static BookInfo
loadFile (VFile fl
) {
47 auto sax
= new SaxyEx();
48 bool complete
= false;
49 string authorFirst
, authorLast
;
51 inout(char)[] strip (inout(char)[] s
) inout {
52 while (s
.length
&& s
.ptr
[0] <= ' ') s
= s
[1..$];
53 while (s
.length
&& s
[$-1] <= ' ') s
= s
[0..$-1];
59 sax
.onOpen("/FictionBook/description/title-info/sequence", (char[] text
, char[][string
] attrs
) {
60 if (auto sn
= "name" in attrs
) {
61 res
.seqname
= strip(*sn
).dup
;
62 if (auto id
= "number" in attrs
) {
64 res
.seqnum
= to
!uint(*id
);
65 if (res
.seqnum
< 1 || res
.seqnum
> 8192) res
.seqname
= null;
70 if (res
.seqname
.length
== 0) { res
.seqname
= null; res
.seqnum
= 0; }
73 sax
.onClose("/FictionBook/description", (char[] text
) {
75 throw new Exception("done"); // sorry
78 // author's first name
79 sax
.onContent("/FictionBook/description/title-info/author/first-name", (char[] text
) {
80 authorFirst
= strip(text
).idup
;
83 sax
.onContent("/FictionBook/description/title-info/author/last-name", (char[] text
) {
84 authorLast
= strip(text
).idup
;
87 sax
.onContent("/FictionBook/description/title-info/book-title", (char[] text
) {
88 res
.title
= strip(text
).idup
;
92 } catch (Exception e
) {
93 if (!complete
) throw e
;
95 if (authorFirst
.length
== 0) {
96 authorFirst
= authorLast
;
98 } else if (authorLast
.length
!= 0) {
99 authorFirst
~= " "~authorLast
;
101 if (authorFirst
.length
== 0 && res
.title
.length
== 0) throw new Exception("no book title found");
102 res
.author
= authorFirst
;
106 import std
.path
: extension
;
107 if (strEquCI(fname
.extension
, ".zip")) {
108 auto did
= vfsAddPak
!"first"(fname
);
109 scope(exit
) vfsRemovePack(did
);
110 foreach (immutable idx
, ref de; vfsFileList
) {
111 if (strEquCI(de.name
.extension
, ".fb2")) return loadFile(vfsOpenFile(de.name
));
113 throw new Exception("no fb2 file found in '"~fname
.idup
~"'");
115 return loadFile(vfsOpenFile(fname
));
120 // ////////////////////////////////////////////////////////////////////////// //
122 string name
; // empty: text node
131 @property inout(Tag
) firstChild () inout pure nothrow @trusted @nogc { pragma(inline
, true); return (children
.length ? children
.ptr
[0] : null); }
132 @property inout(Tag
) lastChild () inout pure nothrow @trusted @nogc { pragma(inline
, true); return (children
.length ? children
[$-1] : null); }
134 string
textContent () const {
135 if (name
.length
== 0) return text
;
137 foreach (const Tag t
; children
) res
~= t
.textContent
;
141 string
toStringNice (int indent
=0) const {
143 void addIndent () { foreach (immutable _
; 0..indent
) res
~= ' '; }
144 void newLine () { res
~= '\n'; foreach (immutable _
; 0..indent
) res
~= ' '; }
145 if (name
.length
== 0) return text
;
147 if (children
.length
== 0) res
~= '/';
149 if (id
.length
) { res
~= " id="; res
~= id
; }
150 if (href
.length
) { res
~= " href="; res
~= href
; }
152 if (children
.length
) {
153 if (indent
>= 0) indent
+= 2;
154 foreach (const Tag cc
; children
) {
155 if (indent
>= 0) newLine();
156 res
~= cc
.toStringNice(indent
);
158 if (indent
>= 0) indent
-= 2;
159 if (indent
>= 0) newLine();
167 override string
toString () const { return toStringNice(int.min
); }
171 // ////////////////////////////////////////////////////////////////////////// //
172 final class BookText
{
179 static struct Image
{
188 this (const(char)[] fname
) { loadFile(fname
); }
189 this (VFile fl
) { loadFile(fl
); }
192 void loadFile (VFile fl
) {
193 auto sax
= new SaxyEx();
195 string
norm (const(char)[] s
) {
197 foreach (char ch
; s
) {
198 if (ch
<= ' ' || ch
== 127) {
199 if (res
.length
&& res
[$-1] > ' ') res
~= ch
;
208 sax
.onOpen("/FictionBook/description/title-info/sequence", (char[] text
, char[][string
] attrs
) {
209 if (auto sn
= "name" in attrs
) {
210 sequence
= (*sn
).dup
;
211 if (auto id
= "number" in attrs
) {
212 import std
.conv
: to
;
213 seqnum
= to
!uint(*id
);
214 if (seqnum
< 1 || seqnum
> 8192) sequence
= null;
219 if (sequence
.length
== 0) { sequence
= null; seqnum
= 0; }
222 // author's first name
223 sax
.onContent("/FictionBook/description/title-info/author/first-name", (char[] text
) {
224 authorFirst
= norm(text
);
226 // author's last name
227 sax
.onContent("/FictionBook/description/title-info/author/last-name", (char[] text
) {
228 authorLast
= norm(text
);
231 sax
.onContent("/FictionBook/description/title-info/book-title", (char[] text
) {
239 sax
.onOpen("/FictionBook/binary", (char[] text
, char[][string
] attrs
) {
241 if (auto ctp
= "content-type" in attrs
) {
242 if (*ctp
== "image/png" ||
*ctp
== "image/jpeg") {
243 if (auto idp
= "id" in attrs
) {
244 imageid
= (*idp
).idup
;
245 if (imageid
.length
== 0) {
246 writeln("image without id");
248 imagefmt
= (*ctp
).idup
;
252 writeln("unknown binary content format: '", *ctp
, "'");
256 sax
.onContent("/FictionBook/binary", (char[] text
) {
257 if (imageid
.length
) {
258 foreach (char ch
; text
) if (ch
> ' ') imagectx
~= ch
;
261 sax
.onClose("/FictionBook/binary", (char[] text
) {
263 if (imageid
.length
) {
264 //writeln("image with id '", imageid, "'...");
266 if (imagectx
.length
== 0) {
267 writeln("image with id '", imageid
, "' has no data");
270 auto imgdata
= Base64
.decode(imagectx
);
272 if (imagefmt
== "image/jpeg") {
273 img
= readJpegFromMemory(imgdata
[]).getAsTrueColorImage
;
274 } else if (imagefmt
== "image/png") {
275 img
= imageFromPng(readPng(imgdata
[])).getAsTrueColorImage
;
279 if (img
.width
> 1 && img
.height
> 1) {
280 if (img
.width
> 1024 || img
.height
> 1024) {
282 float scl
= 1024.0f/(img
.width
> img
.height ? img
.width
: img
.height
);
283 int nw
= cast(int)(img
.width
*scl
);
284 int nh
= cast(int)(img
.height
*scl
);
287 img
= imageResample(img
, nw
, nh
, RESAMPLER_DEFAULT_FILTER
);
290 images
~= Image(imageid
, img
);
291 //writePng("z_"~imageid~".png", images[$-1].img);
292 } catch (Exception e
) {
293 writeln("image with id '", imageid
, "' has invalid data");
294 writeln("ERROR: ", e
.msg
);
305 sax
.onOpen("/FictionBook/body", (char[] text
) {
306 if (content
is null) {
308 content
.name
= "body";
313 sax
.onClose("/FictionBook/body", (char[] text
) {
314 if (content
is null) assert(0, "wtf?!");
315 tagStack
.length
-= 1;
316 tagStack
.assumeSafeAppend
;
319 sax
.onOpen("/FictionBook/body/+", (char[] text
, char[][string
] attrs
) {
320 auto tag
= new Tag();
321 tag
.name
= text
.idup
;
322 tag
.parent
= tagStack
[$-1];
324 if (auto ls = tag.parent.lastChild) {
325 ls.nextSibling = tag;
326 tag.prevSibling = ls;
329 tag
.parent
.children
~= tag
;
330 if (auto p
= "id" in attrs
) tag
.id
= norm(*p
);
331 if (auto p
= "href" in attrs
) tag
.href
= norm(*p
);
332 else if (auto p
= "l:href" in attrs
) tag
.href
= norm(*p
);
334 if (tag.name == "image") {
336 writeln("IMAGE: ", tag.href);
342 sax
.onClose("/FictionBook/body/+", (char[] text
) {
343 tagStack
.length
-= 1;
344 tagStack
.assumeSafeAppend
;
347 sax
.onContent("/FictionBook/body/+", (char[] text
) {
348 auto tag
= new Tag();
350 tag
.parent
= tagStack
[$-1];
352 if (auto ls = tag.parent.lastChild) {
353 ls.nextSibling = tag;
354 tag.prevSibling = ls;
357 tag
.parent
.children
~= tag
;
358 tag
.text
~= text
.idup
;
363 if (content
is null) {
365 content
.name
= "body";
369 void loadFile (const(char)[] fname
) {
370 import std
.path
: extension
;
371 if (strEquCI(fname
.extension
, ".zip")) {
372 auto did
= vfsAddPak
!"first"(fname
);
373 scope(exit
) vfsRemovePack(did
);
374 foreach (immutable idx
, ref de; vfsFileList
) {
375 if (strEquCI(de.name
.extension
, ".fb2")) { loadFile(vfsOpenFile(de.name
)); return; }
377 throw new Exception("no fb2 file found in '"~fname
.idup
~"'");
379 loadFile(vfsOpenFile(fname
));