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*/;
36 // ////////////////////////////////////////////////////////////////////////// //
42 string diskfile
; // not set by `loadBookInfo()`!
46 BookInfo
loadBookInfo (const(char)[] fname
) {
47 static BookInfo
loadFile (VFile fl
) {
49 auto sax
= new SaxyEx();
50 bool complete
= false;
51 string authorFirst
, authorLast
;
53 inout(char)[] strip (inout(char)[] s
) inout {
54 while (s
.length
&& s
.ptr
[0] <= ' ') s
= s
[1..$];
55 while (s
.length
&& s
[$-1] <= ' ') s
= s
[0..$-1];
61 sax
.onOpen("/FictionBook/description/title-info/sequence", (char[] text
, char[][string
] attrs
) {
62 if (auto sn
= "name" in attrs
) {
63 res
.seqname
= strip(*sn
).dup
;
64 if (auto id
= "number" in attrs
) {
66 res
.seqnum
= to
!uint(*id
);
67 if (res
.seqnum
< 1 || res
.seqnum
> 8192) res
.seqname
= null;
72 if (res
.seqname
.length
== 0) { res
.seqname
= null; res
.seqnum
= 0; }
75 sax
.onClose("/FictionBook/description", (char[] text
) {
77 throw new Exception("done"); // sorry
80 // author's first name
81 sax
.onContent("/FictionBook/description/title-info/author/first-name", (char[] text
) {
82 authorFirst
= strip(text
).idup
;
85 sax
.onContent("/FictionBook/description/title-info/author/last-name", (char[] text
) {
86 authorLast
= strip(text
).idup
;
89 sax
.onContent("/FictionBook/description/title-info/book-title", (char[] text
) {
90 res
.title
= strip(text
).idup
;
94 } catch (Exception e
) {
95 if (!complete
) throw e
;
97 if (authorFirst
.length
== 0) {
98 authorFirst
= authorLast
;
100 } else if (authorLast
.length
!= 0) {
101 authorFirst
~= " "~authorLast
;
103 if (authorFirst
.length
== 0 && res
.title
.length
== 0) throw new Exception("no book title found");
104 res
.author
= authorFirst
;
108 import std
.path
: extension
;
109 if (strEquCI(fname
.extension
, ".zip")) {
111 scope(exit
) vfsRemovePack(fname
);
112 foreach (immutable idx
, ref de; vfsFileList
) {
113 if (strEquCI(de.name
.extension
, ".fb2")) return loadFile(vfsOpenFile(de.name
));
115 throw new Exception("no fb2 file found in '"~fname
.idup
~"'");
117 return loadFile(vfsOpenFile(fname
));
122 // ////////////////////////////////////////////////////////////////////////// //
124 string name
; // empty: text node
133 @property inout(Tag
) firstChild () inout pure nothrow @trusted @nogc { pragma(inline
, true); return (children
.length ? children
.ptr
[0] : null); }
134 @property inout(Tag
) lastChild () inout pure nothrow @trusted @nogc { pragma(inline
, true); return (children
.length ? children
[$-1] : null); }
136 string
toStringNice (int indent
=0) const {
138 void addIndent () { foreach (immutable _
; 0..indent
) res
~= ' '; }
139 void newLine () { res
~= '\n'; foreach (immutable _
; 0..indent
) res
~= ' '; }
140 if (name
.length
== 0) return text
;
142 if (children
.length
== 0) res
~= '/';
144 if (id
.length
) { res
~= " id="; res
~= id
; }
145 if (href
.length
) { res
~= " href="; res
~= href
; }
147 if (children
.length
) {
148 if (indent
>= 0) indent
+= 2;
149 foreach (const Tag cc
; children
) {
150 if (indent
>= 0) newLine();
151 res
~= cc
.toStringNice(indent
);
153 if (indent
>= 0) indent
-= 2;
154 if (indent
>= 0) newLine();
162 override string
toString () const { return toStringNice(int.min
); }
166 // ////////////////////////////////////////////////////////////////////////// //
167 final class BookText
{
176 this (const(char)[] fname
) { loadFile(fname
); }
177 this (VFile fl
) { loadFile(fl
); }
180 void loadFile (VFile fl
) {
181 auto sax
= new SaxyEx();
184 sax
.onOpen("/FictionBook/description/title-info/sequence", (char[] text
, char[][string
] attrs
) {
185 if (auto sn
= "name" in attrs
) {
186 sequence
= (*sn
).dup
;
187 if (auto id
= "number" in attrs
) {
188 import std
.conv
: to
;
189 seqnum
= to
!uint(*id
);
190 if (seqnum
< 1 || seqnum
> 8192) sequence
= null;
195 if (sequence
.length
== 0) { sequence
= null; seqnum
= 0; }
198 // author's first name
199 sax
.onContent("/FictionBook/description/title-info/author/first-name", (char[] text
) {
200 authorFirst
= text
.xstrip
.idup
;
202 // author's last name
203 sax
.onContent("/FictionBook/description/title-info/author/last-name", (char[] text
) {
204 authorLast
= text
.xstrip
.idup
;
207 sax
.onContent("/FictionBook/description/title-info/book-title", (char[] text
) {
208 title
= text
.xstrip
.idup
;
213 sax
.onOpen("/FictionBook/body", (char[] text
) {
214 if (content
is null) {
216 content
.name
= "body";
221 sax
.onClose("/FictionBook/body", (char[] text
) {
222 if (content
is null) assert(0, "wtf?!");
223 tagStack
.length
-= 1;
224 tagStack
.assumeSafeAppend
;
227 sax
.onOpen("/FictionBook/body/+", (char[] text
, char[][string
] attrs
) {
228 auto tag
= new Tag();
229 tag
.name
= text
.idup
;
230 tag
.parent
= tagStack
[$-1];
231 if (auto ls
= tag
.parent
.lastChild
) {
232 ls
.nextSibling
= tag
;
233 tag
.prevSibling
= ls
;
235 tag
.parent
.children
~= tag
;
236 if (auto p
= "id" in attrs
) tag
.id
= (*p
).xstrip
.idup
;
237 if (auto p
= "href" in attrs
) tag
.href
= (*p
).xstrip
.idup
;
241 sax
.onClose("/FictionBook/body/+", (char[] text
) {
242 tagStack
.length
-= 1;
243 tagStack
.assumeSafeAppend
;
246 sax
.onContent("/FictionBook/body/+", (char[] text
) {
247 auto tag
= new Tag();
249 tag
.parent
= tagStack
[$-1];
250 if (auto ls
= tag
.parent
.lastChild
) {
251 ls
.nextSibling
= tag
;
252 tag
.prevSibling
= ls
;
254 tag
.parent
.children
~= tag
;
255 tag
.text
~= text
.idup
;
260 if (content
is null) {
262 content
.name
= "body";
266 void loadFile (const(char)[] fname
) {
267 import std
.path
: extension
;
268 if (strEquCI(fname
.extension
, ".zip")) {
270 scope(exit
) vfsRemovePack(fname
);
271 foreach (immutable idx
, ref de; vfsFileList
) {
272 if (strEquCI(de.name
.extension
, ".fb2")) { loadFile(vfsOpenFile(de.name
)); return; }
274 throw new Exception("no fb2 file found in '"~fname
.idup
~"'");
276 loadFile(vfsOpenFile(fname
));