You are here

function typogrify_space_to_nbsp in Typogrify 7

space_to_nbsp

Replaces the space before a "double punctuation mark" (!?:;) with `` `` Especially useful in french.

1 call to typogrify_space_to_nbsp()
_typogrify_process in ./typogrify.module
Processing function to apply the Typogrify filters.

File

./smartypants.php, line 1104
SmartyPants - Smart punctuation for web sites

Code

function typogrify_space_to_nbsp($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 = preg_replace("/\\s([\\!\\?\\:;])/", '&nbsp;$1', $t);
      }
      $result .= $t;
    }
  }
  return $result;
}