Completed
Push — master ( 612227...7394c2 )
by Dev
14:55
created

MainContentManager::getContentWithoutIntro()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
3
namespace PiedWeb\CMSBundle\Service;
4
5
use PiedWeb\CMSBundle\Entity\PageInterface;
6
use PiedWeb\CMSBundle\Service\toc\MarkupFixer;
7
use Twig\Environment as Twig;
8
9
class MainContentManager
10
{
11
    protected $page;
12
    protected $chapeau;
13
    protected $mainContent;
14
15
    public function __construct(Twig $twig, PageInterface $page)
16
    {
17
        $this->page = $page;
18
        $this->twig = $twig;
0 ignored issues
show
Bug Best Practice introduced by
The property twig does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
19
20
        $parsedContent = explode('<!--break-->', (string) $this->page->getMainContent());
21
22
        $this->chapeau = isset($parsedContent[1]) ? $parsedContent[0] : null;
23
        $this->mainContent = $parsedContent[1] ?? $parsedContent[0];
24
25
        if ($this->page->mainContentIsMarkdown()) {
26
            if ($this->chapeau) {
27
                $this->chapeau = '{% filter markdown %}'.$this->chapeau.'{% endfilter %}';
28
            }
29
            if ($this->mainContent) {
30
                $this->mainContent = self::convertMarkdownImage($this->mainContent);
31
                $this->mainContent = '{% filter markdown %}'.$this->mainContent.'{% endfilter %}';
32
            }
33
        }
34
    }
35
36
    public static function removeHtmlComments(string $content)
37
    {
38
        return preg_replace('/<!--(.|\s)*?-->/', '', $content);
39
    }
40
41
    public static function punctuationBeautifer($text)
42
    {
43
        return str_replace(
44
            [' ;', ' :', ' ?', ' !', '« ', ' »', '&laquo; ', ' &raquo;'],
45
            ['&nbsp;;', '&nbsp;:', '&nbsp;?', '&nbsp;!', '«&nbsp;', '&nbsp;»', '&laquo;&nbsp;', '&nbsp;&raquo;'],
46
            $text
47
        );
48
    }
49
50
    public static function convertMarkdownImage(string $body)
51
    {
52
        return preg_replace(
53
            '/(?:!\[(.*?)\]\((.*?)\))/',
54
            '{%'
55
            .PHP_EOL.'    include "@PiedWebCMS/component/_inline_image.html.twig" with {'
56
            .PHP_EOL.'        "image_wrapper_class" : "mimg",'
57
            .PHP_EOL.'        "image_src" : "$2",'
58
            .PHP_EOL.'        "image_alt" : "$1"'
59
            .PHP_EOL.'    } only'
60
            .PHP_EOL.'%}'.PHP_EOL,
61
            $body
62
        );
63
    }
64
65
    protected function render($string)
66
    {
67
        $string = $this->twig->createTemplate($string)->render(['page' => $this->page]); // convert to html
68
        $string = self::punctuationBeautifer($string); // small fixes on punctuation to avoid linebreak
69
        $string = self::removeHtmlComments($string);
70
71
        return $string;
72
    }
73
74
    public function getFull()
75
    {
76
        return $this->render($this->chapeau.chr(10).chr(10).$this->mainContent);
77
    }
78
79
    public function getChapeau()
80
    {
81
        return $this->render($this->chapeau);
82
    }
83
84
    public function getMainContent()
85
    {
86
        return $this->render($this->mainContent);
87
    }
88
89
    /**
90
     * Return main content until we met a title (h2, h3, ....).
91
     */
92
    public function getIntro()
93
    {
94
        $parsedContent = explode('<h', $this->getMainContent(), 2);
95
96
        return $parsedContent[1] ? $parsedContent[0] : null;
97
    }
98
99
    /**
100
     * Return Main Content without Intro.
101
     */
102
    public function getContent()
103
    {
104
        $parsedContent = explode('<h', $this->getMainContent(), 2);
105
106
        $content = $parsedContent[1] ? '<h'.$parsedContent[1] : $this->getMainContent();
107
108
        $content = (new MarkupFixer())->fix($content);
109
110
        return $content;
111
    }
112
113
    public function getToc()
114
    {
115
    }
116
}
117