You are here

function biblio_filter in Bibliography Module 6

Same name and namespace in other branches
  1. 5 biblio.module \biblio_filter()
  2. 6.2 biblio.module \biblio_filter()

Implementation of hook_filter().

The bulk of filtering work is done here. This hook is quite complicated, so we'll discuss each operation it defines.

File

./biblio.module, line 1957

Code

function biblio_filter($op, $delta = 0, $format = -1, $text = '') {

  // The "list" operation provides the module an opportunity to declare both how
  // many filters it defines and a human-readable name for each filter. Note that
  // the returned name should be passed through t() for translation.
  if ($op == 'list') {
    return array(
      0 => t('Biblio module references &lt;bib&gt; <i>or</i> [bib]'),
      1 => t('Biblio module inline references &lt;ibib&gt; <i>or</i> [ibib]'),
    );
  }
  if ($op == 'no cache') {
    return TRUE;
  }

  // All operations besides "list" provide a $delta argument so we know which
  // filter they refer to. We'll switch on that argument now so that we can
  // discuss each filter in turn.
  switch ($delta) {

    // First is the html footnotes filter
    case 0:
      switch ($op) {

        // This description is shown in the administrative interface, unlike the
        // filter tips which are shown in the content editing interface.
        case 'description':
          return t('Use &lt;bib&gt;citekey&lt;/bib&gt; or [bib]citebkey[/bib]to insert automatically numbered references.');

        // We don't need the "prepare" operation for this filter, but it's required
        // to at least return the input text as-is.

        //TODO: May need to escape <fn> if we use HTML filter too, but Footnotes could be first
        case 'prepare':
          return $text;

        // The actual filtering is performed here. The supplied text should be
        // returned, once any necessary substitutions have taken place.
        case 'process':
          $pattern = array(
            '|\\[bib](.*?)\\[/bib]|s',
            '|<bib>(.*?)</bib>|s',
          );
          if (variable_get('biblio_footnotes_integration', 0) && module_exists('footnotes')) {

            // this is used with footnote module integration to replace the <bib> tags with <fn> tags
            $text = preg_replace_callback($pattern, '_biblio_filter_footnote_callback', $text);
            return $text;
          }
          else {
            $text = preg_replace_callback($pattern, '_biblio_filter_replace_callback', $text);

            //Replace tag <footnotes> with the list of footnotes.

            //If tag is not present, by default add the footnotes at the end.

            //Thanks to acp on drupal.org for this idea. see http://drupal.org/node/87226
            $footer = '';
            $footer = _biblio_filter_replace_callback(NULL, 'output footer');
            if (preg_match('/<bibliography(\\/( )?)?>/', $text) > 0) {
              $text = preg_replace('/<bibliography(\\/( )?)?>/', $footer, $text, 1);
              return $text;
            }
            else {
              return $text . "\n\n" . $footer;
            }
          }
      }
      break;
    case 1:
      switch ($op) {

        // This description is shown in the administrative interface, unlike the
        // filter tips which are shown in the content editing interface.
        case 'description':
          return t('Use &lt;ibib&gt;citekey&lt;/ibib&gt; or [ibib]citebkey[/ibib]to insert inline references.');

        // We don't need the "prepare" operation for this filter, but it's required
        // to at least return the input text as-is.

        //TODO: May need to escape <fn> if we use HTML filter too, but Footnotes could be first
        case 'prepare':
          return $text;

        // The actual filtering is performed here. The supplied text should be
        // returned, once any necessary substitutions have taken place.
        case 'process':
          $pattern = array(
            '|\\[ibib](.*?)\\[/ibib]|s',
            '|<ibib>(.*?)</ibib>|s',
          );
          $text = preg_replace_callback($pattern, '_biblio_inline_filter_replace_callback', $text);
          return $text;
      }
      break;
  }
}