You are here

function persiantools_fix_mixed_path in PersianTools 7

Fix mixed-up paths in rtl blocks. Logic: Gets triggered once a starting '.' or '/' is detected after a whitespace. The correcting symbol is inserted once an english char is seen inside the path.

1 call to persiantools_fix_mixed_path()
persiantools_convert_sm in ./persiantools.module
Main function for multiple features and fixes of persiantools module.

File

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

Code

function persiantools_fix_mixed_path($str, $ch, $type, $i, $len) {
  static $maybe_path = TRUE;
  static $is_path = FALSE;
  static $path_pos = -1;
  $changed = FALSE;
  if ($i == 0) {
    $maybe_path = TRUE;
  }
  if ($type == PERSIANTOOLS_CONST::WS) {
    $maybe_path = TRUE;
  }
  elseif ($is_path) {
    if ($type == PERSIANTOOLS_CONST::EN) {
      $str = persiantools_insert_str($str, '‎', $path_pos);
      $changed = TRUE;
    }
    $path_pos = -1;
    $is_path = FALSE;
  }
  elseif ($maybe_path) {
    if ($type == PERSIANTOOLS_CONST::SLASH) {
      $is_path = TRUE;
      if ($path_pos < 0) {
        $path_pos = $i;
      }
    }
    elseif ($ch == '.') {
      if ($path_pos < 0) {
        $path_pos = $i;
      }
    }
    else {
      $maybe_path = FALSE;
      $is_path = FALSE;
      $path_pos = -1;
    }
  }

  // Detect trailing slashes in paths.
  if ($type == PERSIANTOOLS_CONST::SLASH && $i > 0) {
    $prev_ch = drupal_substr($str, $i - 1, 1);
    $prev_ch_type = persiantools_get_char_type($prev_ch);
    $is_last_char = $i == $len - 1;
    if ($prev_ch_type == PERSIANTOOLS_CONST::EN && ($is_last_char || drupal_substr($str, $i + 1, 1) == ' ')) {
      $str = persiantools_insert_str($str, '&lrm;', $i + 1);
      $changed = TRUE;
    }
  }
  return array(
    $str,
    $changed,
  );
}