You are here

public static function Typogrify::caps in Typogrify 5

Same name and namespace in other branches
  1. 6 typogrify.class.php \Typogrify::caps()
  2. 7 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

File

./typogrify.class.php, line 64
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[A-Z\\d]*        # Group 2: Any amount of caps and digits\n            [A-Z]\\d*[A-Z]      # A cap string much at least include two caps (but they can have digits between them)\n            [A-Z\\d]*\\b)        # Any amount of caps and digits\n            | (\\b[A-Z]+\\.\\s?   # OR: Group 3: Some caps, followed by a '.' and an optional space\n            (?:[A-Z]+\\.\\s?)+)  # Followed by the same thing at least once more\n            (?:\\s|\\b|\$))/x";
  $tags_to_skip_regex = "/<(\\/)?(?:pre|code|kbd|script|math)[^>]*>/i";
  foreach ($tokens as $token) {
    if ($token[0] == "tag") {

      // Don't mess with tags.
      $result[] = $token[1];
      $close_match = preg_match($tags_to_skip_regex, $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);
}