Skip to content
This repository has been archived by the owner on Dec 24, 2017. It is now read-only.

Commit

Permalink
Remove unecessary 'fixes' caused by encoding EE tags.
Browse files Browse the repository at this point in the history
ExpressionEngine automatically encodes EE tags in the content.
If the user wants to encode the encoded tagdata, let him.
  • Loading branch information
Stephen Lewis committed Sep 4, 2011
1 parent 3b4cf96 commit 3b3ac62
Showing 1 changed file with 83 additions and 54 deletions.
137 changes: 83 additions & 54 deletions ee2/third_party/smartdown/pi.smartdown.php
Expand Up @@ -61,60 +61,85 @@ public function __construct($tagdata = '')
$functions = $ee->functions;
$tmpl = $ee->TMPL;

$default_smartquotes = '2';
$this->return_data = '';
$this->return_data = '';

/**
* Establish the default settings, and override them with
* any config settings.
*/

$settings = array(
'disable:markdown' => $config->item('disable:markdown', 'smartdown') === TRUE ? TRUE : FALSE,
'disable:smartypants' => $config->item('disable:smartypants', 'smartdown') === TRUE ? TRUE : FALSE,
'ee_tags:encode' => $config->item('ee_tags:encode', 'smartdown') === TRUE ? TRUE : FALSE,
'smart_quotes' => $config->item('smart_quotes', 'smartdown') ? $config->item('smart_quotes', 'smartdown') : '2'
);

if ($tagdata)
if ( ! $tagdata)
{
// Fieldtype.
$disable_md = $config->item('smartdown:disable:markdown') == 'yes';
$disable_sp = $config->item('smartdown:disable:smartypants') == 'yes';
$encode = $config->item('smartdown:ee_tags:encode') == 'yes';
$fix_images = !($config->item('smartdown:ee_tags:fix_transplanted_images') == 'no');
$smart_quotes = $config->item('smartdown:smart_quotes') ? $config->item('smartdown:smart_quotes') : $default_smartquotes;
$tagdata = $tmpl->tagdata;

/**
* Override the settings with any tag parameters. There must be
* a more elegant way of doing this, but my brain is failing me
* right now.
*/

$settings = array(
'disable:markdown' => $tmpl->fetch_param('disable:markdown')
? ($tmpl->fetch_param('disable:markdown') == 'yes')
: $settings['disable:markdown'],

'disable:smartypants' => $tmpl->fetch_param('disable:smartypants')
? ($tmpl->fetch_param('disable:smartypants') == 'yes')
: $settings['disable:smartypants'],

'ee_tags:encode' => $tmpl->fetch_param('ee_tags:encode')
? ($tmpl->fetch_param('ee_tags:encode') == 'yes')
: $settings['ee_tags:encode'],

'smart_quotes' => $tmpl->fetch_param('smart_quotes')
? $tmpl->fetch_param('smart_quotes')
: $settings['smart_quotes']
);
}
else

// Encode EE tags.
if ($settings['ee_tags:encode'])
{
// Template tag.
$tagdata = $tmpl->tagdata;
$disable_md = $tmpl->fetch_param('disable:markdown') == 'yes';
$disable_sp = $tmpl->fetch_param('disable:smartypants') == 'yes';
$encode = ($tmpl->fetch_param('ee_tags:encode') == 'yes') OR ($tmpl->fetch_param('encode_ee_tags') == 'yes');
$fix_images = !($tmpl->fetch_param('ee_tags:fix_transplanted_images') == 'no');
$smart_quotes = $tmpl->fetch_param('smart_quotes') ? $tmpl->fetch_param('smart_quotes') : $default_smartquotes;
$tagdata = $functions->encode_ee_tags($tagdata, TRUE);
}

if ( ! $disable_md)
// Markdown.
if ( ! $settings['disable:markdown'])
{
if ($encode)
{
$tagdata = Markdown($functions->encode_ee_tags($tagdata, TRUE));

// Fix EE code samples.
$tagdata = preg_replace_callback(
'|' .preg_quote('<code>') .'(.*?)' .preg_quote('</code>') .'|s',
array($this, '_fix_encoded_ee_code_samples'),
$tagdata
);

// Fix {path=} URLs.
$tagdata = preg_replace('/&#123;(path=.*?)&#125;/i', '{$1}', $tagdata);

// Play nicely with NSM Transplant and the {image_xx} technique.
if ($fix_images)
{
$tagdata = preg_replace('/&#123;(image_[0-9]+)&#125;/i', '{$1}', $tagdata);
}
}
else
{
$tagdata = Markdown($tagdata);
}
$tagdata = Markdown($tagdata);

/**
* ExpressionEngine automatically encodes any EE tags within
* the tagdata, regardless of context.
*
* This is not what is required within <code> tags, so we
* fix that problem here.
*/

$tagdata = preg_replace_callback(
'|' .preg_quote('<code>') .'(.*?)' .preg_quote('</code>') .'|s',
array($this, '_fix_encoded_ee_code_samples'),
$tagdata
);

// Fix {path=} URLs.
// $tagdata = preg_replace('/&#123;(path=.*?)&#125;/i', '{$1}', $tagdata);

// Play nicely with NSM Transplant and the {image_xx} technique.
// $tagdata = preg_replace('/&#123;(image_[0-9]+)&#125;/i', '{$1}', $tagdata);
}

if ( ! $disable_sp)
// SmartyPants.
if ( ! $settings['disable:smartypants'])
{
$tagdata = SmartyPants($tagdata, $smart_quotes);
$tagdata = SmartyPants($tagdata, $settings['smart_quotes']);
}

$this->return_data = $tagdata;
Expand Down Expand Up @@ -156,27 +181,30 @@ public function usage()
{/exp:smartdown}

## Parameters ##
`disable:markdown`
: Set to `yes` to disable Markdown. Default is `no`.

`disable:smartypants`
: Set to `yes` to disable SmartyPants. Default is `no`.

`ee_tags:encode`
: Set to `yes`, to convert the curly braces for all EE tags and variables into entities. Default is `no`.

`ee_tags:fix_transplanted_images`
: Set to `no` to prevent `ee_tags:encode` from playing nicely with NSM Transplant and the `{image_xx}` technique. Default is `yes`.

`smart_quotes`
: Fine-grained control over SmartyPants' handling of smart quotes. Will never be used by 99% of you.
Nosey types should take a look at the SmartyPants source code.

## Fieldtype parameters ##
The SmartDown fieldtype may be configured using `config.php`. This makes it possible to set any of the supported SmartDown parameters directly in the fieldtype, without the requirement to use the `{exp:smartdown}` template tag.

`$config['smartdown:ee_tags:encode']`
: Mirrors the `ee_tags:encode` template tag.

`$config['smartdown:ee_tags:fix_transplanted_images']`
: Mirrors the `ee_tags:fix_transplanted_images` template tag.
The SmartDown config settings should take the form of an associative array. All of the documented template parameters are supported, the only difference being that `TRUE` should be used instead of `yes`, and `FALSE` instead of `no`.

`$config['smartdown:smart_quotes']`
: Mirrors the `smart_quotes` template tag.
$config['smartdown'] => array(
'disable:markdown' => TRUE, // TRUE or FALSE. Default is FALSE.
'disable:smartypants' => TRUE, // TRUE or FALSE. Default is FALSE.
'ee_tags:encode' => TRUE, // TRUE or FALSE. Default is FALSE.
'smart_quotes' => 1 // Default is 2.
);

<?php

Expand Down Expand Up @@ -213,5 +241,6 @@ private function _fix_encoded_ee_code_samples($matches)

}


/* End of file : pi.smartdown.php */
/* File location : third_party/smartdown/pi.smartdown.php */

0 comments on commit 3b3ac62

Please sign in to comment.