/** * Perform character conversion. * * @param string $test * Text to be parsed. * @param array $characters_to_convert * Array of ASCII characters to convert. * @return string * The result of the conversion. */ function convert_characters($text, $characters_to_convert) { if ($characters_to_convert == NULL || count($characters_to_convert) < 1) { // do nothing return $text; } // get ascii to unicode mappings $unicode_map = unicode_conversion_map(); foreach ($characters_to_convert as $ascii_string) { $unicode_strings[] = $unicode_map[$ascii_string]; } $tokens = _TokenizeHTML($text); $result = ''; $in_pre = 0; // Keep track of when we're inside <pre> or <code> tags foreach ($tokens as $cur_token) { if ($cur_token[0] == "tag") { // Don't mess with text inside tags, <pre> blocks, or <code> blocks $result .= $cur_token[1]; // Get the tags to skip regex from SmartyPants if (preg_match(SMARTYPANTS_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; }
/** * SmartEllipses. * * @param string $text * Text to be parsed. * @param string $attr * Value of the smart_quotes="" attribute. * @param string $ctx * MT context object (unused). * * @return string * $text with replacements. */ function SmartEllipses($text, $attr = NULL, $ctx = NULL) { if ($attr == NULL) { global $_typogrify_smartypants_attr; $attr = $_typogrify_smartypants_attr; } if ($attr == 0) { // Do nothing; return $text; } $tokens; $tokens = _TokenizeHTML($text); $result = ''; // Keep track of when we're inside <pre> or <code> tags. $in_pre = 0; foreach ($tokens as $cur_token) { if ($cur_token[0] == "tag") { // Don't mess with quotes inside tags. $result .= $cur_token[1]; if (preg_match(SMARTYPANTS_TAGS_TO_SKIP, $cur_token[1], $matches)) { $in_pre = isset($matches[1]) && $matches[1] == '/' ? 0 : 1; } } else { $t = $cur_token[1]; if (!$in_pre) { $t = ProcessEscapes($t); $t = EducateEllipses($t); } $result .= $t; } } return $result; }
function SmartEllipses($text, $attr = NULL, $ctx = NULL) { # Paramaters: $text; # text to be parsed $attr; # value of the smart_ellipses="" attribute $ctx; # MT context object (unused) if ($attr == NULL) { $attr = $smartypants_attr; } if ($attr == 0) { # do nothing; return $text; } $tokens; $tokens = _TokenizeHTML($text); $result = ''; $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags foreach ($tokens as $cur_token) { if ($cur_token[0] == "tag") { # Don't mess with quotes inside tags $result .= $cur_token[1]; 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) { $t = ProcessEscapes($t); $t = EducateEllipses($t); } $result .= $t; } } return $result; }
function SmartyPants($text = '', $mode = DEFAULT_OPERATION_MODE) { // quick return for empty string if ($text == '') { return ''; } // default all commands to FALSE, then reset according to $mode $do_quotes = $do_backticks_double = $do_backticks_all = FALSE; $do_dashes = $do_oldschool_dashes = $do_inverted_oldschool_dashes = FALSE; $do_ellipses = $do_stupefy = $convert_quot = FALSE; // setting these flags introduces a layer of abstraction that turned out // to be unnecessary. Such is porting. if ($mode & DO_QUOTES) { $do_quotes = TRUE; } if ($mode & DO_BACKTICKS_DOUBLE) { $do_backticks_double = TRUE; } if ($mode & DO_BACKTICKS_ALL) { $do_backticks_all = TRUE; } if ($mode & DO_DASHES) { $do_dashes = TRUE; } if ($mode & DO_OLDSCHOOL_DASHES) { $do_oldschool_dashes = TRUE; } if ($mode & DO_INVERTED_OLDSCHOOL_DASHES) { $do_inverted_oldschool_dashes = TRUE; } if ($mode & DO_ELLIPSES) { $do_ellipses = TRUE; } if ($mode & DO_QUOT_CONV) { $convert_quot = TRUE; } // tokenize input string -- break it into HTML tags and the text between them. $tokens = array(); _tokenize($text, $tokens); $result = ''; $in_pre = FALSE; # Keep track of when we're inside <pre> or <code> tags. $prev_token_last_char = ''; # This is a cheat, used to get some context # for one-character tokens that consist of # just a quote char. What we do is remember # the last character of the previous text # token, to use as context to curl single- # character quote tokens correctly. foreach ($tokens as $data) { if ($data['type'] == TOKENS_TYPE_TAG) { # Don't mess with quotes inside tags. $result .= $data['body']; // if the current tag contains text that should not be // modified, set $in_pre to TRUE if (preg_match(TAGS_TO_SKIP, $data['body'], $hits)) { $in_pre = $hits[1] == '' ? TRUE : FALSE; } } else { $t = $data['body']; $last_char = substr($t, -1); # Remember last char of this token before processing. if (!$in_pre) { $t = ProcessEscapes($t); if ($convert_quot) { $t = str_replace('"', '"', $t); } if ($do_dashes) { $t = EducateDashes($t); } elseif ($do_oldschool_dashes) { $t = EducateDashesOldSchool($t); } elseif ($do_inverted_oldschool_dashes) { $t = EducateDashesOldSchoolInverted($t); } if ($do_ellipses) { $t = EducateEllipses($t); } # Note: backticks need to be processed before quotes. if ($do_backticks_double || $do_backticks_all) { $t = EducateBackticks($t); } if ($do_backticks_all) { $t = EducateSingleBackticks($t); } if ($do_quotes) { if ($t == "'") { # Special case: single-character ' token if (preg_match("/\\S/", $prev_token_last_char)) { $t = "’"; } else { $t = "‘"; } } elseif ($t == '"') { # Special case: single-character " token if (preg_match("/\\S/", $prev_token_last_char)) { $t = "”"; } else { $t = "“"; } } else { $t = EducateQuotes($t); } } if ($do_stupefy) { $t = StupefyEntities($t); } } $prev_token_last_char = $last_char; $result .= $t; } } return $result; }