BaseMarkdown.php 3.05 KB
Newer Older
1 2 3 4 5 6 7
<?php
/**
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @link http://www.yiiframework.com/
 * @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 36 37 38 39 40 41 42 43 44 45 46
    /**
     * @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' => [
            'class' => 'cebe\markdown\Markdown',
            'html5' => true,
            'enableNewlines' => true,
        ],
    ];
    /**
     * @var string the markdown flavor to use when none is specified explicitly.
     * Defaults to `original`.
     * @see $flavors
     */
    public static $defaultFlavor = 'original';
47

48 49 50
    /**
     * Converts markdown into HTML.
     *
51 52 53
     * @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
54 55 56 57 58
     * @throws \yii\base\InvalidParamException when an undefined flavor is given.
     */
    public static function process($markdown, $flavor = 'original')
    {
        $parser = static::getParser($flavor);
59

60 61
        return $parser->parse($markdown);
    }
62

63 64 65 66 67
    /**
     * Converts markdown into HTML but only parses inline elements.
     *
     * This can be useful for parsing small comments or description lines.
     *
68 69 70
     * @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
71 72 73 74 75
     * @throws \yii\base\InvalidParamException when an undefined flavor is given.
     */
    public static function processParagraph($markdown, $flavor = 'original')
    {
        $parser = static::getParser($flavor);
76

77 78 79 80
        return $parser->parseParagraph($markdown);
    }

    /**
81
     * @param string $flavor
82 83 84
     * @return \cebe\markdown\Parser
     * @throws \yii\base\InvalidParamException when an undefined flavor is given.
     */
85
    protected static function getParser($flavor)
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    {
        /** @var \cebe\markdown\Markdown $parser */
        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];
    }
102
}