You are here

function SmartDashes in Typogrify 5

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

File

./smartypants.php, line 317

Code

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

  # Paramaters:
  $text;

  # text to be parsed
  $attr;

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

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

  # reference to the subroutine to use for dash education, default to EducateDashes:
  $dash_sub_ref = 'EducateDashes';
  if ($attr == 0) {

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

      # use old smart dash shortcuts, "--" for en, "---" for em
      $dash_sub_ref = 'EducateDashesOldSchool';
    }
    else {
      if ($attr == 3) {

        # inverse of 2, "--" for em, "---" for en
        $dash_sub_ref = 'EducateDashesOldSchoolInverted';
      }
    }
  }
  $tokens;
  $tokens = _TokenizeHTML($text);
  $result = '';
  $in_pre = 0;

  # Keep track of when we're inside <pre> or <code> tags
  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];
      if (!$in_pre) {
        $t = ProcessEscapes($t);
        $t = $dash_sub_ref($t);
      }
      $result .= $t;
    }
  }
  return $result;
}