You are here

function _freelinking_process in Freelinking 7.3

Implements filter process callback

1 string reference to '_freelinking_process'
freelinking_filter_info in ./freelinking.module
Implements hook_filter_info().

File

./freelinking.module, line 74

Code

function _freelinking_process($text, $filter, $format, $langcode) {
  $freelinking = freelinking_get_plugins();
  $defaultplugin = variable_get('freelinking_default', 'nodetitle');
  $global_options = variable_get('freelinking_options', array());
  $start = 0;
  $remain = $text;
  $delim = '[[';
  $newtext = '';
  while (TRUE) {
    $offset = 0;
    if (empty($remain)) {
      break;
    }
    if ('[' == $remain[0] && '[' == $remain[1]) {
      $infreelinkp = TRUE;
      $delim = ']]';
    }
    else {
      $infreelinkp = FALSE;
      $delim = '[[';
    }
    $pos = strpos($remain, $delim);
    if (FALSE === $pos) {
      break;
    }
    $chunk_all = substr($remain, $start, $pos);
    if ($infreelinkp) {
      $current_plugin = '';
      $chunk_stripped = substr($chunk_all, 2);
      $delim = strpos($chunk_stripped, ':');
      if (FALSE === $delim) {
        $indicator = $defaultplugin;
        $target = $chunk_stripped;
      }
      else {
        $indicator = substr($chunk_stripped, 0, $delim);
        $target = substr($chunk_stripped, $delim + 1);
      }

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

        // end looping through plugins
      }
      if (empty($global_options['ignore_upi']) && $freelinking['nodetitle']['enabled'] && 'nodetitle' == $defaultplugin && empty($current_plugin)) {
        $target = $chunk_stripped;
        $current_plugin = 'nodetitle';
      }
      if (empty($global_options['ignore_upi']) || !empty($current_plugin)) {
        if (empty($current_plugin)) {
          $message = 'NONE' == $indicator ? t('Missing plugin indicator') : t('unknown plugin indicator "%indicator"', array(
            '%indicator' => $indicator,
          ));
          $link = theme('freelink_error', array(
            'plugin' => $indicator,
            'message' => $message,
          ));
        }
        else {
          $target_array = _freelinking_parse_target($target, $langcode);
          $link = freelinking_get_freelink($current_plugin, $target_array);
        }
        if ($link) {
          $chunk_all = $link;
          $offset = 2;
        }
      }
      $remain = substr($remain, $pos + $offset);
    }
    else {
      $remain = substr($remain, $pos);
    }
    $newtext .= $chunk_all;
  }
  $newtext .= $remain;
  return $newtext;
}