You are here

public static function Typogrify::caps in Typogrify 8

Stylable capitals.

Wraps multiple capital letters in ``<span class="caps">`` so they can be styled with CSS.

Uses the smartypants tokenizer to not screw with HTML or with tags it shouldn't.

3 calls to Typogrify::caps()
Typogrify::filter in src/TwigExtension/Typogrify.php
Filter text by Typogrify.
Typogrify::filter in src/Typogrify.php
Typogrify.
TypogrifyFilter::process in src/Plugin/Filter/TypogrifyFilter.php
Performs the filter processing.

File

src/Typogrify.php, line 79

Class

Typogrify
Class \Drupal\typogrify\Typogrify.

Namespace

Drupal\typogrify

Code

public static function caps($text) {
  $tokens = SmartyPants::tokenizeHtml($text);
  $result = [];
  $in_skipped_tag = FALSE;
  $cap_finder = "/(\n      (\\b[[\\p{Lu}=\\d]*       # Group 2: Any amount of caps and digits\n      [[\\p{Lu}][[\\p{Lu}\\d]*  # A cap string much at least include two caps (but they can have digits between them)\n      (?:&amp;)?             # allowing ampersand in caps.\n      [[\\p{Lu}'\\d]*[[\\p{Lu}\\d]) # Any amount of caps and digits\n      | (\\b[[\\p{Lu}]+\\.\\s?   # OR: Group 3: Some caps, followed by a '.' and an optional space\n      (?:[[\\p{Lu}]+\\.\\s?)+)  # Followed by the same thing at least once more\n      (\\s|\\b|\$|[)}\\]>]))/xu";
  foreach ($tokens as $token) {
    if ($token[0] == 'tag') {

      // Don't mess with tags.
      $result[] = $token[1];
      $close_match = preg_match(SmartyPants::SMARTYPANTS_TAGS_TO_SKIP, $token[1]);
      if ($close_match) {
        $in_skipped_tag = TRUE;
      }
      else {
        $in_skipped_tag = FALSE;
      }
    }
    else {
      if ($in_skipped_tag) {
        $result[] = $token[1];
      }
      else {
        $result[] = preg_replace_callback($cap_finder, 'self::capWrapper', $token[1]);
      }
    }
  }
  return implode('', $result);
}