CommonMarkConverter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
dl 0
loc 63
ccs 16
cts 16
cp 1
rs 10
c 3
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A convertToHtml() 0 5 1
A getEnvironment() 0 3 1
A __construct() 0 14 3
A __invoke() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
11
 *  - (c) John MacFarlane
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 */
16
17
namespace League\CommonMark;
18
19
use League\CommonMark\Environment\ConfigurableEnvironmentInterface;
20
use League\CommonMark\Environment\Environment;
21
use League\CommonMark\Environment\EnvironmentInterface;
22
use League\CommonMark\Parser\MarkdownParser;
23
use League\CommonMark\Parser\MarkdownParserInterface;
24
use League\CommonMark\Renderer\HtmlRenderer;
25
use League\CommonMark\Renderer\HtmlRendererInterface;
26
27
/**
28
 * Converts CommonMark-compatible Markdown to HTML.
29
 */
30
class CommonMarkConverter implements MarkdownConverterInterface
31
{
32
    /** @var EnvironmentInterface */
33
    protected $environment;
34
35
    /** @var MarkdownParserInterface */
36
    protected $markdownParser;
37
38
    /** @var HtmlRendererInterface */
39
    protected $htmlRenderer;
40
41
    /**
42
     * Create a new commonmark converter instance.
43
     *
44
     * @param array<string, mixed> $config
45
     */
46 2736
    public function __construct(array $config = [], ?EnvironmentInterface $environment = null)
47
    {
48 2736
        if ($environment === null) {
49 2040
            $environment = Environment::createCommonMarkEnvironment();
50
        }
51
52 2736
        if ($environment instanceof ConfigurableEnvironmentInterface) {
53 2736
            $environment->mergeConfig($config);
54
        }
55
56 2736
        $this->environment = $environment;
57
58 2736
        $this->markdownParser = new MarkdownParser($environment);
59 2736
        $this->htmlRenderer   = new HtmlRenderer($environment);
60 2736
    }
61
62 9
    public function getEnvironment(): EnvironmentInterface
63
    {
64 9
        return $this->environment;
65
    }
66
67
    /**
68
     * Converts CommonMark to HTML.
69
     *
70
     * @param string $commonMark The Markdown to convert
71
     *
72
     * @return string Rendered HTML
73
     *
74
     * @throws \RuntimeException
75
     */
76 2727
    public function convertToHtml(string $commonMark): string
77
    {
78 2727
        $documentAST = $this->markdownParser->parse($commonMark);
79
80 2718
        return $this->htmlRenderer->renderDocument($documentAST);
81
    }
82
83
    /**
84
     * Converts CommonMark to HTML.
85
     *
86
     * @see Converter::convertToHtml
87
     *
88
     * @throws \RuntimeException
89
     */
90 3
    public function __invoke(string $commonMark): string
91
    {
92 3
        return $this->convertToHtml($commonMark);
93
    }
94
}
95