-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev.php
executable file
·42 lines (37 loc) · 1.07 KB
/
dev.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
#!/usr/bin/env php
<?php
require __DIR__.'/autoload.php';
$urls = require __DIR__.'/data/urls.php';
$tmpDir = __DIR__.'/tmp';
if (!file_exists($tmpDir)) {
mkdir($tmpDir, 0775, true);
}
try {
run($urls, $tmpDir);
} catch (\Chitanka\Converter\XmlIsInvalid $e) {
echo $e->getFormattedErrors();
exit(1);
}
function run(array $urls, string $tmpDir) {
foreach ($urls as $url) {
echo '==> Converting ', $url, "\n";
$htmlFile = fetchUrl($url, $tmpDir);
echo " File stored in {$htmlFile}\n";
$sfb = test($htmlFile, $tmpDir);
file_put_contents($htmlFile.'.sfb', $sfb);
}
}
function fetchUrl(string $url, string $tmpDir) {
$tmpHtmlFile = $tmpDir.'/'.basename($url).'.html';
if (file_exists($tmpHtmlFile)) {
return $tmpHtmlFile;
}
$html = file_get_contents($url);
file_put_contents($tmpHtmlFile, $html);
return $tmpHtmlFile;
}
function test(string $htmlFile, string $tmpDir) {
$xml = (new \Chitanka\Converter\Lexbg\HtmlToXmlConverter())->convert(file_get_contents($htmlFile));
$sfb = (new \Chitanka\Converter\Lexbg\XmlToSfbConverter($tmpDir))->convert($xml);
return $sfb;
}