You are here

function persiantools_post_render_rtl in PersianTools 8

1 string reference to 'persiantools_post_render_rtl'
persiantools_preprocess_html in ./persiantools.module
Implements hook_preprocess_html().

File

./persiantools.module, line 18

Code

function persiantools_post_render_rtl($html, array $elements) {
  $html_page = (string) $html;
  $digit_method = \Drupal::config('persiantools.settings')
    ->get('digit_method');
  $rtlmaker = \Drupal::config('persiantools.settings')
    ->get('rtlmaker');
  $skip_tags = '(<(textarea|script|style)( [^>]*)?>(.*?)<\\/\\3>\\s*)';
  $normal_tags = '(<\\/?[^>]+>\\s*)?([^<]*)(?=<)';
  $chained_tags = '/(?s)(' . $skip_tags . '|' . $normal_tags . ')([^<]*)(?=<)/';
  preg_match_all($chained_tags, $html_page, $matches, PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER);
  $new_html_page = '';
  for ($i = 0; $i < count($matches[0]); $i++) {
    if (strlen($matches[7][$i][0]) + strlen($matches[8][$i][0]) > 0) {
      $matches[7][$i][0] = RTLMaker::convert_sm($matches[7][$i][0] . $matches[8][$i][0], $digit_method, $rtlmaker);
    }
    $new_html_page .= $matches[2][$i][0] . $matches[6][$i][0] . $matches[7][$i][0];
  }
  $last = end($matches[0]);
  $last_pos = strlen($last[0]) + $last[1];
  if ($last_pos < strlen($html_page)) {
    $new_html_page .= substr($html_page, $last_pos);
  }
  return $new_html_page;
}