-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsave_bilingual_fb2.php
211 lines (174 loc) · 7.4 KB
/
save_bilingual_fb2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Saves bilingual FB2 file with alternating paragraphs from two input TXT files.
*
* @param string $fileAddress1 Path to TXT file 1.
* @param string $fileAddress2 Path to TXT file 2.
* @param string $outputFileAddress Path to output bilingual FB2 file.
* @param string $coverAddress Path to PNG cover.
* @param string $picsFolder Path to folder with illustrations.
* @param string $srcLang ISO code of language 1.
* @param string $lang ISO code of language 2.
* @param string $id Book ID.
* @return void
*/
function saveBilingualFb2 ($fileAddress1, $fileAddress2,
$outputFileAddress = './bilingual.fb2',
$coverAddress = '', $picsFolder = '', $srcLang = '', $lang = '', $id = '') {
if (!file_exists($fileAddress1) && !file_exists($fileAddress2)) {
throw new Exception("Files $fileAddress1 and $fileAddress2 not found!");
} else if (!file_exists($fileAddress1)) {
throw new Exception("File $fileAddress1 not found!");
} else if (!file_exists($fileAddress2)) {
throw new Exception("File $fileAddress2 not found!");
}
$outputDirectory = explode('/', $outputFileAddress);
$outputDirectory = array_slice($outputDirectory, 0, count($outputDirectory) - 1);
$outputDirectory = implode('/', $outputDirectory);
if (!is_dir($outputDirectory)) {
throw new Exception("Directory $outputDirectory not found!");
}
$fileContent1 = file_get_contents($fileAddress1);
$fileContent2 = file_get_contents($fileAddress2);
$fileCoding1 = mb_detect_encoding($fileContent1, ['UTF-8', 'ASCII', 'CP1251']);
$fileCoding2 = mb_detect_encoding($fileContent2, ['UTF-8', 'ASCII', 'CP1251']);
$textArray1 = preg_split("/[\r]*[\n]+/", iconv($fileCoding1, 'UTF-8', $fileContent1));
$textArray2 = preg_split("/[\r]*[\n]+/", iconv($fileCoding2, 'UTF-8', $fileContent2));
$author1 = $textArray1[0];
$author2 = $textArray2[0];
$title1 = str_replace(['<h1>', '</h1>'], '', explode('<delimiter>', $textArray1[1])[0]);
$title2 = str_replace(['<h1>', '</h1>'], '', explode('<delimiter>', $textArray2[1])[0]);
$titleRest1 = explode('<delimiter>', $textArray1[1])[1];
$titleRest2 = explode('<delimiter>', $textArray2[1])[1];
$authorsCouple = '';
$authorTitle = '';
if ($author1 !== '<delimiter>' && $author2 !== '<delimiter>') {
$authorsCouple = "$author1 / $author2";
$authorTitle = "<p>$authorsCouple</p>";
} else if ($author1 !== '<delimiter>' && $author2 === '<delimiter>') {
$authorsCouple = $author1;
$authorTitle = "<p>$authorsCouple</p>";
} else if ($author1 === '<delimiter>' && $author2 !== '<delimiter>') {
$authorsCouple = $author2;
$authorTitle = "<p>$authorsCouple</p>";
}
$date = date('m.d.Y');
$year = date('Y');
$fbContent = <<<FB2
<?xml version="1.0" encoding="UTF-8"?>
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink">
<description>
<title-info>
<genre>antique</genre>
<author><nickname>$authorsCouple</nickname></author>
<book-title>$title1 / $title2</book-title>
<coverpage><image l:href="#cover"/></coverpage>
<lang>$lang</lang>
<src-lang>$srcLang</src-lang>
</title-info>
<document-info>
<author><nickname>$authorsCouple</nickname></author>
<program-used>B-Editor</program-used>
<date>$date</date>
<id>$id</id>
<version>1.0</version>
</document-info>
<publish-info>
<book-name>$title1 / $title2</book-name>
<publisher>Bilinguator</publisher>
<year>$year</year>
</publish-info>
</description>
<body>
<title>$authorTitle<p>$title1 / $title2</p></title>
FB2;
if ($titleRest1 !== '') {
$fbContent .= <<<FB2
<empty-line/>
<p>$titleRest1</p>
FB2;
}
if ($titleRest2 !== '') {
$fbContent .= <<<FB2
<empty-line/>
<p>$titleRest2</p>
FB2;
}
$imgCount = 0;
for ($i = 2; $i < count($textArray1); $i++) {
if (strpos($textArray1[$i], '<img') !== false && $textArray1[$i] === $textArray2[$i]) {
$imgIndex = explode('>', explode('<img', $textArray1[$i])[1])[0];
$fbContent .= <<<FB2
<empty-line/>
<p><image l:href="#$imgIndex"/></p>
FB2;
$imgCount++;
} else {
if (strpos($textArray1[$i], '<img') !== false) {
$imgIndex = explode('>', explode('<img', $textArray1[$i])[1])[0];
$fbContent .= <<<FB2
<empty-line/>
<p><image l:href="#$imgIndex"/></p>
FB2;
$imgCount++;
} else {
$textArray1[$i] = strpos($textArray1[$i], '<h1>') !== false ? $textArray1[$i] : "<p>{$textArray1[$i]}</p>";
$fbContent .= <<<FB2
<empty-line/>
$textArray1[$i]
FB2;
}
if (strpos($textArray2[$i], '<img') !== false) {
$imgIndex = explode('>', explode('<img', $textArray2[$i])[1])[0];
$fbContent .= <<<FB2
<empty-line/>
<p><image l:href="#$imgIndex"/></p>
FB2;
$imgCount++;
} else {
$textArray2[$i] = strpos($textArray2[$i], '<h1>') !== false ? $textArray2[$i] : "<p>{$textArray2[$i]}</p>";
$fbContent .= <<<FB2
<empty-line/>
$textArray2[$i]
FB2;
}
}
}
$fbContent = str_replace(['<i>', '</i>'], ['<emphasis>', '</emphasis>'], $fbContent);
$fbContent = str_replace("</h1>\n<empty-line/>\n<h1>", ' / ', $fbContent);
$fbContent = str_replace('<h1>', "</section>\n<section>\n<title>", $fbContent);
function str_replace_once ($search, $replace, $text) {
$pos = strpos($text, $search);
return $pos !== false ? substr_replace($text, $replace, $pos, strlen($search)) : $text;
}
$fbContent = str_replace_once("\n</section>", '', $fbContent);
$fbContent = str_replace('</h1>', '</title>', $fbContent);
$fbContent .= substr_count($fbContent, '<title>') > 1 ? "</section>\n" : '';
$fbContent .= "</body>\n";
$fbContent = str_replace(['<delimiter>'], ['</p><p>'], $fbContent);
$fbContent = str_replace(['<b>', '</b>'], ['<strong>', '</strong>'], $fbContent);
$fbContent = str_replace("<p></p>\n<empty-line/>\n", '', $fbContent);
$cover = '';
if (file_exists($coverAddress)) {
$cover = base64_encode(file_get_contents($coverAddress));
}
$pictures = '';
if ($imgCount > 0) {
for ($i = 1; $i <= $imgCount; $i++) {
$picAddress = "$picsFolder/$i.png";
if (file_exists($picAddress)) {
$pic = file_get_contents($picAddress);
$encodedPic = base64_encode($pic);
$pictures .= <<<FB2
<binary id="$i" content-type="image/png">$encodedPic</binary>
FB2;
}
}
}
$fbContent = preg_replace("/\n{4,6}/", str_repeat("\n", 2), $fbContent);
$fbContent .= <<<FB2
<binary id="cover" content-type="image/png">$cover</binary>
$pictures</FictionBook>
FB2;
file_put_contents($outputFileAddress, $fbContent);
}