You are here

function persiantools_convert_sm in PersianTools 7

Main function for multiple features and fixes of persiantools module.

1 call to persiantools_convert_sm()
persiantools_convert_data in ./persiantools.module

File

./persiantools.module, line 113
Adds common features and fixes for persian pages.

Code

function persiantools_convert_sm($str) {
  $is_all_en = TRUE;
  $any_en = FALSE;
  $digit_method = variable_get('persiantools_digit_method');
  $rtl_ltr_fix = variable_get('persiantools_rtl_ltr_fix');
  $closing_ch = '\\0';
  $paren_state = 0;
  $dir = PERSIANTOOLS_CONST::RTL;
  $len = drupal_strlen($str);
  for ($i = 0; $i < $len; $i = $i + 1) {
    $ch = drupal_substr($str, $i, 1);

    // Skip unicode characters, which might be mistaken for english characters
    // (e.g. &nbsp;).
    if ($ch == '&') {
      for ($j = $i; $j < $len; $j = $j + 1) {
        if (drupal_substr($str, $j, 1) == ';') {
          $i = $j;
          continue 2;
        }
        elseif ($j > $i + 9) {
          break;
        }
      }
    }
    $type = persiantools_get_char_type($ch);
    if ($type == PERSIANTOOLS_CONST::FA) {
      $is_all_en = FALSE;
    }
    elseif ($ch == $closing_ch) {
      $type = PERSIANTOOLS_CONST::CLOSING;
    }
    elseif ($type == PERSIANTOOLS_CONST::UN) {

      // Last char should go through anyway, to wrap things up.
      if ($i != $len - 1) {
        continue;
      }
    }
    if ($rtl_ltr_fix) {
      list($str, $changed) = persiantools_fix_mixed_path($str, $ch, $type, $i, $len);
      if ($changed) {
        $i += 5;
        $len += 5;
      }
    }
    switch ($type) {
      case PERSIANTOOLS_CONST::DIGIT:
        if ($digit_method == 'full' || $digit_method == 'smart' && $dir == PERSIANTOOLS_CONST::RTL && !($is_all_en && $any_en)) {
          $new_digit = PERSIANTOOLS_CONST::$FA_DIGITS[$ch - '0'];
          $str = drupal_substr($str, 0, $i) . $new_digit . drupal_substr($str, $i + 1);
          $len += drupal_strlen($new_digit) - 1;
          $i += drupal_strlen($new_digit) - 1;
        }
        break;
      case PERSIANTOOLS_CONST::EN:
        $dir = PERSIANTOOLS_CONST::LTR;
        $any_en = TRUE;
        break;
      case PERSIANTOOLS_CONST::FA:
        $dir = PERSIANTOOLS_CONST::RTL;
        break;
      case PERSIANTOOLS_CONST::OPENING:
        $opening_pos = $i;
        $paren_state = 1;
        break;
    }

    // Fix misplaced enclosing chars, like paranthesis, bracket, quotation, ...
    if ($rtl_ltr_fix) {
      switch ($paren_state) {
        case 1:
          $pre_open = $dir;
          $closing_ch = persiantools_get_closing_char($ch);
          $paren_state = 2;
          break;
        case 2:
          if ($type == PERSIANTOOLS_CONST::CLOSING) {

            // Fix misplaced empty enclosing chars, like function calls, array
            // access, ...
            if ($dir == PERSIANTOOLS_CONST::LTR) {
              $str = persiantools_insert_str($str, '&lrm;', $i + 1);
              $len += 5;
              $i += 5;
            }
            $paren_state = 0;
          }
          elseif ($type == PERSIANTOOLS_CONST::EN || $type == PERSIANTOOLS_CONST::FA) {
            $post_open = $dir;
            $paren_state = 3;
          }
          break;
        case 3:
          if ($type == PERSIANTOOLS_CONST::CLOSING) {
            $pre_close = $dir;
            $paren_state = 4;
            $closed_pos = $i;
          }
          break;
        case 4:
          if ($type == PERSIANTOOLS_CONST::EN || $type == PERSIANTOOLS_CONST::FA) {
            $post_close = $dir;
            $paren_state = 5;
          }
          break;
      }
      if ($paren_state == 4 && $i == $len - 1) {
        $post_close = PERSIANTOOLS_CONST::RTL;
        $paren_state = 5;
      }
      if ($paren_state == 5) {
        if ($pre_open == $post_open) {
          $open_dir = $pre_open;
        }
        else {
          $open_dir = PERSIANTOOLS_CONST::RTL;
        }
        if ($pre_close == $post_close) {
          $close_dir = $pre_close;
        }
        else {
          $close_dir = PERSIANTOOLS_CONST::RTL;
        }
        if ($open_dir != $close_dir) {
          if ($pre_open == PERSIANTOOLS_CONST::RTL) {
            $str = persiantools_insert_str($str, '&rlm;', $closed_pos);
            $len += 5;
            $i += 5;
          }
          elseif ($pre_open == PERSIANTOOLS_CONST::LTR) {

            // &#8234; lre (Left to Right Embedding).
            // &#8236: pdf (Pop Directional Formatting).
            $str = persiantools_insert_str($str, '&#8234;', $opening_pos);
            $str = persiantools_insert_str($str, '&#8236;', $closed_pos + 8);
            $len += 14;
            $i += 14;
          }
          $paren_state = 0;
        }
      }

      // Fix misplaced dot in English Sentences inside RTL direction.
      if ($is_all_en && $dir == PERSIANTOOLS_CONST::LTR && $type == PERSIANTOOLS_CONST::EOS) {
        if ($i < $len - 1) {
          $next_ch = drupal_substr($str, $i + 1, 1);
          $next_type = persiantools_get_char_type($next_ch);
          if ($next_type == PERSIANTOOLS_CONST::EN) {
            continue;
          }
        }
        $str = persiantools_insert_str($str, '&lrm;', $i + 1);
        $i += 5;
        $len += 5;
      }
    }
  }
  return $str;
}