You are here

function _pathologic_replace in Pathologic 7

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

Replace the attributes. preg_replace_callback() callback.

File

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

Code

function _pathologic_replace($matches, $absolute) {

  // First, "files:" support. This is fairly easy.
  if ($matches[2] === 'files:') {
    return $matches[1] . '="' . file_create_url(file_build_uri(urldecode($matches[3]))) . '"';
  }

  // 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']);
    }
    foreach ($qparts as $key => $qpart) {
      if ($qpart === '') {

        // In a query string, this is a key for which there isn't a value; for
        // example, the "baz" in "foo=bar&baz&qux=quux". In order for url() (or,
        // more specifically, drupal_http_build_query()) to handle this
        // correctly, it should have a value of NULL.
        $qparts[$key] = NULL;
      }
    }
  }
  else {
    $qparts = NULL;
  }
  $url = url($parts['path'], array(
    'query' => $qparts,
    'fragment' => isset($parts['fragment']) ? $parts['fragment'] : NULL,
    'absolute' => $absolute,
  ));

  // $matches[1] will be the attribute; src, href, etc.
  return "{$matches[1]}=\"{$url}\"";
}