Passed
Push — master ( 9dbdd9...d5a428 )
by Alexander
04:15
created

framework/helpers/BaseMarkdown.php (1 issue)

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\InvalidArgumentException;
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
     * Defaults to [[$defaultFlavor]], if not set.
59
     * @return string the parsed HTML output
60
     * @throws InvalidArgumentException when an undefined flavor is given.
61
     */
62 2
    public static function process($markdown, $flavor = null)
63
    {
64 2
        $parser = static::getParser($flavor);
65
66 1
        return $parser->parse($markdown);
67
    }
68
69
    /**
70
     * Converts markdown into HTML but only parses inline elements.
71
     *
72
     * This can be useful for parsing small comments or description lines.
73
     *
74
     * @param string $markdown the markdown text to parse
75
     * @param string $flavor the markdown flavor to use. See [[$flavors]] for available values.
76
     * Defaults to [[$defaultFlavor]], if not set.
77
     * @return string the parsed HTML output
78
     * @throws InvalidArgumentException when an undefined flavor is given.
79
     */
80 1
    public static function processParagraph($markdown, $flavor = null)
81
    {
82 1
        $parser = static::getParser($flavor);
83
84 1
        return $parser->parseParagraph($markdown);
85
    }
86
87
    /**
88
     * @param string $flavor the markdown flavor to use. See [[$flavors]] for available values.
89
     * Defaults to [[$defaultFlavor]], if not set.
90
     * @return \cebe\markdown\Parser
91
     * @throws InvalidArgumentException when an undefined flavor is given.
92
     */
93 3
    protected static function getParser($flavor)
94
    {
95 3
        if ($flavor === null) {
0 ignored issues
show
The condition $flavor === null is always false.
Loading history...
96 2
            $flavor = static::$defaultFlavor;
97
        }
98
        /* @var $parser \cebe\markdown\Markdown */
99 3
        if (!isset(static::$flavors[$flavor])) {
100 1
            throw new InvalidArgumentException("Markdown flavor '$flavor' is not defined.'");
101 2
        } elseif (!is_object($config = static::$flavors[$flavor])) {
102 1
            static::$flavors[$flavor] = Yii::createObject($config);
103
        }
104
105 2
        return static::$flavors[$flavor];
106
    }
107
}
108