Completed
Push — master ( 1bbcf8...109a6d )
by Peter
26:01
created

AppExtension::markdownToHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace AppBundle\Twig;
4
5
use Mni\FrontYAML\Parser;
6
7
/**
8
 * Application Twig extension which adds 'md2html' Twig filter for converting Markdown
9
 * content to HTML.
10
 */
11
class AppExtension extends \Twig_Extension
12
{
13
    /**
14
     * @var Parser
15
     */
16
    private $parser;
17
18
    /**
19
     * AppExtension constructor.
20
     *
21
     * @param Parser $parser
22
     */
23
    public function __construct(Parser $parser)
24
    {
25
        $this->parser = $parser;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getFilters()
32
    {
33
        return [
34
            new \Twig_SimpleFilter('md2html', [$this, 'markdownToHtml'], ['is_safe' => ['html']]),
35
        ];
36
    }
37
38
    /**
39
     * Parses the given Markdown content and converts it HTML.
40
     *
41
     * @param string $content
42
     * @return string
43
     */
44
    public function markdownToHtml($content)
45
    {
46
        $document = $this->parser->parse($content);
47
        return $document->getContent();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getName()
54
    {
55
        return 'app.extension';
56
    }
57
}
58