You are here

function _freelinking_do_filtering in Freelinking 6

Same name and namespace in other branches
  1. 5 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 457

Code

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

  // this finds [[links like this]], un-greedily and utf-8
  preg_match_all($freelinkingregexp, $text, $flmatches, PREG_PATTERN_ORDER);
  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[1]);
  }
  else {
    $wikiwords = $flmatches[1];
  }
  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('/^(http|mailto|ftp):/', $freelink)) {
        $replacement = '<a class="freelinking external" href="' . $freelink . '">' . $phrase . '</a>';
        $store = FALSE;
      }
      else {
        $replacement = l(html_entity_decode($phrase), 'freelinking/' . $freelink, array(
          'attributes' => 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), array(
          'attributes' => array(
            'class' => 'freelinking',
          ),
        ));
      }
    }
    $text = preg_replace($pattern, $replacement, $text, variable_get("freelinking_onceonly", 0) ? 1 : -1);
    if ($store) {
      _freelinking_store($freelink, $replacement);
    }
  }

  // foreach wikiword
  // strip out comment syntax (!)
  $text = preg_replace('/[!\\\\](\\[\\[.+]])/Uu', '$1', $text);
  return $text;
}