BaseMarkdown.php 3.17 KB
Newer Older
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Carsten Brandt committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5 6 7
 * @license http://www.yiiframework.com/license/
 */

8
namespace yii\helpers;
9

10 11
use Yii;
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 <mail@cebe.cc>
19 20
 * @since 2.0
 */
21
class BaseMarkdown
22
{
23 24 25 26 27 28 29 30 31 32 33 34 35
    /**
     * @var array a map of markdown flavor names to corresponding parser class configurations.
     */
    public static $flavors = [
        'original' => [
            'class' => 'cebe\markdown\Markdown',
            'html5' => true,
        ],
        'gfm' => [
            'class' => 'cebe\markdown\GithubMarkdown',
            'html5' => true,
        ],
        'gfm-comment' => [
36
            'class' => 'cebe\markdown\GithubMarkdown',
37 38 39
            'html5' => true,
            'enableNewlines' => true,
        ],
40 41 42 43
        'extra' => [
            'class' => 'cebe\markdown\MarkdownExtra',
            'html5' => true,
        ],
44 45 46 47 48 49 50
    ];
    /**
     * @var string the markdown flavor to use when none is specified explicitly.
     * Defaults to `original`.
     * @see $flavors
     */
    public static $defaultFlavor = 'original';
51

52

53 54 55
    /**
     * Converts markdown into HTML.
     *
56 57 58
     * @param string $markdown the markdown text to parse
     * @param string $flavor the markdown flavor to use. See [[$flavors]] for available values.
     * @return string the parsed HTML output
59 60 61 62 63
     * @throws \yii\base\InvalidParamException when an undefined flavor is given.
     */
    public static function process($markdown, $flavor = 'original')
    {
        $parser = static::getParser($flavor);
64

65 66
        return $parser->parse($markdown);
    }
67

68 69 70 71 72
    /**
     * Converts markdown into HTML but only parses inline elements.
     *
     * This can be useful for parsing small comments or description lines.
     *
73 74 75
     * @param string $markdown the markdown text to parse
     * @param string $flavor the markdown flavor to use. See [[$flavors]] for available values.
     * @return string the parsed HTML output
76 77 78 79 80
     * @throws \yii\base\InvalidParamException when an undefined flavor is given.
     */
    public static function processParagraph($markdown, $flavor = 'original')
    {
        $parser = static::getParser($flavor);
81

82 83 84 85
        return $parser->parseParagraph($markdown);
    }

    /**
86
     * @param string $flavor
87 88 89
     * @return \cebe\markdown\Parser
     * @throws \yii\base\InvalidParamException when an undefined flavor is given.
     */
90
    protected static function getParser($flavor)
91
    {
92
        /* @var $parser \cebe\markdown\Markdown */
93 94 95 96 97 98 99 100 101 102 103 104 105 106
        if (!isset(static::$flavors[$flavor])) {
            throw new InvalidParamException("Markdown flavor '$flavor' is not defined.'");
        } elseif (!is_object($config = static::$flavors[$flavor])) {
            $parser = Yii::createObject($config);
            if (is_array($config)) {
                foreach ($config as $name => $value) {
                    $parser->{$name} = $value;
                }
            }
            static::$flavors[$flavor] = $parser;
        }

        return static::$flavors[$flavor];
    }
107
}