cosmetix
[xreader.git] / booktext.d
blob9f1aed76a614063c64cd081ba698f9f0b70dcc0f
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*/;
19 import std.encoding;
20 import std.stdio;
22 import arsd.color;
23 import arsd.png;
24 import arsd.jpeg;
26 import iv.encoding;
27 import iv.nanovg;
28 import iv.saxy;
29 import iv.strex;
30 import iv.utfutil;
31 import iv.vfs;
33 import xlayouter;
36 // ////////////////////////////////////////////////////////////////////////// //
37 struct BookInfo {
38 string author;
39 string title;
40 string seqname;
41 uint seqnum;
42 string diskfile; // not set by `loadBookInfo()`!
46 BookInfo loadBookInfo (const(char)[] fname) {
47 static BookInfo loadFile (VFile fl) {
48 BookInfo res;
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];
56 return s;
59 try {
60 // sequence tag
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) {
65 import std.conv : to;
66 res.seqnum = to!uint(*id);
67 if (res.seqnum < 1 || res.seqnum > 8192) res.seqname = null;
69 } else {
70 res.seqname = null;
72 if (res.seqname.length == 0) { res.seqname = null; res.seqnum = 0; }
73 });
75 sax.onClose("/FictionBook/description", (char[] text) {
76 complete = true;
77 throw new Exception("done"); // sorry
78 });
80 // author's first name
81 sax.onContent("/FictionBook/description/title-info/author/first-name", (char[] text) {
82 authorFirst = strip(text).idup;
83 });
84 // author's last name
85 sax.onContent("/FictionBook/description/title-info/author/last-name", (char[] text) {
86 authorLast = strip(text).idup;
87 });
88 // book title
89 sax.onContent("/FictionBook/description/title-info/book-title", (char[] text) {
90 res.title = strip(text).idup;
91 });
93 sax.loadStream(fl);
94 } catch (Exception e) {
95 if (!complete) throw e;
97 if (authorFirst.length == 0) {
98 authorFirst = authorLast;
99 authorLast = null;
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;
105 return res;
108 import std.path : extension;
109 if (strEquCI(fname.extension, ".zip")) {
110 vfsAddPak(fname);
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~"'");
116 } else {
117 return loadFile(vfsOpenFile(fname));
122 // ////////////////////////////////////////////////////////////////////////// //
123 final class Tag {
124 string name; // empty: text node
125 string id;
126 string href;
127 string text;
128 Tag[] children;
129 Tag parent;
130 Tag prevSibling;
131 Tag nextSibling;
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 {
137 string res;
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;
141 res ~= '<';
142 if (children.length == 0) res ~= '/';
143 res ~= name;
144 if (id.length) { res ~= " id="; res ~= id; }
145 if (href.length) { res ~= " href="; res ~= href; }
146 res ~= '>';
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();
155 res ~= "</";
156 res ~= name;
157 res ~= ">";
159 return res;
162 override string toString () const { return toStringNice(int.min); }
166 // ////////////////////////////////////////////////////////////////////////// //
167 final class BookText {
168 string authorFirst;
169 string authorLast;
170 string title;
171 string sequence;
172 uint seqnum;
174 Tag content; // body
176 this (const(char)[] fname) { loadFile(fname); }
177 this (VFile fl) { loadFile(fl); }
179 private:
180 void loadFile (VFile fl) {
181 auto sax = new SaxyEx();
183 // sequence tag
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;
192 } else {
193 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;
206 // book title
207 sax.onContent("/FictionBook/description/title-info/book-title", (char[] text) {
208 title = text.xstrip.idup;
211 Tag[] tagStack;
213 sax.onOpen("/FictionBook/body", (char[] text) {
214 if (content is null) {
215 content = new Tag();
216 content.name = "body";
218 tagStack ~= content;
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;
238 tagStack ~= tag;
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();
248 tag.name = null;
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;
258 sax.loadStream(fl);
260 if (content is null) {
261 content = new Tag();
262 content.name = "body";
266 void loadFile (const(char)[] fname) {
267 import std.path : extension;
268 if (strEquCI(fname.extension, ".zip")) {
269 vfsAddPak(fname);
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~"'");
275 } else {
276 loadFile(vfsOpenFile(fname));