Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making Markdown class configurable (see #17) #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 48 additions & 23 deletions markdown/Markdown.php → Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,13 +20,20 @@
* @author Kartik Visweswaran <[email protected]>
* @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;
Expand All @@ -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
*
Expand All @@ -48,46 +68,51 @@ 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
*
* @param string $content
* @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);
}

/**
Expand All @@ -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);
}

}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.