forked from kartik-v/yii2-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Markdown.php
108 lines (98 loc) · 3.37 KB
/
Markdown.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014
* @package yii2-markdown
* @version 1.1.0
*/
namespace kartik\markdown;
use \Michelf\MarkdownExtra;
use \Michelf\SmartyPantsTypographer;
use yii\base\InvalidConfigException;
/**
* Markdown provides concrete implementation for PHP Markdown Extra
* and PHP SmartyPantsTypographer.
*
* @author Kartik Visweswaran <[email protected]>
* @since 1.0
*/
class Markdown
{
/**
* @var MarkdownExtra
*/
protected static $markdown;
// SmartyPantsTypographer does nothing at all
const SMARTYPANTS_ATTR_DO_NOTHING = 0;
// "--" for em-dashes; no en-dash support
const SMARTYPANTS_ATTR_EM_DASH = 1;
// "---" for em-dashes; "--" for en-dashes
const SMARTYPANTS_ATTR_LONG_EM_DASH_SHORT_EN = 2;
// "--" for em-dashes; "---" for en-dashes
const SMARTYPANTS_ATTR_SHORT_EM_DASH_LONG_EN = 3;
/**
* Converts markdown into HTML
*
* @param string $content
* @param array $config . Options to configure MarkdownExtra and smarty
* - markdown: array for MarkdownExtra configuration parameters
* - smarty: array for SmartyPantsTypographer configuration parameters
* - custom: array for Custom configuration parameters
* @param int $smartyMode the SmartyPantsTypographer processing mode
* @return string
* @throws InvalidConfigException if module not set
*/
public static function convert($content, $config = [], $smartyMode = self::SMARTYPANTS_ATTR_LONG_EM_DASH_SHORT_EN)
{
$module = \Yii::$app->getModule('markdown');
if ($module === null) {
throw new InvalidConfigException("The module 'markdown' was not found. Ensure you have setup the 'markdown' module in your Yii configuration file.");
}
$output = $content;
if (strlen($output) > 0) {
$mdConfig = empty($config['markdown']) ? [] : $config['markdown'];
$output = static::process($content, $mdConfig);
if ($module->smartyPants) {
$smConfig = empty($config['smarty']) ? [] : $config['smarty'];
$smarty = new SmartyPantsTypographer($smartyMode);
foreach ($smConfig as $name => $value) {
$smarty->{$name} = $value;
}
$output = $smarty->transform($output);
$cuConfig = empty($config['custom']) ? $module->customConversion : $config['custom'];
$output = static::customProcess($output, $cuConfig);
}
}
return $output;
}
/**
* Converts markdown into HTML
*
* @param string $content
* @param array $config
* @return string
*/
public static function process($content, $config = [])
{
if (static::$markdown === null) {
static::$markdown = new MarkdownExtra();
}
foreach ($config as $name => $value) {
static::$markdown->{$name} = $value;
}
return static::$markdown->transform($content);
}
/**
* Custom conversion of patterns
*
* @param string $content
* @param array $config . List of key value pairs to find and replace
* @return string
*/
public static function customProcess($content, $config = [])
{
if (empty($config)) {
return $content;
}
return strtr($content, $config);
}
}