You are here

function biblio_filter in Bibliography Module 5

Same name and namespace in other branches
  1. 6.2 biblio.module \biblio_filter()
  2. 6 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 3513

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('References <bib>'),
    );
  }
  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 <bib>...</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':
          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('|<bib>(.*?)</bib>|s', '_biblio_filter_footnote_callback', $text);
            return $text;
          }
          else {
            $text = preg_replace_callback('|<bib>(.*?)</bib>|s', '_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;
  }
}