6 * let's reinvent the wheel one last time
8 * This library of code is meant to be a fast and universal replacement
9 * for any and all text-processing systems written in PHP
11 * It is dual-licensed for any use under the GNU/GPL2 and MIT licenses,
14 * (c) 2009 Fil - fil@rezo.net
15 * Documentation & http://zzz.rezo.net/-TextWheel-
17 * Usage: $wheel = new TextWheel(); echo $wheel->text($text);
21 if (!defined('_ECRIRE_INC_VERSION')) {
25 require_once dirname(__FILE__
) . "/textwheelrule.php";
27 abstract class TextWheelDataSet
{
29 protected $data = array();
32 * file finder : can be overloaded in order to use application dependant
39 protected function findFile(&$file, $path = '') {
42 // absolute file path ?
43 if (file_exists($file)) {
47 // file embed with texwheels, relative to calling ruleset
48 if ($path and file_exists($f = $path . $file)) {
52 // textwheel default path ?
54 $default_path = dirname(__FILE__
) . '/../wheels/';
56 if (file_exists($f = $default_path . $file)) {
64 * Load a yaml file describing data
67 * @param string $default_path
70 protected function loadFile(&$file, $default_path = '') {
71 if (!preg_match(',[.]yaml$,i', $file)
73 or !$file = $this->findFile($file, $default_path)
78 defined('_YAML_EVAL_PHP') ||
define('_YAML_EVAL_PHP', false);
79 if (!function_exists('yaml_decode')) {
80 if (function_exists('include_spip')) {
81 include_spip('inc/yaml-mini');
83 require_once dirname(__FILE__
) . '/../inc/yaml.php';
86 $dataset = yaml_decode(file_get_contents($file));
88 if (is_null($dataset)) {
91 # throw new DomainException('yaml file is empty, unreadable or badly formed: '.$file.var_export($dataset,true));
93 // if a php file with same name exists
94 // include it as it contains callback functions
95 if ($f = preg_replace(',[.]yaml$,i', '.php', $file)
98 $dataset[] = array('require' => $f, 'priority' => -1000);
106 class TextWheelRuleSet
extends TextWheelDataSet
{
108 protected $sorted = true;
113 * @param array|string $ruleset
114 * @param string $filepath
116 public function __construct($ruleset = array(), $filepath = '') {
118 $this->addRules($ruleset, $filepath);
123 * public static loader
124 * can be overloaded to use memoization
126 * @param array $ruleset
127 * @param string $callback
128 * @param string $class
131 public static function &loader($ruleset, $callback = '', $class = 'TextWheelRuleSet') {
133 $ruleset = new $class($ruleset);
142 * Get an existing named rule in order to override it
144 * @param string $name
147 public function &getRule($name) {
148 if (isset($this->data
[$name])) {
149 return $this->data
[$name];
161 public function &getRules() {
170 * @param TextWheelRule $rule
172 public function addRule($rule) {
173 # cast array-rule to object
174 if (is_array($rule)) {
175 $rule = new TextWheelRule($rule);
177 $this->data
[] = $rule;
178 $this->sorted
= false;
182 * add an list of rules
184 * - an array of rules
185 * - a string filename
186 * - an array of string filename
188 * @param array|string $rules
189 * @param string $filepath
191 public function addRules($rules, $filepath = '') {
192 // rules can be an array of filename
193 if (is_array($rules) and is_string(reset($rules))) {
194 foreach ($rules as $i => $filename) {
195 $this->addRules($filename);
201 // rules can be a string : yaml filename
202 if (is_string($rules)) {
203 $file = $rules; // keep the real filename
204 $rules = $this->loadFile($file, $filepath);
205 $filepath = dirname($file) . '/';
208 // rules can be an array of rules
209 if (is_array($rules) and count($rules)) {
210 # cast array-rules to objects
211 foreach ($rules as $i => $rule) {
212 if (is_array($rule)) {
213 $rules[$i] = new TextWheelRule($rule);
215 // load subwheels when necessary
216 if ($rules[$i]->is_wheel
) {
217 // subruleset is of the same class as current ruleset
218 $class = get_class($this);
219 $rules[$i]->replace
= new $class($rules[$i]->replace
, $filepath);
222 $this->data
= array_merge($this->data
, $rules);
223 $this->sorted
= false;
228 * Sort rules according to priority and
229 * purge disabled rules
232 protected function sort() {
233 if (!$this->sorted
) {
235 foreach ($this->data
as $index => $rule) {
236 if (!$rule->disabled
) {
237 $rulz[intval($rule->priority
)][$index] = $rule;
241 $this->data
= array();
242 foreach ($rulz as $rules) {
243 $this->data +
= $rules;
246 $this->sorted
= true;