You are here

public static function SmartyPants::smartNumbers in Typogrify 8

Adding space in numbers for easier reading aka digit grouping.

Parameters

string $text: The string to prettify.

int $attr: The kind of space to use.

array $ctx: Not used.

Return value

string The modified text.

1 call to SmartyPants::smartNumbers()
TypogrifyFilter::process in src/Plugin/Filter/TypogrifyFilter.php
Performs the filter processing.

File

src/SmartyPants.php, line 640

Class

SmartyPants
SmartyPants - Smart punctuation for web sites.

Namespace

Drupal\typogrify

Code

public static function smartNumbers($text, $attr = 0, array $ctx = NULL) {
  $tokens;
  $tokens = self::tokenizeHtml($text);
  if ($attr == 0) {
    return $text;
  }
  elseif ($attr == 1) {
    $method = 'numberNarrownbsp';
  }
  elseif ($attr == 2) {
    $method = 'numberThinsp';
  }
  elseif ($attr == 3) {
    $method = 'numberSpan';
  }
  elseif ($attr == 4) {
    $method = 'numberJustSpan';
  }
  $result = '';

  // Keep track of when we're inside <pre> or <code> tags.
  $in_pre = 0;
  $span_stop = 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(self::SMARTYPANTS_TAGS_TO_SKIP, $cur_token[1], $matches)) {
        $in_pre = isset($matches[1]) && $matches[1] == '/' ? 0 : 1;
      }
      elseif (preg_match('/<span .*class="[^"]*\\b(number|phone|ignore)\\b[^"]*"/', $cur_token[1], $matches)) {
        $span_stop = isset($matches[1]) && $matches[1] == '/' ? 0 : 1;
      }
      elseif ($cur_token[1] == '</span>') {
        $span_stop = 0;
      }
    }
    else {
      $t = $cur_token[1];
      if (!$in_pre && !$span_stop) {
        $number_finder = '@(?:(&#\\d{2,4};|&(#x[0-9a-fA-F]{2,4}|frac\\d\\d);)|' . '(\\d{4}-\\d\\d-\\d\\d)|(\\d\\d\\.\\d\\d\\.\\d{4})|' . '(0[ \\d\\-/]+)|([+-]?\\d+)([.,]\\d+|))@';
        $t = preg_replace_callback($number_finder, [
          __CLASS__,
          $method,
        ], $t);
      }
      $result .= $t;
    }
  }
  return $result;
}