You are here

function _freelinking_do_filtering in Freelinking 5

Same name and namespace in other branches
  1. 6 freelinking.module \_freelinking_do_filtering()
  2. 6.2 freelinking.module \_freelinking_do_filtering()
2 calls to _freelinking_do_filtering()
freelinking_filter in ./freelinking.module
freelinking_nodeapi in ./freelinking.module

File

./freelinking.module, line 387

Code

function _freelinking_do_filtering($text, $store = FALSE) {
  $allowcamelcase = variable_get("freelinking_camelcase", TRUE);
  $freelinkingregexp = '/\\[\\[.+]]/U';

  // this finds [[links like this]], un-greedily
  preg_match_all($freelinkingregexp, $text, $flmatches);
  if ($allowcamelcase) {
    $camelcaseregexp = '/\\b([[:upper:]][[:lower:]]+){2,}\\b/';

    // this gets us close, but is not perfect. Example: ThisIsACamelCaseWord won't match (two caps in a row)
    preg_match_all($camelcaseregexp, $text, $ccmatches);
    $wikiwords = array_merge($ccmatches[0], $flmatches[0]);
  }
  else {
    $wikiwords = $flmatches[0];
  }
  foreach (array_unique($wikiwords) as $wikiword) {
    if (substr($wikiword, 0, 2) == '[[') {

      // if it's a freelink, the expressions are different
      $phrase = substr($wikiword, 2, -2);
      $freelink = $phrase;
      $barpos = strpos($phrase, '|');
      $pattern = '/\\[\\[' . preg_quote($phrase, '/') . ']]/';
      if ($barpos) {
        $freelink = substr($freelink, 0, $barpos);
        $phrase = substr($phrase, $barpos + 1);
      }
      if (preg_match('/^(https?|mailto|ftp):/', $freelink)) {
        $replacement = '<a class="freelinking external" href="' . $freelink . '">' . $phrase . '</a>';
        $store = FALSE;
      }
      else {
        $replacement = l($phrase, 'freelinking/' . rawurlencode($freelink), array(
          'class' => 'freelinking',
        ));
      }
    }
    else {
      if ($allowcamelcase) {

        // it's a CamelCase, expressions are a bit simpler
        $pattern = '/\\b' . $wikiword . '\\b(?![^<]*>)/';
        $phrase = $wikiword;

        // consistency for the db
        $freelink = $wikiword;

        // also for the db
        $replacement = l($wikiword, 'freelinking/' . urlencode($wikiword));
      }
    }
    $text = preg_replace($pattern, $replacement, $text, variable_get("freelinking_onceonly", 0) ? 1 : -1);
    if (variable_get('freelinking_onceonly', 0)) {
      $text = preg_replace($pattern, $phrase, $text);
    }
    if ($store) {
      _freelinking_store($freelink, $replacement);
    }
  }

  // foreach wikiword
  return $text;
}