Completed
Pull Request — master (#11438)
by Misbahul D
09:03
created

BaseMarkdown::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\helpers;
9
10
use Yii;
11
use yii\base\InvalidParamException;
12
13
/**
14
 * BaseMarkdown provides concrete implementation for [[Markdown]].
15
 *
16
 * Do not use BaseMarkdown. Use [[Markdown]] instead.
17
 *
18
 * @author Carsten Brandt <[email protected]>
19
 * @since 2.0
20
 */
21
class BaseMarkdown
22
{
23
    /**
24
     * @var array a map of markdown flavor names to corresponding parser class configurations.
25
     */
26
    public static $flavors = [
27
        'original' => [
28
            'class' => 'cebe\markdown\Markdown',
29
            'html5' => true,
30
        ],
31
        'gfm' => [
32
            'class' => 'cebe\markdown\GithubMarkdown',
33
            'html5' => true,
34
        ],
35
        'gfm-comment' => [
36
            'class' => 'cebe\markdown\GithubMarkdown',
37
            'html5' => true,
38
            'enableNewlines' => true,
39
        ],
40
        'extra' => [
41
            'class' => 'cebe\markdown\MarkdownExtra',
42
            'html5' => true,
43
        ],
44
    ];
45
    /**
46
     * @var string the markdown flavor to use when none is specified explicitly.
47
     * Defaults to `original`.
48
     * @see $flavors
49
     */
50
    public static $defaultFlavor = 'original';
51
52
53
    /**
54
     * Converts markdown into HTML.
55
     *
56
     * @param string $markdown the markdown text to parse
57
     * @param string $flavor the markdown flavor to use. See [[$flavors]] for available values.
58
     * @return string the parsed HTML output
59
     * @throws \yii\base\InvalidParamException when an undefined flavor is given.
60
     */
61 1
    public static function process($markdown, $flavor = null)
62
    {
63 1
        $parser = static::getParser($flavor);
64
65 1
        return $parser->parse($markdown);
66
    }
67
68
    /**
69
     * Converts markdown into HTML but only parses inline elements.
70
     *
71
     * This can be useful for parsing small comments or description lines.
72
     *
73
     * @param string $markdown the markdown text to parse
74
     * @param string $flavor the markdown flavor to use. See [[$flavors]] for available values.
75
     * @return string the parsed HTML output
76
     * @throws \yii\base\InvalidParamException when an undefined flavor is given.
77
     */
78
    public static function processParagraph($markdown, $flavor = null)
79
    {
80
        $parser = static::getParser($flavor);
81
82
        return $parser->parseParagraph($markdown);
83
    }
84
85
    /**
86
     * @param string $flavor
87
     * @return \cebe\markdown\Parser
88
     * @throws \yii\base\InvalidParamException when an undefined flavor is given.
89
     */
90 1
    protected static function getParser($flavor)
91
    {
92 1
        if ($flavor === null) {
93 1
            $flavor = static::$defaultFlavor;
94 1
        }
95
        /* @var $parser \cebe\markdown\Markdown */
96 1
        if (!isset(static::$flavors[$flavor])) {
97
            throw new InvalidParamException("Markdown flavor '$flavor' is not defined.'");
98 1
        } elseif (!is_object($config = static::$flavors[$flavor])) {
99 1
            static::$flavors[$flavor] = Yii::createObject($config);
100 1
        }
101
102 1
        return static::$flavors[$flavor];
103
    }
104
}
105