You are here

public static function Typogrify::caps in Typogrify 7

Same name and namespace in other branches
  1. 5 typogrify.class.php \Typogrify::caps()
  2. 6 typogrify.class.php \Typogrify::caps()

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.

2 calls to Typogrify::caps()
Typogrify::filter in ./typogrify.class.php
typogrify
_typogrify_process in ./typogrify.module
Processing function to apply the Typogrify filters.

File

./typogrify.class.php, line 65
typogrify.class.php Defines a class for providing different typographical tweaks to HTML

Class

Typogrify
@file typogrify.class.php Defines a class for providing different typographical tweaks to HTML

Code

public static function caps($text) {

  // If _TokenizeHTML from Smartypants is not present, don't do anything.
  if (!function_exists('_TokenizeHTML')) {
    return $text;
  }
  $tokens = _TokenizeHTML($text);
  $result = array();
  $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_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, array(
          'Typogrify',
          '_cap_wrapper',
        ), $token[1]);
      }
    }
  }
  return join("", $result);
}