|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Cecil. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Arnaud Ligny <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Cecil\Generator; |
|
15
|
|
|
|
|
16
|
|
|
use Cecil\Collection\Page\Page; |
|
17
|
|
|
use Cecil\Converter\Converter; |
|
18
|
|
|
use Cecil\Exception\RuntimeException; |
|
19
|
|
|
use Cecil\Util; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* ExternalBody generator class. |
|
23
|
|
|
* |
|
24
|
|
|
* This class is responsible for generating the body of pages |
|
25
|
|
|
* by fetching content from external sources specified in the 'external' variable of each page. |
|
26
|
|
|
* It reads the content from the specified URL, converts it to HTML using the Converter, |
|
27
|
|
|
* and sets the resulting HTML as the body of the page. |
|
28
|
|
|
* If an error occurs while fetching the content, it logs the error message. |
|
29
|
|
|
*/ |
|
30
|
|
|
class ExternalBody extends AbstractGenerator implements GeneratorInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public function generate(): void |
|
36
|
|
|
{ |
|
37
|
1 |
|
$filteredPages = $this->builder->getPages()->filter(function (Page $page) { |
|
38
|
1 |
|
return $page->getVariable('external') !== null; |
|
39
|
1 |
|
}); |
|
40
|
|
|
|
|
41
|
|
|
/** @var Page $page */ |
|
42
|
1 |
|
foreach ($filteredPages as $page) { |
|
43
|
|
|
try { |
|
44
|
1 |
|
$pageContent = Util\File::fileGetContents($page->getVariable('external')); |
|
45
|
1 |
|
if ($pageContent === false) { |
|
46
|
1 |
|
throw new RuntimeException(\sprintf('Unable to get external contents from "%s".', $page->getVariable('external'))); |
|
47
|
|
|
} |
|
48
|
1 |
|
$html = (new Converter($this->builder))->convertBody($pageContent); |
|
49
|
1 |
|
$page->setBodyHtml($html); |
|
50
|
|
|
|
|
51
|
1 |
|
$this->generatedPages->add($page); |
|
52
|
1 |
|
} catch (\Exception $e) { |
|
53
|
1 |
|
$message = \sprintf('Error in "%s": %s', $page->getFilePath(), $e->getMessage()); |
|
54
|
1 |
|
$this->builder->getLogger()->error($message); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|