You are here

function _pathologic_replace in Pathologic 6.3

Same name and namespace in other branches
  1. 8 pathologic.module \_pathologic_replace()
  2. 7.3 pathologic.module \_pathologic_replace()
  3. 7 pathologic.module \_pathologic_replace()
  4. 7.2 pathologic.module \_pathologic_replace()

Replace the attributes. preg_replace_callback() callback.

File

./pathologic.module, line 100
Pathologic text filter for Drupal.

Code

function _pathologic_replace($matches, $absolute) {

  // Build the full URL, then take it apart
  $parts = parse_url('http://example.com/' . urldecode($matches[3]));
  if ($parts['path'] === '/' || $parts['path'] === '//') {

    // '//' will be the case if the original path was just a slash
    $parts['path'] = '<front>';
  }
  else {

    // Trim initial slash off path.
    $parts['path'] = drupal_substr($parts['path'], 1);
  }

  // Need to parse the query parts
  if (isset($parts['query'])) {
    parse_str($parts['query'], $qparts);
    if (isset($qparts['q'])) {
      $parts['path'] = $qparts['q'];
      unset($qparts['q']);
    }
  }
  else {
    $qparts = NULL;
  }

  // Attempt to correctly handle language issues
  // http://drupal.org/node/348421
  global $language;
  $lang_to_use = drupal_clone($language);
  if (!menu_get_item($parts['path'])) {

    // If it's not a menu item, don't mangle the path!
    // http://drupal.org/node/961618
    $lang_to_use->prefix = '';
  }
  $url = url($parts['path'], array(
    'query' => $qparts,
    'fragment' => isset($parts['fragment']) ? $parts['fragment'] : NULL,
    'absolute' => $absolute,
    'language' => $lang_to_use,
  ));

  // $matches[1] will be "src" or "href"
  return "{$matches[1]}=\"{$url}\"";
}