From 02a27eac2af2da97ae7ea11aec9208b2ebba7887 Mon Sep 17 00:00:00 2001 From: Alex Sawallich Date: Sun, 6 Mar 2016 11:05:25 +0100 Subject: [PATCH] Added MarkdownExtra support Added functionality to choose MarkdownExtra for parsing. Adjusted the readme.md --- Module.php | 29 ++++++++++++++++++++++++++--- README.md | 23 +++++++++++++++++++---- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/Module.php b/Module.php index 72da478..4f42027 100644 --- a/Module.php +++ b/Module.php @@ -3,14 +3,37 @@ class Module extends \Zend\View\Helper\AbstractHelper { + const MARKDOWN_NORMAL = 'normal'; + const MARKDOWN_EXTRA = 'extra'; + + protected $classWhitelist = array( + self::MARKDOWN_NORMAL => '\Michelf\Markdown', + self::MARKDOWN_EXTRA => '\Michelf\MarkdownExtra' + ); + + protected static $defaultType = self::MARKDOWN_NORMAL; + public function getViewHelperConfig() { return array('services' => array('markdown' => $this)); } - public function __invoke($string = null) + public function __invoke($string = null, $type = null) { - if (!class_exists('Michelf\Markdown')) require_once __DIR__ . '/vendor/php-markdown/Michelf/Markdown.inc.php'; - return \Michelf\Markdown::defaultTransform($string); + if (null == $type) { + $type = self::$defaultType; + } + + $className = $this->classWhitelist[$type]; + if (!class_exists($className)) { + $fileName = str_replace('\\', '/', $className) . '.inc.php'; + require_once __DIR__ . '/vendor/php-markdown' . $fileName; + } + return $className::defaultTransform($string); + } + + public static function setDefaultType($type) + { + self::$defaultType = $type; } } diff --git a/README.md b/README.md index 9905fa2..6b187ca 100644 --- a/README.md +++ b/README.md @@ -17,14 +17,29 @@ To install EdpMarkdown, simply recursively clone this repository (`git clone With this module installed, using Markdown in your view scripts is easy: ```php -markdown('Hello, **this** is _Markdown_!'); ?> +markdown('Hello, **this** is _Markdown_!'); ?> ``` -**NOTE:** For security purposes, the output **SHOULD** be [sanitized](http://htmlpurifier.org/) if the Markdown is from an untrusted source. ([@padraic](https://github.com/padraic) says so!) See the [Markdown documentation on inline HTML](http://daringfireball.net/projects/markdown/syntax#html) to understand why this is necessary. +You may also use MarkdownExtra with this module: + +```php +markdown('Hello, **this** is MarkdownExtra!', \EdpMarkdown\Module::MARKDOWN_EXTRA); ?> +``` + +or simpler: + +```php +markdown('Hello, **this** is MarkdownExtra!', 'extra'); ?> +``` + +If you want all your calls to the markdown-helper to use MarkdownExtra, you can setup the default Markdown-type as follows: -## Configuration +```php +// For example in your onBootsrap-method: +\EdpMarkdown\Module::setDefaultType(\EdpMarkdown\Module::MARKDOWN_EXTRA); +``` -TODO: Create simple way to toggle to the 'extra' parser. +**NOTE:** For security purposes, the output **SHOULD** be [sanitized](http://htmlpurifier.org/) if the Markdown is from an untrusted source. ([@padraic](https://github.com/padraic) says so!) See the [Markdown documentation on inline HTML](http://daringfireball.net/projects/markdown/syntax#html) to understand why this is necessary. ## License