You are here

function SmartQuotes in Typogrify 5

Same name and namespace in other branches
  1. 6 smartypants.php \SmartQuotes()
  2. 7 smartypants.php \SmartQuotes()

File

./smartypants.php, line 223

Code

function SmartQuotes($text, $attr = NULL, $ctx = NULL) {
  global $smartypants_attr, $sp_tags_to_skip;

  # Paramaters:
  $text;

  # text to be parsed
  $attr;

  # value of the smart_quotes="" attribute
  $ctx;

  # MT context object (unused)
  if ($attr == NULL) {
    $attr = $smartypants_attr;
  }
  $do_backticks;

  # should we educate ``backticks'' -style quotes?
  if ($attr == 0) {

    # do nothing;
    return $text;
  }
  else {
    if ($attr == 2) {

      # smarten ``backticks'' -style quotes
      $do_backticks = 1;
    }
    else {
      $do_backticks = 0;
    }
  }

  # Special case to handle quotes at the very end of $text when preceded by

  # an HTML tag. Add a space to give the quote education algorithm a bit of

  # context, so that it can guess correctly that it's a closing quote:
  $add_extra_space = 0;
  if (preg_match("/>['\"]\\z/", $text)) {
    $add_extra_space = 1;

    # Remember, so we can trim the extra space later.
    $text .= " ";
  }
  $tokens = _TokenizeHTML($text);
  $result = '';
  $in_pre = 0;

  # Keep track of when we're inside <pre> or <code> tags
  $prev_token_last_char = "";

  # This is a cheat, used to get some context

  # for one-character tokens that consist of

  # just a quote char. What we do is remember

  # the last character of the previous text

  # token, to use as context to curl single-

  # character quote tokens correctly.
  foreach ($tokens as $cur_token) {
    if ($cur_token[0] == "tag") {

      # Don't mess with quotes inside tags
      $result .= $cur_token[1];
      if (preg_match("@{$sp_tags_to_skip}@", $cur_token[1], $matches)) {
        $in_pre = isset($matches[1]) && $matches[1] == '/' ? 0 : 1;
      }
    }
    else {
      $t = $cur_token[1];
      $last_char = substr($t, -1);

      # Remember last char of this token before processing.
      if (!$in_pre) {
        $t = ProcessEscapes($t);
        if ($do_backticks) {
          $t = EducateBackticks($t);
        }
        if ($t == "'") {

          # Special case: single-character ' token
          if (preg_match('/\\S/', $prev_token_last_char)) {
            $t = "&#8217;";
          }
          else {
            $t = "&#8216;";
          }
        }
        else {
          if ($t == '"') {

            # Special case: single-character " token
            if (preg_match('/\\S/', $prev_token_last_char)) {
              $t = "&#8221;";
            }
            else {
              $t = "&#8220;";
            }
          }
          else {

            # Normal case:
            $t = EducateQuotes($t);
          }
        }
      }
      $prev_token_last_char = $last_char;
      $result .= $t;
    }
  }
  if ($add_extra_space) {
    preg_replace('/ \\z/', '', $result);

    # Trim trailing space if we added one earlier.
  }
  return $result;
}