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')) return;
23 require_once dirname(__FILE__
)."/textwheelrule.php";
25 abstract class TextWheelDataSet
{
27 protected $data = array();
30 * file finder : can be overloaded in order to use application dependant
37 protected function findFile(&$file, $path=''){
40 // absolute file path ?
41 if (file_exists($file))
44 // file embed with texwheels, relative to calling ruleset
45 if ($path AND file_exists($f = $path.$file))
48 // textwheel default path ?
50 $default_path = dirname(__FILE__
).'/../wheels/';
51 if (file_exists($f = $default_path.$file))
58 * Load a yaml file describing data
60 * @param string $default_path
63 protected function loadFile(&$file, $default_path='') {
64 if (!preg_match(',[.]yaml$,i',$file)
66 OR !$file = $this->findFile($file,$default_path))
69 defined('_YAML_EVAL_PHP') ||
define('_YAML_EVAL_PHP', false);
70 if (!function_exists('yaml_decode')) {
71 if (function_exists('include_spip'))
72 include_spip('inc/yaml-mini');
74 require_once dirname(__FILE__
).'/../inc/yaml.php';
76 $dataset = yaml_decode(file_get_contents($file));
78 if (is_null($dataset))
80 # throw new DomainException('yaml file is empty, unreadable or badly formed: '.$file.var_export($dataset,true));
82 // if a php file with same name exists
83 // include it as it contains callback functions
84 if ($f = preg_replace(',[.]yaml$,i','.php',$file)
85 AND file_exists($f)) {
86 $dataset[] = array('require' => $f, 'priority' => -1000);
93 class TextWheelRuleSet
extends TextWheelDataSet
{
95 protected $sorted = true;
100 * @param array|string $ruleset
101 * @param string $filepath
103 public function TextWheelRuleSet($ruleset = array(), $filepath='') {
105 $this->addRules($ruleset, $filepath);
109 * public static loader
110 * can be overloaded to use memoization
112 * @param array $ruleset
113 * @param string $callback
114 * @param string $class
117 public static function &loader($ruleset, $callback='', $class='TextWheelRuleSet'){
119 $ruleset = new $class($ruleset);
126 * Get an existing named rule in order to override it
128 * @param string $name
131 public function &getRule($name){
132 if (isset($this->data
[$name]))
133 return $this->data
[$name];
142 public function &getRules(){
150 * @param TextWheelRule $rule
152 public function addRule($rule) {
153 # cast array-rule to object
155 $rule = new TextWheelRule($rule);
156 $this->data
[] = $rule;
157 $this->sorted
= false;
161 * add an list of rules
163 * - an array of rules
164 * - a string filename
165 * - an array of string filename
167 * @param array|string $rules
168 * @param string $filepath
170 public function addRules($rules, $filepath='') {
171 // rules can be an array of filename
172 if (is_array($rules) AND is_string(reset($rules))) {
173 foreach($rules as $i=>$filename)
174 $this->addRules($filename);
178 // rules can be a string : yaml filename
179 if (is_string($rules)) {
180 $file = $rules; // keep the real filename
181 $rules = $this->loadFile($file, $filepath);
182 $filepath = dirname($file).'/';
185 // rules can be an array of rules
186 if (is_array($rules) AND count($rules)){
187 # cast array-rules to objects
188 foreach ($rules as $i => $rule) {
190 $rules[$i] = new TextWheelRule($rule);
191 // load subwheels when necessary
192 if ($rules[$i]->is_wheel
){
193 // subruleset is of the same class as current ruleset
194 $class = get_class($this);
195 $rules[$i]->replace
= new $class($rules[$i]->replace
, $filepath);
198 $this->data
= array_merge($this->data
, $rules);
199 $this->sorted
= false;
204 * Sort rules according to priority and
205 * purge disabled rules
208 protected function sort() {
209 if (!$this->sorted
) {
211 foreach($this->data
as $index => $rule)
212 if (!$rule->disabled
)
213 $rulz[intval($rule->priority
)][$index] = $rule;
215 $this->data
= array();
216 foreach($rulz as $rules)
217 $this->data +
= $rules;
219 $this->sorted
= true;