You are here

function hashtags_filter in Hashtags 6

Implementation of hook_filter().

File

./hashtags.module, line 59

Code

function hashtags_filter($op, $delta = 0, $format = -1, $text = '') {
  switch ($op) {
    case 'list':
      return array(
        0 => t('Hashtags filter'),
      );
    case 'description':
      return t('Turn #words into links which lead to taxonomy terms');
    case 'settings':

      // nothing
      break;
    case 'no cache':
      return FALSE;
    case 'prepare':
      return $text;
    case 'process':
      $hashtags_string = hashtags_get_tags($text);
      if (empty($hashtags_string)) {
        return $text;
      }
      $hashtags_tids = hashtags_get_terms_by_names($hashtags_string);

      // create a class to pass parameters and have replace logic
      $replace_parameter = new hashtags_replace_parameter();
      $replace_parameter->hashtags_tids = $hashtags_tids;

      // 1) 2+ character after #
      // 2) Don't start with or use only numbers (0-9) (#123abc, #123 etc)
      // 3) Letters - digits work correct (#abc123, #conference2013)
      // 4) No Special Characters “!, $, %, ^, &, *, +, .”
      // 5) No Spaces
      // 6) May use an underscore. Hyphens and dashes will not work.
      // 7) <p>#hashtag</p> - is valid
      // 8) <a href="#hashtag">Link</p> - is not valid
      // Bug when hashtag resides at the begining of the string
      $pattern = "/([\\s>]+?)(#[[:alpha:]][[:alnum:]_]*[^<\\s[:punct:]])/iu";
      $text = preg_replace_callback($pattern, array(
        &$replace_parameter,
        'replace',
      ), $text);
      return $text;
    case 'default':
      return $text;
  }
}