You are here

function typogrify_smart_abbreviation in Typogrify 7

Wrapping abbreviations and adding half space between digit grouping.

Parameters

string $text: to work on.

int $attr: kind of space to use.

array $ctx: not used.

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

File

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

Code

function typogrify_smart_abbreviation($text, $attr = 0, $ctx = NULL) {
  $tokens;
  $tokens = _TokenizeHTML($text);
  $replace_method = '_typogrify_abbr_asis';
  if ($attr == 1) {
    $replace_method = '_typogrify_abbr_narrownbsp';
  }
  elseif ($attr == 2) {
    $replace_method = '_typogrify_abbr_thinsp';
  }
  elseif ($attr == 3) {
    $replace_method = '_typogrify_abbr_span';
  }
  $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) {
        $abbr_finder = '/(?<=\\s|)(\\p{L}+\\.)(\\p{L}+\\.)+(?=\\s|)/u';
        $t = preg_replace_callback($abbr_finder, $replace_method, $t);
      }
      $result .= $t;
    }
  }
  return $result;
}