You are here

function _pathologic_abs_regex in Pathologic 6.2

Same name and namespace in other branches
  1. 5 pathologic.module \_pathologic_abs_regex()
  2. 6 pathologic.module \_pathologic_abs_regex()

Return the hard part of the regular expression to be used when making paths relative.

Parameters

$attr: The attribute holding paths we're looking for. Should be 'href' or 'src'.

$format: The ID of the input format being used.

Return value

string Part of a regular expression to be used to find paths to act upon.

1 call to _pathologic_abs_regex()
pathologic_filter in ./pathologic.module
Implementation of hook_filter().

File

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

Code

function _pathologic_abs_regex($attr, $format) {
  static $pathstr = FALSE;
  if ($pathstr === FALSE) {
    $paths_field = trim(variable_get('filter_pathologic_abs_paths_' . $format, ''));
    if ($paths_field !== '') {

      // Get rid of spirious white space on each line.
      $paths = array_map('trim', explode("\n", $paths_field));
    }
    else {
      $paths = array();
    }
    $paths[] = _pathologic_url('<front>');

    // It's possible the user entered the path for the current site in the box,
    // especially if the DB contents are shared between two different servers
    // (likely if it's a testing/production server thing). This won't screw up
    // the regex, but it will unnecessarily complicate it, so let's remove
    // duplicates from the array.
    $paths = array_unique($paths);

    // We need to account for the fact that http://example.com/foo,
    // http://example.com/?q=foo and http://example.com/index.php?q=foo are all
    // valid.
    $pathstr = ')="(' . implode('/?(index.php)?(\\?q=)?|', $paths) . '/?(index.php)?(\\?q=)?)([^"]*)"`';

    // $pathstr now looks like:
    // )="(http://example.com//?(index.php)?(\?q=)?|http://example.org//?(index.php)?(\?q=)?)([^"]*)";
  }
  return '`(' . $attr . $pathstr;

  // Returned value looks like:
  // (href)="(http://example.com//?(index.php)?(\?q=)?|http://example.org//?(index.php)?(\?q=)?)([^"]*)"
  // We want to match the attribute so that the callback
  // _pathologic_abs_to_rel() below can return a value with the attribute
  // without special trickery or creating duplicate functions.
}