1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staticka; |
4
|
|
|
|
5
|
|
|
use Staticka\Contracts\BuilderContract; |
6
|
|
|
use Staticka\Contracts\LayoutContract; |
7
|
|
|
use Staticka\Contracts\PageContract; |
8
|
|
|
use Staticka\Contracts\RendererContract; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Builder |
12
|
|
|
* |
13
|
|
|
* @package Staticka |
14
|
|
|
* @author Rougin Gutib <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Builder extends \Parsedown implements BuilderContract |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \Staticka\Contracts\RendererContract |
20
|
|
|
*/ |
21
|
|
|
protected $renderer; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param \Staticka\Contracts\RendererContract $renderer |
25
|
|
|
*/ |
26
|
27 |
|
public function __construct(RendererContract $renderer) |
27
|
|
|
{ |
28
|
27 |
|
$this->renderer = $renderer; |
29
|
27 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Builds the HTML of the page instance. |
33
|
|
|
* |
34
|
|
|
* @param \Staticka\Contracts\PageContract $page |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
18 |
|
public function build(PageContract $page) |
38
|
|
|
{ |
39
|
18 |
|
$data = array_merge($page->helpers(), $page->data()); |
40
|
|
|
|
41
|
18 |
|
$body = (string) $data[PageContract::DATA_BODY]; |
42
|
|
|
|
43
|
18 |
|
$data[PageContract::DATA_BODY] = $this->text($body); |
44
|
|
|
|
45
|
|
|
// TODO: Remove this on v1.0.0. |
46
|
|
|
// Use DATA_BODY instead of "content". |
47
|
18 |
|
$data['content'] = $data[PageContract::DATA_BODY]; |
48
|
|
|
|
49
|
18 |
|
if (! isset($data[PageContract::DATA_TITLE])) |
50
|
6 |
|
{ |
51
|
18 |
|
$output = $data[PageContract::DATA_BODY]; |
52
|
|
|
|
53
|
18 |
|
preg_match('/<h1>(.*?)<\/h1>/', $output, $matches); |
54
|
|
|
|
55
|
18 |
|
if (isset($matches[1])) |
56
|
6 |
|
{ |
57
|
18 |
|
$data[PageContract::DATA_TITLE] = $matches[1]; |
58
|
6 |
|
} |
59
|
6 |
|
} |
60
|
|
|
|
61
|
18 |
|
if (isset($data[PageContract::DATA_PLATE])) |
62
|
6 |
|
{ |
63
|
3 |
|
$plate = (string) $data[PageContract::DATA_PLATE]; |
64
|
|
|
|
65
|
3 |
|
$html = $this->renderer->render($plate, $data); |
66
|
1 |
|
} |
67
|
|
|
else |
68
|
|
|
{ |
69
|
15 |
|
$body = $data[PageContract::DATA_BODY]; |
70
|
|
|
|
71
|
15 |
|
$base = $page->layout()->body(); |
72
|
|
|
|
73
|
15 |
|
$search = LayoutContract::BODY_DEFAULT; |
74
|
|
|
|
75
|
15 |
|
$html = str_replace($search, $body, $base); |
76
|
|
|
} |
77
|
|
|
|
78
|
18 |
|
foreach ($page->filters() as $filter) |
79
|
|
|
{ |
80
|
6 |
|
$html = $filter->filter($html); |
81
|
6 |
|
} |
82
|
|
|
|
83
|
18 |
|
return (string) $html; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|