View source
<?php
if (!function_exists('SmartyPants')) {
require_once dirname(__FILE__) . '/smartypants.php';
}
global $sp_tags_to_skip;
$sp_tags_to_skip = '<(/?)(?:pre|code|kbd|script|math)[\\s>]';
global $ligature_map;
$ligature_map = array(
"ffi" => "ffi",
"ffl" => "ffl",
"ff" => "ff",
"fi" => "fi",
"fl" => "fl",
'ij' => 'ij',
'IJ' => 'IJ',
"st" => "st",
"ss" => "ß",
);
global $punctuation_map;
$punctuation_map = array(
"..." => "…",
".." => "‥",
". . ." => "…",
"---" => "—",
"--" => "–",
);
global $arrow_map;
$arrow_map = array(
"->>" => "↠",
"<<-" => "↞",
"->|" => "⇥",
"|<-" => "⇤",
"<->" => "↔",
"->" => "→",
"<-" => "←",
"<=>" => "⇔",
"=>" => "⇒",
"<=" => "⇐",
);
global $unicode_map;
$unicode_map = array_merge($ligature_map, $arrow_map, $punctuation_map);
function convert_characters($text, $characters_to_convert) {
if ($characters_to_convert == NULL || count($characters_to_convert) < 1) {
return $text;
}
global $unicode_map;
foreach ($characters_to_convert as $ascii_string) {
$unicode_strings[] = $unicode_map[$ascii_string];
}
$tokens = _TokenizeHTML($text);
$result = '';
$in_pre = 0;
foreach ($tokens as $cur_token) {
if ($cur_token[0] == "tag") {
$result .= $cur_token[1];
global $sp_tags_to_skip;
if (preg_match("@{$sp_tags_to_skip}@", $cur_token[1], $matches)) {
$in_pre = isset($matches[1]) && $matches[1] == '/' ? 0 : 1;
}
}
else {
$t = $cur_token[1];
if ($in_pre == 0) {
$t = ProcessEscapes($t);
$t = str_replace($characters_to_convert, $unicode_strings, $t);
}
$result .= $t;
}
}
return $result;
}
if (!function_exists('_TokenizeHTML')) {
function _TokenizeHTML($str) {
$index = 0;
$tokens = array();
$match = '(?s:<!(?:--.*?--\\s*)+>)|' . '(?s:<\\?.*?\\?>)|' . '(?:<[/!$]?[-a-zA-Z0-9:]+\\b(?>[^"\'>]+|"[^"]*"|\'[^\']*\')*>)';
$parts = preg_split("{({$match})}", $str, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($parts as $part) {
if (++$index % 2 && $part != '') {
$tokens[] = array(
'text',
$part,
);
}
else {
$tokens[] = array(
'tag',
$part,
);
}
}
return $tokens;
}
}