diff --git a/markdown/Markdown.php b/Markdown.php similarity index 58% rename from markdown/Markdown.php rename to Markdown.php index bf31f7f..09dd389 100644 --- a/markdown/Markdown.php +++ b/Markdown.php @@ -11,6 +11,7 @@ use \Michelf\MarkdownExtra; use \Michelf\SmartyPantsTypographer; use yii\base\InvalidConfigException; +use yii\base\Component; /** * Markdown provides concrete implementation for PHP Markdown Extra @@ -19,13 +20,20 @@ * @author Kartik Visweswaran * @since 1.0 */ -class Markdown +class Markdown extends Component { /** * @var MarkdownExtra */ - protected static $markdown; + protected $markdown; + + protected $mdConfig; + protected $smConfig; + protected $cuConfig; + + public $config; + public $module; // SmartyPantsTypographer does nothing at all const SMARTYPANTS_ATTR_DO_NOTHING = 0; @@ -36,6 +44,18 @@ class Markdown // "--" for em-dashes; "---" for en-dashes const SMARTYPANTS_ATTR_SHORT_EM_DASH_LONG_EN = 3; + public function init() + { + $this->module = \Yii::$app->getModule('markdown'); + if ($this->module === null) { + throw new InvalidConfigException("The module 'markdown' was not found. Ensure you have setup the 'markdown' module in your Yii configuration file."); + } + + $this->mdConfig = empty($config['markdown']) ? [] : $config['markdown']; + $this->smConfig = empty($config['smarty']) ? [] : $config['smarty']; + $this->cuConfig = empty($config['custom']) ? $this->module->customConversion : $config['custom']; + } + /** * Converts markdown into HTML * @@ -48,30 +68,35 @@ class Markdown * @return string * @throws InvalidConfigException if module not set */ - public static function convert($content, $config = [], $smartyMode = self::SMARTYPANTS_ATTR_LONG_EM_DASH_SHORT_EN) + public function convert($content, $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']; + $output = $this->beforeProcess($output); + $output = $this->process($output); + $output = $this->afterProcess($output); + if ($this->module->smartyPants) { $smarty = new SmartyPantsTypographer($smartyMode); - foreach ($smConfig as $name => $value) { + foreach ($this->smConfig as $name => $value) { $smarty->{$name} = $value; } $output = $smarty->transform($output); - $cuConfig = empty($config['custom']) ? $module->customConversion : $config['custom']; - $output = static::customProcess($output, $cuConfig); + $output = $this->customProcess($output); } } return $output; } + public function beforeProcess($content) + { + return $content; + } + + public function afterProcess($content) + { + return $content; + } + /** * Converts markdown into HTML * @@ -79,15 +104,15 @@ public static function convert($content, $config = [], $smartyMode = self::SMART * @param array $config * @return string */ - public static function process($content, $config = []) + public function process($content) { - if (static::$markdown === null) { - static::$markdown = new MarkdownExtra(); + if ($this->markdown === null) { + $this->markdown = new MarkdownExtra(); } - foreach ($config as $name => $value) { - static::$markdown->{$name} = $value; + foreach ($this->mdConfig as $name => $value) { + $this->markdown->{$name} = $value; } - return static::$markdown->transform($content); + return $this->markdown->transform($content); } /** @@ -97,12 +122,12 @@ public static function process($content, $config = []) * @param array $config . List of key value pairs to find and replace * @return string */ - public static function customProcess($content, $config = []) + public function customProcess($content) { - if (empty($config)) { + if (empty($this->cuConfig)) { return $content; } - return strtr($content, $config); + return strtr($content, $this->cuConfig); } } \ No newline at end of file diff --git a/markdown/MarkdownEditor.php b/MarkdownEditor.php similarity index 100% rename from markdown/MarkdownEditor.php rename to MarkdownEditor.php diff --git a/markdown/MarkdownEditorAsset.php b/MarkdownEditorAsset.php similarity index 90% rename from markdown/MarkdownEditorAsset.php rename to MarkdownEditorAsset.php index 0162e1f..1b7875a 100644 --- a/markdown/MarkdownEditorAsset.php +++ b/MarkdownEditorAsset.php @@ -19,7 +19,7 @@ class MarkdownEditorAsset extends \kartik\widgets\AssetBundle public function init() { - $this->setSourcePath(__DIR__ . '/../assets'); + $this->setSourcePath(__DIR__ . '/assets'); $this->setupAssets('css', ['css/kv-markdown']); $this->setupAssets('js', ['js/rangyinputs-jquery-1.1.2', 'js/kv-markdown']); parent::init(); diff --git a/markdown/Module.php b/Module.php similarity index 100% rename from markdown/Module.php rename to Module.php diff --git a/markdown/controllers/ParseController.php b/controllers/ParseController.php similarity index 92% rename from markdown/controllers/ParseController.php rename to controllers/ParseController.php index d3c12c3..8ec1a5a 100644 --- a/markdown/controllers/ParseController.php +++ b/controllers/ParseController.php @@ -25,8 +25,9 @@ public function actionPreview() { $output = ''; $module = Yii::$app->controller->module; + $parser = $module->get('parser'); if (isset($_POST['source'])) { - $output = (strlen($_POST['source']) > 0) ? Markdown::convert($_POST['source'], ['custom' => $module->customConversion]) : $_POST['nullMsg']; + $output = (strlen($_POST['source']) > 0) ? $parser->convert($_POST['source']) : $_POST['nullMsg']; } echo Json::encode(HtmlPurifier::process($output)); } diff --git a/markdown/messages/de/markdown.php b/messages/de/markdown.php similarity index 100% rename from markdown/messages/de/markdown.php rename to messages/de/markdown.php diff --git a/markdown/messages/en/markdown.php b/messages/en/markdown.php similarity index 100% rename from markdown/messages/en/markdown.php rename to messages/en/markdown.php diff --git a/markdown/messages/fr/markdown.php b/messages/fr/markdown.php similarity index 100% rename from markdown/messages/fr/markdown.php rename to messages/fr/markdown.php diff --git a/markdown/views/parse/download.php b/views/parse/download.php similarity index 100% rename from markdown/views/parse/download.php rename to views/parse/download.php