===== Textile Behavior =====
This Behavior is based on [[http://bakery.cakephp.org/articles/view/textile-1|the article in the Bakery]] and [[http://me.mathideas.org/code/cakephp/text_behavior/text_behavior.php.txt|the version posted in comments]] by Adeel Khan.
To use this behavior you need classTextile.php from [[http://svn.textpattern.com/development/4.0/textpattern/lib/classTextile.php| TextPattern subversion repository]]. Put it in one of the vendor dirs (/vendor or /app/vendor).
To use it add
var $actAs = array('Textile');
in your model. By default it expects to find a field named body that will contain the textile formated text and html_body that will contain html generated by textile. Here are the default options:
var $actAs = array('Textile' => array('body' => array(
'html_prefix' => 'html_',
'rel' => '',
'lite' => false,
'noimages' => false,
'restricted' => false))
);
Here is a [[http://forum.textpattern.com/viewtopic.php?pid=145732#p145732|good explanation about different modes]] that Textile parser can work in by [[http://thresholdstate.com/|Alex]] from TextPattern forums(emphasis mine).
>**Textile Lite** allows bq and p, but no other block tags. It doesn’t prevent raw XHTML by itself.
>**Textile Restricted** escapes all raw HTML and special characters and turns off class/id/style/lang attributes, to prevent certain types of XSS attacks from untrusted users (it’s intended for comment forms, forum posts etc). It also uses ‘rel=nofollow’ on external links by default, but you can control that via the function parameters.
>Both the **normal** (TextileThis()) and **restricted** (TextileRestricted()) functions can be used with or without **Lite** mode. I believe the Textpattern support forum uses **Textile Restricted without Lite**. I’d suggest:
> * TextileThis with Lite=0 for article content from trusted users
> * TextileRestricted with Lite=0 for article content, forum posts and larger content from untrusted users
> * TextileRestricted with Lite=1 for comment posts, short descriptions etc
> * TextileThis with Lite=1 is probably not very useful
You can have multiple fields per model that will be textiled ;)
var $actAs = array('Textile' => array('body', 'excerpt' => array(
'html_prefix' => 'html_',
'rel' => 'nofollow',
'lite' =>true,
'noimages' => true,
'restricted' => true))
);
The //body// here will have the default options and the //excerpt// wont allow html or images and link will have the 'bofollow' attribute.
===== Source code =====
Here is the code - save it to app/models/behaviors/textile.php
model =& $model;
$default = array( 'body' => array('html_prefix' => 'html_',
'rel' => '',
'lite' => false,
'noimages' => false,
'restricted' => false)
);
if (empty($settings))
{
$this->fields = $default;
}
else
{
$this->fields = $settings;
}
foreach($this->fields as $field => $options)
{
$htmlPrefix = 'html_';
if(!empty($options['html_prefix']))
{
$htmlPrefix = $options['html_prefix'];
}
if(false===$model->hasField($field))
{
user_error('Model "'.$model->name.'" does not have a field called: '. $field, E_USER_ERROR );
}
else if(false===$model->hasField($htmlPrefix.$field))
{
user_error('Model "'.$model->name.'" does not have a field called: html_'.$field, E_USER_ERROR );
}
}
}
/**
* beforeSave callback
*
*/
function beforeSave()
{
$textile = @new Textile;
foreach($this->fields as $field => $options)
{
if(isset($this->model->data[$this->model->name][$field]))
{
$rel ='';
$lite = 0;
$noimages = 0;
$htmlPrefix = 'html_';
if(!empty($options['rel']))
{
$rel = $options['rel'];
}
if(isset($options['lite']) && $options['lite'] == true)
{
$lite = 1;
}
if(isset($options['noimages']) && $options['noimages'] == true)
{
$noimages = 1;
}
if(!empty($options['html_prefix']))
{
$htmlPrefix = $options['html_prefix'];
}
if(isset($options['restricted']) && $options['restricted'] == true)
{
$this->model->data[$this->model->name][$htmlPrefix.$field] = @$textile->TextileRestricted($this->model->data[$this->model->name][$field], $lite, $noimages, $rel);
}
else
{
$this->model->data[$this->model->name][$htmlPrefix.$field] = @$textile->TextileThis($this->model->data[$this->model->name][$field], $lite, '', $noimages, '', $rel);
}
}
}
return parent::beforeSave($this->model);
}
}
?>
===== Textile Reference =====
Here is a handy [[http://hobix.com/textile/quick.html|Textile reference]].
--- //[[blog@kabturek.info|Marcin Domanski]] 2007/07/27 01:46//
{{tag>[textile behavior cakephp]}}
~~DISCUSSION~~