You are here

function footnotes_filter in Footnotes 6

Same name and namespace in other branches
  1. 5.2 footnotes.module \footnotes_filter()
  2. 5 footnotes.module \footnotes_filter()
  3. 6.2 footnotes.module \footnotes_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

./footnotes.module, line 67
The Footnotes module is a filter that can be used to insert automatically numbered footnotes into Drupal texts.

Code

function footnotes_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('Footnotes <fn>'),
      1 => t('Footnotes Textile style'),
    );
  }

  // 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 <fn>...</fn> to insert automatically numbered footnotes.');

        // 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':
          $text = preg_replace_callback('|<fn>(.*?)</fn>|s', '_footnotes_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 = _footnotes_replace_callback(NULL, 'output footer');
          if (preg_match('/<footnotes(\\/( )?)?>/', $text) > 0) {
            $text = preg_replace('/<footnotes(\\/( )?)?>/', $footer, $text, 1);
            return $text;
          }
          else {
            return $text . "\n\n" . $footer;
          }
      }
      break;

    // Textile version.
    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 [# ...] to insert automatically numbered footnotes in Textile markup.');

        // We don't need the "prepare" operation for this filter, but it's required
        // to at least return the input text as-is.
        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':
          $text = preg_replace_callback('|\\[# (.*?)\\]|s', '_footnotes_replace_callback_textile', $text);

          //Replace Textile 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 = _footnotes_replace_callback_textile(NULL, 'output footer');
          if (preg_match('/\\n *footnotes\\. *(\\n|$)/', $text) > 0) {
            $text = preg_replace('/\\n *footnotes\\. *(\\n|$)/', "\n{$footer}\n", $text, 1);
            return $text;
          }
          else {
            return $text . "\n\n" . $footer;
          }
      }
      break;
  }
}