1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiedWeb\CMSBundle\Service\PageMainContentManager; |
4
|
|
|
|
5
|
|
|
use PiedWeb\CMSBundle\Entity\PageInterface; |
6
|
|
|
use PiedWeb\CMSBundle\Service\App; |
7
|
|
|
use PiedWeb\CMSBundle\Utils\HtmlBeautifer; |
8
|
|
|
use TOC\MarkupFixer; |
9
|
|
|
use TOC\TocGenerator; |
10
|
|
|
use Twig\Environment as Twig; |
11
|
|
|
|
12
|
|
|
// TODO remove APP and remove PAGE to use it on what i want string (like in a twig extension) |
13
|
|
|
class Raw implements MainContentManagerInterface |
14
|
|
|
{ |
15
|
|
|
protected $parts = ['chapeau', 'intro', 'toc', 'content', 'postContent']; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Twig |
19
|
|
|
*/ |
20
|
|
|
protected $twig; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var App |
24
|
|
|
* Required only for markdown image render... |
25
|
|
|
* remove it and set template in page |
26
|
|
|
*/ |
27
|
|
|
protected $app; |
28
|
|
|
|
29
|
|
|
protected $page; |
30
|
|
|
protected $chapeau = ''; |
31
|
|
|
protected $intro = ''; |
32
|
|
|
protected $toc = ''; |
33
|
|
|
protected $content = ''; |
34
|
|
|
protected $postContent = ''; |
35
|
|
|
|
36
|
|
|
protected $parsed = false; |
37
|
|
|
|
38
|
|
|
public function __construct(App $app, Twig $twig, PageInterface $page) |
39
|
|
|
{ |
40
|
|
|
$this->page = $page; |
41
|
|
|
$this->app = $app->switchCurrentApp($page->getHost()); |
|
|
|
|
42
|
|
|
$this->twig = $twig; |
43
|
|
|
|
44
|
|
|
$this->parse(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function parse() |
48
|
|
|
{ |
49
|
|
|
if (true === $this->parsed) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->parseContentBeforeRendering(); |
54
|
|
|
$this->applyRendering(); |
55
|
|
|
$this->parseContentAfterRendering(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function applyRendering() |
59
|
|
|
{ |
60
|
|
|
foreach ($this->parts as $part) { |
61
|
|
|
if ($this->page->mustParseTwig()) { |
62
|
|
|
$this->$part = $this->render($this->$part); |
63
|
|
|
} |
64
|
|
|
$this->$part = HtmlBeautifer::punctuationBeautifer($this->$part); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function parseContentBeforeRendering() |
69
|
|
|
{ |
70
|
|
|
$originalContent = (string) $this->page->getMainContent(); |
71
|
|
|
|
72
|
|
|
$parsedContent = explode('<!--break-->', $originalContent, 3); |
73
|
|
|
|
74
|
|
|
$this->chapeau = isset($parsedContent[1]) ? $parsedContent[0] : ''; |
75
|
|
|
$this->postContent = $parsedContent[2] ?? ''; |
76
|
|
|
$this->content = $parsedContent[1] ?? $parsedContent[0]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function parseContentAfterRendering() |
80
|
|
|
{ |
81
|
|
|
$this->parseToc(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function parseToc() |
85
|
|
|
{ |
86
|
|
|
$this->content = (new MarkupFixer())->fix($this->content); // this work only on good html |
87
|
|
|
|
88
|
|
|
// this is a bit crazy |
89
|
|
|
$content = $this->content; |
90
|
|
|
$content = explode('<h', $content, 2); |
91
|
|
|
//var_dump($content);exit; |
92
|
|
|
if (isset($content[1])) { |
93
|
|
|
$this->intro = $content[0]; |
94
|
|
|
$this->content = '<h'.$content[1]; |
95
|
|
|
} else { |
96
|
|
|
$this->content = $content[0]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($this->page->getOtherProperty('toc')) { |
100
|
|
|
$this->toc = (new TocGenerator())->getHtmlMenu($this->content); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getBody(bool $withChapeau = false) |
105
|
|
|
{ |
106
|
|
|
$this->parse(); |
107
|
|
|
|
108
|
|
|
return ($withChapeau ? $this->chapeau : '').$this->intro.$this->content.$this->postContent; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getChapeau() |
112
|
|
|
{ |
113
|
|
|
$this->parse(); |
114
|
|
|
|
115
|
|
|
return $this->chapeau; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getContent() |
119
|
|
|
{ |
120
|
|
|
$this->parse(); |
121
|
|
|
|
122
|
|
|
return $this->content; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getPostContent() |
126
|
|
|
{ |
127
|
|
|
$this->parse(); |
128
|
|
|
|
129
|
|
|
return $this->postContent; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getIntro() |
133
|
|
|
{ |
134
|
|
|
$this->parse(); |
135
|
|
|
|
136
|
|
|
return $this->intro; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function getToc() |
140
|
|
|
{ |
141
|
|
|
$this->parse(); |
142
|
|
|
|
143
|
|
|
return $this->toc; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function convertMarkdownImage(string $body) |
147
|
|
|
{ |
148
|
|
|
preg_match_all('/(?:!\[(.*?)\]\((.*?)\))/', $body, $matches); |
149
|
|
|
|
150
|
|
|
if (!isset($matches[1])) { |
151
|
|
|
return; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$nbrMatch = \count($matches[0]); |
155
|
|
|
for ($k = 0; $k < $nbrMatch; ++$k) { |
156
|
|
|
$renderImg = $this->twig->render( |
157
|
|
|
$this->app->getTemplate('/component/_inline_image.html.twig', $this->twig), |
158
|
|
|
[ |
159
|
|
|
//"image_wrapper_class" : "mimg",' |
160
|
|
|
'image_src' => $matches[2][$k], |
161
|
|
|
'image_alt' => htmlspecialchars($matches[1][$k]), |
162
|
|
|
] |
163
|
|
|
); |
164
|
|
|
$body = str_replace($matches[0][$k], $renderImg, $body); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $body; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
protected function render($string) |
171
|
|
|
{ |
172
|
|
|
if (!$string) { |
173
|
|
|
return ''; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$tmpl = $this->twig->createTemplate(HtmlBeautifer::removeHtmlComments($string)); |
177
|
|
|
$string = $tmpl->render(['page' => $this->page]); |
178
|
|
|
|
179
|
|
|
return $string; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.