|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiedWeb\CMSBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use PiedWeb\CMSBundle\Entity\PageInterface; |
|
6
|
|
|
use TOC\MarkupFixer; |
|
7
|
|
|
use TOC\TocGenerator; |
|
8
|
|
|
use Twig\Environment as Twig; |
|
9
|
|
|
|
|
10
|
|
|
class MainContentManager |
|
11
|
|
|
{ |
|
12
|
|
|
protected $page; |
|
13
|
|
|
protected $chapeau; |
|
14
|
|
|
protected $mainContent; |
|
15
|
|
|
protected $postContent; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(Twig $twig, PageInterface $page) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->page = $page; |
|
20
|
|
|
$this->twig = $twig; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
$parsedContent = explode('<!--break-->', (string) $this->page->getMainContent()); |
|
23
|
|
|
|
|
24
|
|
|
$this->chapeau = isset($parsedContent[1]) ? $parsedContent[0] : null; |
|
25
|
|
|
$this->mainContent = $parsedContent[1] ?? $parsedContent[0]; |
|
26
|
|
|
|
|
27
|
|
|
if ($this->page->mainContentIsMarkdown()) { |
|
28
|
|
|
if ($this->chapeau) { |
|
29
|
|
|
$this->chapeau = '{% filter markdown %}'.$this->chapeau.'{% endfilter %}'; |
|
30
|
|
|
} |
|
31
|
|
|
if ($this->mainContent) { |
|
32
|
|
|
$this->mainContent = self::convertMarkdownImage($this->mainContent); |
|
33
|
|
|
$this->mainContent = '{% filter markdown %}'.$this->mainContent.'{% endfilter %}'; |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public static function removeHtmlComments(string $content) |
|
39
|
|
|
{ |
|
40
|
|
|
return preg_replace('/<!--(.|\s)*?-->/', '', $content); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function punctuationBeautifer($text) |
|
44
|
|
|
{ |
|
45
|
|
|
return str_replace( |
|
46
|
|
|
[' ;', ' :', ' ?', ' !', '« ', ' »', '« ', ' »'], |
|
47
|
|
|
[' ;', ' :', ' ?', ' !', '« ', ' »', '« ', ' »'], |
|
48
|
|
|
$text |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public static function convertMarkdownImage(string $body) |
|
53
|
|
|
{ |
|
54
|
|
|
return preg_replace( |
|
55
|
|
|
'/(?:!\[(.*?)\]\((.*?)\))/', |
|
56
|
|
|
'{%' |
|
57
|
|
|
.PHP_EOL.' include "@PiedWebCMS/component/_inline_image.html.twig" with {' |
|
58
|
|
|
.PHP_EOL.' "image_wrapper_class" : "mimg",' |
|
59
|
|
|
.PHP_EOL.' "image_src" : "$2",' |
|
60
|
|
|
.PHP_EOL.' "image_alt" : "$1"' |
|
61
|
|
|
.PHP_EOL.' } only' |
|
62
|
|
|
.PHP_EOL.'%}'.PHP_EOL, |
|
63
|
|
|
$body |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected function render($string) |
|
68
|
|
|
{ |
|
69
|
|
|
$string = $this->twig->createTemplate($string)->render(['page' => $this->page]); // convert to html |
|
70
|
|
|
$string = self::punctuationBeautifer($string); // small fixes on punctuation to avoid linebreak |
|
71
|
|
|
$string = self::removeHtmlComments($string); |
|
72
|
|
|
|
|
73
|
|
|
return $string; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getFull() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->render($this->chapeau.chr(10).chr(10).$this->mainContent); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getChapeau() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->render($this->chapeau); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getMainContent() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->render($this->mainContent); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Return main content until we met a title (h2, h3, ....). |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getIntro() |
|
95
|
|
|
{ |
|
96
|
|
|
$parsedContent = explode('<h', $this->getMainContent(), 2); |
|
97
|
|
|
|
|
98
|
|
|
return $parsedContent[1] ? $parsedContent[0] : null; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Return Main Content without Intro. |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getContent() |
|
105
|
|
|
{ |
|
106
|
|
|
$parsedContent = explode('<h', $this->getMainContent(), 2); |
|
107
|
|
|
$content = $parsedContent[1] ? '<h'.$parsedContent[1] : $this->getMainContent(); |
|
108
|
|
|
|
|
109
|
|
|
$content = (new MarkupFixer())->fix($content); |
|
110
|
|
|
|
|
111
|
|
|
$parsedContent = explode('<div class="fullwidth', $content, 2); |
|
112
|
|
|
$content = $parsedContent[1] ? $parsedContent[0] : $content; |
|
113
|
|
|
$this->postContent = '<div class="fullwidth'.$parsedContent[1]; |
|
114
|
|
|
|
|
115
|
|
|
return $content; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function getToc() |
|
119
|
|
|
{ |
|
120
|
|
|
$menu = (new TocGenerator())->getHtmlMenu($this->getContent().$this->postContent); |
|
121
|
|
|
|
|
122
|
|
|
return $menu; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function getPostContent() |
|
126
|
|
|
{ |
|
127
|
|
|
$this->getContent(); |
|
128
|
|
|
|
|
129
|
|
|
return $this->postContent; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|