PHP implementation of Brad Choate's Textile 2. It is feature compatible with the MovableType plugin. Does not play well with the Markdown, Textile, or Textile 2 plugins that ship with WordPress. Packaged by Adam Gessaman.
Author: Jim Riggs
Author URI: http://jimandlissa.com/project/textilephp
*/
// @(#) $Id: Textile.php,v 1.5 2004/05/25 15:05:18 jhriggs Exp $
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
function do_textile($text) {
// setting $dont_make_text_pretty to 1 will ensure that neither wptexturize() or SmartyPants() functions are run on your text.
$dont_make_text_pretty = 0;
//$textile = new Textile;
$textile = new MTLikeTextile;
// head_offset sets the amount to increase header level (i.e. h1. becomes h3. if head_offset == 2)
$textile->options['head_offset'] = 0;
// $textile->options['char_encoding'] = 'utf-8';
$textile->options['char_encoding'] = false;
// do_quotes enables/disables "smart quotes"
$textile->options['do_quotes'] = 1;
// trim_spaces, if set, will tell textile to clear all lines containing only spaces
$textile->options['trim_spaces'] = 0;
// enable/disable SmartyPants filtering
$textile->options['smarty_mode'] = 1;
// if preserve_spaces is 1, whitespace will be encoded to html entities
$textile->options['preserve_spaces'] = 0;
/** Don't edit below this line **/
if ($dont_make_text_pretty == 1) {
$textile->options['smarty_mode'] = 0;
return $textile->process($text);
} else {
return $textile->process($text);
}
}
/**
* The Textile class serves as a wrapper for all Textile
* functionality. It is not inherently necessary that Textile be a
* class; however, this is as close as one can get to a namespace in
* PHP. Wrapping the functionality in a class prevents name
* collisions and dirtying of the global namespace. The Textile class
* uses no global variables and will not have any side-effects on
* other code.
*
* @brief Class wrapper for the Textile functionality.
*/
class Textile {
/**
* The @c array containing all of the Textile options for this
* object.
*
* @private
*/
var $options = array();
/**
* The @c string containing the regular expression pattern for a
* URL. This variable is initialized by @c _create_re() which is
* called in the contructor.
*
* @private
*/
var $urlre;
/**
* The @c string containing the regular expression pattern for
* punctuation characters. This variable is initialized by
* @c _create_re() which is called in the contructor.
*
* @private
*/
var $punct;
/**
* The @c string containing the regular expression pattern for the
* valid vertical alignment codes. This variable is initialized by
* @c _create_re() which is called in the contructor.
*
* @private
*/
var $valignre;
/**
* The @c string containing the regular expression pattern for the
* valid table alignment codes. This variable is initialized by
* @c _create_re() which is called in the contructor.
*
* @private
*/
var $tblalignre;
/**
* The @c string containing the regular expression pattern for the
* valid horizontal alignment codes. This variable is initialized by
* @c _create_re() which is called in the contructor.
*
* @private
*/
var $halignre;
/**
* The @c string containing the regular expression pattern for the
* valid alignment codes. This variable is initialized by
* @c _create_re() which is called in the contructor.
*
* @private
*/
var $alignre;
/**
* The @c string containing the regular expression pattern for the
* valid image alignment codes. This variable is initialized by
* @c _create_re() which is called in the contructor.
*
* @private
*/
var $imgalignre;
/**
* The @c string containing the regular expression pattern for a
* class, ID, and/or padding specification. This variable is
* initialized by @c _create_re() which is called in the contructor.
*
* @private
*/
var $clstypadre;
/**
* The @c string containing the regular expression pattern for a
* class and/or ID specification. This variable is initialized by
* @c _create_re() which is called in the contructor.
*
* @private
*/
var $clstyre;
/**
* The @c string containing the regular expression pattern for a
* class, ID, and/or filter specification. This variable is
* initialized by @c _create_re() which is called in the contructor.
*
* @private
*/
var $clstyfiltre;
/**
* The @c string containing the regular expression pattern for a
* code block. This variable is initialized by @c _create_re() which
* is called in the contructor.
*
* @private
*/
var $codere;
/**
* The @c string containing the regular expression pattern for all
* block tags. This variable is initialized by @c _create_re() which
* is called in the contructor.
*
* @private
*/
var $blocktags;
/**
* The @c array containing the list of lookup links.
*
* @private
*/
var $links = array();
/**
* The @c array containing array
s of replacement blocks
* of text that are temporary removed from the input text to avoid
* processing. Different functions use this replacement
* functionality, and each shifts its own replacement array into
* position 0 and removes it when finished. This avoids having
* several replacement variables and/or functions clobbering
* eachothers' replacement blocks.
*
* @private
*/
var $repl = array();
/**
* The @c array containing temporary string
s used in
* replacement callbacks. *JHR*
*
* @private
*/
var $tmp = array();
/**
* Instantiates a new Textile object. Optional options
* can be passed to initialize the object. Attributes for the
* options key are the same as the get/set method names
* documented here.
*
* @param $options The @c array specifying the options to use for
* this object.
*
* @public
*/
function Textile($options = array()) {
$this->options = $options;
$this->options['filters'] = ($this->options['filters'] ? $this->options['filters'] : array());
$this->options['charset'] = ($this->options['charset'] ? $this->options['charset'] : 'iso-8859-1');
$this->options['char_encoding'] = ($this->options['char_encoding'] ? $this->options['char_encoding'] : 1);
$this->options['do_quotes'] = ($this->options['do_quotes'] ? $this->options['do_quotes'] : 1);
$this->options['trim_spaces'] = ($this->options['trim_spaces'] ? $this->options['trim_spaces'] : 0);
$this->options['smarty_mode'] = ($this->options['smarty_mode'] ? $this->options['smarty_mode'] : 1);
$this->options['preserve_spaces'] = ($this->options['preserve_spaces'] ? $this->options['preserve_spaaces'] : 0);
$this->options['head_offset'] = ($this->options['head_offset'] ? $this->options['head_offset'] : 0);
if (is_array($this->options['css'])) {
$this->css($this->options['css']);
}
$this->options['macros'] = ($this->options['macros'] ? $this->options['macros'] : $this->default_macros());
if ($this->options['flavor']) {
$this->flavor($this->options['flavor']);
} else {
$this->flavor('xhtml1/css');
}
$this->_create_re();
} // function Textile
// getter/setter methods...
/**
* Used to set Textile attributes. Attribute names are the same
* as the get/set method names documented here.
*
* @param $opt A @c string specifying the name of the option to
* change or an @c array specifying options and values.
* @param $value The value for the provided option name.
*
* @public
*/
function set($opt, $value = NULL) {
if (is_array($opt)) {
foreach ($opt as $opt => $value) {
$this->set($opt, $value);
}
} else {
// the following options have special set methods
// that activate upon setting:
if ($opt == 'charset') {
$this->charset($value);
} elseif ($opt == 'css') {
$this->css($value);
} elseif ($opt == 'flavor') {
$this->flavor($value);
} else {
$this->options[$opt] = $value;
}
}
} // function set
/**
* Used to get Textile attributes. Attribute names are the same
* as the get/set method names documented here.
*
* @param $opt A @c string specifying the name of the option to get.
*
* @return The value for the provided option.
*
* @public
*/
function get($opt) {
return $this->options[$opt];
} // function get
/**
* Gets or sets the "disable html" control, which allows you to
* prevent HTML tags from being used within the text processed.
* Any HTML tags encountered will be removed if disable html is
* enabled. Default behavior is to allow HTML.
*
* @param $disable_html If provided, a @c bool indicating whether or
* not this object should disable HTML.
*
* @return A true value if this object disables HTML; a false value
* otherwise.
*
* @public
*/
function disable_html($disable_html = NULL) {
if ($disable_html != NULL) {
$this->options['disable_html'] = $disable_html;
}
return ($this->options['disable_html'] ? $this->options['disable_html'] : 0);
} // function disable_html
/**
* Gets or sets the relative heading offset, which allows you to
* change the heading level used within the text processed. For
* example, if the heading offset is '2' and the text contains an
* 'h1' block, an \
';
$this->options['_blockcode_close'] = '
';
$this->options['css_mode'] = 1;
}
} elseif (preg_match('/^html/', $flavor)) {
$this->options['_line_open'] = '';
$this->options['_line_close'] = '';
$this->options['_blockcode_close'] = '
';
$this->options['css_mode'] = preg_match('/\/css/', $flavor);
}
if ($this->options['css_mode'] && !$this->options['css']) { $this->_css_defaults(); }
}
return $this->options['flavor'];
} // function flavor
/**
* Gets or sets the css support for Textile. If css is enabled,
* Textile will emit CSS rules. You may pass a 1 or 0 to enable
* or disable CSS behavior altogether. If you pass an associative array,
* you may assign the CSS class names that are used by
* Textile. The following key names for such an array are
* recognized:
*
* ,
* \ blocks or \