You are here

public static function SmartyPants::spaceToNbsp in Typogrify 8

Space to nbsp.

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

Parameters

string $text: The text to work on.

Return value

string The modified text.

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

File

src/SmartyPants.php, line 1251

Class

SmartyPants
SmartyPants - Smart punctuation for web sites.

Namespace

Drupal\typogrify

Code

public static function spaceToNbsp($text) {
  $tokens;
  $tokens = self::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(self::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;
}