You are here

function freelinking_filter in Freelinking 6.3

Same name and namespace in other branches
  1. 5 freelinking.module \freelinking_filter()
  2. 6 freelinking.module \freelinking_filter()
  3. 6.2 freelinking.module \freelinking_filter()

Implementation of hook_filter().

File

./freelinking.module, line 36

Code

function freelinking_filter($op, $delta = 0, $format = -1, $text = '', $langcode = '', $cache_id = 0) {
  switch ($op) {
    case 'list':
      return array(
        0 => 'freelinking filter',
      );
    case 'no cache':
      return TRUE;
    case 'description':
      return t('Allows for a flexible format for linking content');
    case 'process':
      $freelinking = freelinking_get_plugins();
      $defaultplugin = variable_get('freelinking_default', 'nodetitle');
      $syntax = variable_get('freelinking_match_syntax', 'double_bracket');
      $regex = _freelinking_match_pattern();

      // Loop through every freelink format
      // Space at text start prevents match failure at start.
      preg_match_all($regex[$syntax], ' ' . $text, $matches, PREG_SET_ORDER);
      foreach ($matches as $match) {
        $current_plugin = '';

        // in markdown mode, the first match is part of the target.
        // This is hacky and temporary while matching is in transition.
        if ($syntax == 'markdown') {
          if (!$match[4]) {
            $match[4] = $match[3];
          }
          else {
            $match[4] = $match[3] . ':' . $match[4];
          }

          // encode pipes in Title.
          $match[2] = urlencode($match[2]);
          $match[1] = $match[4] . '|' . $match[2] . '|' . $match[5];
        }

        // default freelink (no colon)
        if (strpos($match[1], ':') === FALSE) {
          if ($syntax == 'markdown' && preg_match('!(\\.\\.?)?/!', $match[1])) {
            continue;

            // it's a regular markdown link
          }
          $current_plugin = $defaultplugin;
          $target = $match[1];
        }
        else {
          $delim = strpos($match[1], ':');
          $indicator = substr($match[1], 0, $delim);
          $target = substr($match[1], $delim + 1);

          // find a plugin for the match
          foreach (array_keys($freelinking) as $plugin) {
            if ($freelinking[$plugin]['enabled'] && preg_match($freelinking[$plugin]['indicator'], $indicator)) {
              $current_plugin = $plugin;
            }
          }

          // end looping through plugins
          if (!$current_plugin & $syntax == 'markdown') {
            continue;

            // it's a regular markdown link
          }
        }

        // end non-default freelinks
        $target = freelinking_parse_target($target, $current_plugin);
        $link = freelinking_get_freelink($current_plugin, $target);
        if ($link) {
          $text = str_replace($match[0], $link, $text);
        }
      }
      return $text;
    case 'prepare':
    default:
      return $text;
  }

  // endswitch $op
}