some more code for soft hypens
[xreader.git] / booktext.d
blobb409f4ff2e834f331759fe338cf120033e5f6102
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 textContent () const {
137 if (name.length == 0) return text;
138 string res;
139 foreach (const Tag t; children) res ~= t.textContent;
140 return res;
143 string toStringNice (int indent=0) const {
144 string res;
145 void addIndent () { foreach (immutable _; 0..indent) res ~= ' '; }
146 void newLine () { res ~= '\n'; foreach (immutable _; 0..indent) res ~= ' '; }
147 if (name.length == 0) return text;
148 res ~= '<';
149 if (children.length == 0) res ~= '/';
150 res ~= name;
151 if (id.length) { res ~= " id="; res ~= id; }
152 if (href.length) { res ~= " href="; res ~= href; }
153 res ~= '>';
154 if (children.length) {
155 if (indent >= 0) indent += 2;
156 foreach (const Tag cc; children) {
157 if (indent >= 0) newLine();
158 res ~= cc.toStringNice(indent);
160 if (indent >= 0) indent -= 2;
161 if (indent >= 0) newLine();
162 res ~= "</";
163 res ~= name;
164 res ~= ">";
166 return res;
169 override string toString () const { return toStringNice(int.min); }
173 // ////////////////////////////////////////////////////////////////////////// //
174 final class BookText {
175 string authorFirst;
176 string authorLast;
177 string title;
178 string sequence;
179 uint seqnum;
181 Tag content; // body
183 this (const(char)[] fname) { loadFile(fname); }
184 this (VFile fl) { loadFile(fl); }
186 private:
187 void loadFile (VFile fl) {
188 auto sax = new SaxyEx();
190 string norm (const(char)[] s) {
191 string res;
192 foreach (char ch; s) {
193 if (ch <= ' ' || ch == 127) {
194 if (res.length && res[$-1] > ' ') res ~= ch;
195 } else {
196 res ~= ch;
199 return res;
202 // sequence tag
203 sax.onOpen("/FictionBook/description/title-info/sequence", (char[] text, char[][string] attrs) {
204 if (auto sn = "name" in attrs) {
205 sequence = (*sn).dup;
206 if (auto id = "number" in attrs) {
207 import std.conv : to;
208 seqnum = to!uint(*id);
209 if (seqnum < 1 || seqnum > 8192) sequence = null;
211 } else {
212 sequence = null;
214 if (sequence.length == 0) { sequence = null; seqnum = 0; }
217 // author's first name
218 sax.onContent("/FictionBook/description/title-info/author/first-name", (char[] text) {
219 authorFirst = norm(text);
221 // author's last name
222 sax.onContent("/FictionBook/description/title-info/author/last-name", (char[] text) {
223 authorLast = norm(text);
225 // book title
226 sax.onContent("/FictionBook/description/title-info/book-title", (char[] text) {
227 title = norm(text);
230 Tag[] tagStack;
232 sax.onOpen("/FictionBook/body", (char[] text) {
233 if (content is null) {
234 content = new Tag();
235 content.name = "body";
237 tagStack ~= content;
240 sax.onClose("/FictionBook/body", (char[] text) {
241 if (content is null) assert(0, "wtf?!");
242 tagStack.length -= 1;
243 tagStack.assumeSafeAppend;
246 sax.onOpen("/FictionBook/body/+", (char[] text, char[][string] attrs) {
247 auto tag = new Tag();
248 tag.name = text.idup;
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 if (auto p = "id" in attrs) tag.id = norm(*p);
256 if (auto p = "href" in attrs) tag.href = norm(*p);
257 tagStack ~= tag;
260 sax.onClose("/FictionBook/body/+", (char[] text) {
261 tagStack.length -= 1;
262 tagStack.assumeSafeAppend;
265 sax.onContent("/FictionBook/body/+", (char[] text) {
266 auto tag = new Tag();
267 tag.name = null;
268 tag.parent = tagStack[$-1];
269 if (auto ls = tag.parent.lastChild) {
270 ls.nextSibling = tag;
271 tag.prevSibling = ls;
273 tag.parent.children ~= tag;
274 tag.text ~= text.idup;
277 sax.loadStream(fl);
279 if (content is null) {
280 content = new Tag();
281 content.name = "body";
285 void loadFile (const(char)[] fname) {
286 import std.path : extension;
287 if (strEquCI(fname.extension, ".zip")) {
288 vfsAddPak(fname);
289 scope(exit) vfsRemovePack(fname);
290 foreach (immutable idx, ref de; vfsFileList) {
291 if (strEquCI(de.name.extension, ".fb2")) { loadFile(vfsOpenFile(de.name)); return; }
293 throw new Exception("no fb2 file found in '"~fname.idup~"'");
294 } else {
295 loadFile(vfsOpenFile(fname));