BaseMarkdown.php 2.69 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
	 * @var array a map of markdown flavor names to corresponding parser class configurations.
25
	 */
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
	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';

48

Alexander Makarov committed
49
	/**
50
	 * Converts markdown into HTML.
Alexander Makarov committed
51
	 *
52 53 54 55
	 * @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
	 * @throws \yii\base\InvalidParamException when an undefined flavor is given.
Alexander Makarov committed
56
	 */
57
	public static function process($markdown, $flavor = 'original')
58
	{
59
		$parser = static::getParser($flavor);
60
		return $parser->parse($markdown);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	}

	/**
	 * Converts markdown into HTML but only parses inline elements.
	 *
	 * This can be useful for parsing small comments or description lines.
	 *
	 * @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
	 * @throws \yii\base\InvalidParamException when an undefined flavor is given.
	 */
	public static function processParagraph($markdown, $flavor = 'original')
	{
		$parser = static::getParser($flavor);
76
		return $parser->parseParagraph($markdown);
77 78 79 80 81 82 83 84 85 86 87 88
	}

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