Example #1
0
/**
 * Nettoie un texte, traite les raccourcis autre qu'URL, la typo, etc.
 *
 * http://code.spip.net/@traiter_raccourcis
 *
 * @param string $t
 * @param bool $show_autobr
 * @return string
 */
function traiter_raccourcis($t, $show_autobr = false)
{
    static $wheel, $notes;
    static $img_br_auto, $img_br_manuel, $img_br_no;
    // hack1: respecter le tag ignore br
    if (_AUTOBR_IGNORER and strncmp($t, _AUTOBR_IGNORER, strlen(_AUTOBR_IGNORER)) == 0) {
        $ignorer_autobr = true;
        $t = substr($t, strlen(_AUTOBR_IGNORER));
    } else {
        $ignorer_autobr = false;
    }
    // Appeler les fonctions de pre_traitement
    $t = pipeline('pre_propre', $t);
    if (!isset($wheel)) {
        $ruleset = SPIPTextWheelRuleset::loader($GLOBALS['spip_wheels']['raccourcis'], 'personnaliser_raccourcis');
        $wheel = new TextWheel($ruleset);
        if (_request('var_mode') == 'wheel' and autoriser('debug')) {
            $f = $wheel->compile();
            echo "<pre>\n" . spip_htmlspecialchars($f) . "</pre>\n";
            exit;
        }
        $notes = charger_fonction('notes', 'inc');
    }
    // Gerer les notes (ne passe pas dans le pipeline)
    list($t, $mes_notes) = $notes($t);
    $t = $wheel->text($t);
    // Appeler les fonctions de post-traitement
    $t = pipeline('post_propre', $t);
    if ($mes_notes) {
        $notes($mes_notes, 'traiter', $ignorer_autobr);
    }
    // hack2: wrap des autobr dans l'espace prive, pour affichage css
    // car en css on ne sait pas styler l'element BR
    if ($ignorer_autobr and _AUTOBR) {
        if (is_null($img_br_no)) {
            $img_br_no = $show_autobr ? http_img_pack("br-no-10.png", _T("tw:retour_ligne_ignore"), "class='br-no'", _T("tw:retour_ligne_ignore")) : "";
        }
        $t = str_replace(_AUTOBR, $img_br_no, $t);
    }
    if ($show_autobr and _AUTOBR) {
        if (is_null($img_br_manuel)) {
            $img_br_manuel = http_img_pack("br-manuel-10.png", _T("tw:retour_ligne_manuel"), "class='br-manuel'", _T("tw:retour_ligne_manuel"));
        }
        if (is_null($img_br_auto)) {
            $img_br_auto = http_img_pack("br-auto-10.png", _T("tw:retour_ligne_auto"), "class='br-auto'", _T("tw:retour_ligne_auto"));
        }
        if (false !== strpos(strtolower($t), '<br')) {
            $t = preg_replace("/<br\\b.*>/UiS", "{$img_br_manuel}\\0", $t);
            $t = str_replace($img_br_manuel . _AUTOBR, $img_br_auto . _AUTOBR, $t);
        }
    }
    return $t;
}
Example #2
0
 /**
  * Initializing a rule a first call
  * including file, creating function or wheel
  * optimizing tests
  *
  * @param TextWheelRule $rule
  */
 protected function initRule(&$rule)
 {
     # language specific
     if ($rule->require) {
         require_once $rule->require;
     }
     # optimization: strpos or stripos?
     if (isset($rule->if_str)) {
         if (strtolower($rule->if_str) !== strtoupper($rule->if_str)) {
             $rule->if_stri = $rule->if_str;
             $rule->if_str = null;
         }
     }
     if ($rule->create_replace) {
         $compile = $rule->replace . '($t)';
         $rule->replace = create_function('$m', $rule->replace);
         $this->compiled[$rule->replace] = $compile;
         $rule->create_replace = false;
         $rule->is_callback = true;
     } elseif ($rule->is_wheel) {
         $n = count(TextWheel::$subwheel);
         TextWheel::$subwheel[] = $this->createSubWheel($rule->replace);
         $var = '$m[' . intval($rule->pick_match) . ']';
         if ($rule->type == 'all' or $rule->type == 'str' or $rule->type == 'split' or !isset($rule->match)) {
             $var = '$m';
         }
         $code = 'return TextWheel::getSubWheel(' . $n . ')->text(' . $var . ');';
         $rule->replace = create_function('$m', $code);
         $cname = 'compiled_' . str_replace('-', '_', $rule->name);
         $compile = TextWheel::getSubWheel($n)->compile($cname);
         $this->compiled[$rule->replace] = $compile;
         $rule->is_wheel = false;
         $rule->is_callback = true;
     }
     # optimization
     $rule->func_replace = '';
     if (isset($rule->replace)) {
         switch ($rule->type) {
             case 'all':
                 $rule->func_replace = 'replace_all';
                 break;
             case 'str':
                 $rule->func_replace = 'replace_str';
                 // test if quicker strtr usable
                 if (!$rule->is_callback and is_array($rule->match) and is_array($rule->replace) and $c = array_map('strlen', $rule->match) and $c = array_unique($c) and count($c) == 1 and reset($c) == 1 and $c = array_map('strlen', $rule->replace) and $c = array_unique($c) and count($c) == 1 and reset($c) == 1) {
                     $rule->match = implode('', $rule->match);
                     $rule->replace = implode('', $rule->replace);
                     $rule->func_replace = 'replace_strtr';
                 }
                 break;
             case 'split':
                 $rule->func_replace = 'replace_split';
                 $rule->match = array($rule->match, is_null($rule->glue) ? $rule->match : $rule->glue);
                 break;
             case 'preg':
             default:
                 $rule->func_replace = 'replace_preg';
                 break;
         }
         if ($rule->is_callback) {
             $rule->func_replace .= '_cb';
         }
     }
     if (!method_exists("TextWheel", $rule->func_replace)) {
         $rule->disabled = true;
         $rule->func_replace = 'replace_identity';
     }
     # /end
 }
Example #3
0
 public function compile()
 {
     $rules =& $this->ruleset->getRules();
     ## apply each in order
     $comp = array();
     foreach ($rules as $name => $rule) {
         $this->initRule($rule);
         $r = "/* {$name} */\n";
         if ($rule->require) {
             $r .= 'require_once ' . TextWheel::export($rule->require) . ';' . "\n";
         }
         if ($rule->if_str) {
             $r .= 'if_str(' . TextWheel::export($rule->if_str) . ', $t)' . "\n";
         }
         if ($rule->if_stri) {
             $r .= 'if_stri(' . TextWheel::export($rule->if_stri) . ', $t)' . "\n";
         }
         if ($rule->if_match) {
             $r .= 'if_match(' . TextWheel::export($rule->if_match) . ', $t)' . "\n";
         }
         if ($rule->func_replace !== 'replace_identity') {
             $fun = 'TextWheel::' . $rule->func_replace;
             $r .= '$t = ' . $fun . '(' . TextWheel::export($rule->match) . ', ' . TextWheel::export($rule->replace) . ', $t);' . "\n";
         }
         $comp[] = $r;
     }
     return join("\n", $comp);
 }
Example #4
0
 /**
  * Apply all rules of RuleSet to a text
  *
  * @param string $t
  * @return string
  */
 public function &test()
 {
     $tests =& $this->testset->getTests();
     $results = array();
     ## apply each in order
     foreach ($tests as $i => $test) {
         #php4+php5
         if (!$test->disabled) {
             $results[$i] = array('test' => &$tests[$i]);
             $w = new TextWheel($test->ruleset);
             $output = $w->text($test->input);
             if ($output == $test->output) {
                 $results[$i]['result'] = true;
             } else {
                 $results[$i]['result'] = false;
                 $results[$i]['output'] = $output;
             }
         }
     }
     return $results;
 }