You are here

function _footnotes_filter in Footnotes 7.2

Same name and namespace in other branches
  1. 7.3 footnotes.module \_footnotes_filter()

The bulk of filtering work is done here.

1 string reference to '_footnotes_filter'
footnotes_filter_info in ./footnotes.module
Implementation of hook_filter_info().

File

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

Code

function _footnotes_filter($text = '', $filter, $format) {

  // Supporting both [fn] and <fn> now. Thanks to fletchgqc http://drupal.org/node/268026
  // Convert all square brackets to angle brackets. This way all further code just
  // manipulates angle brackets. (Angle brackets are preferred here for the simple reason
  // that square brackets are more tedious to use in regexps.)
  $text = preg_replace('|\\[fn([^\\]]*)\\]|', '<fn$1>', $text);
  $text = preg_replace('|\\[/fn\\]|', '</fn>', $text);
  $text = preg_replace('|\\[footnotes([^\\]]*)\\]|', '<footnotes$1>', $text);

  // Check that there are an even number of open and closing tags.
  // If there is one closing tag missing, append this to the end.
  // If there is more disparity, throw a warning and continue.
  // A closing tag may sometimes be missing when we are processing a teaser
  // and it has been cut in the middle of the footnote.
  // See http://drupal.org/node/253326
  $foo = array();
  $open_tags = preg_match_all("|<fn([^>]*)>|", $text, $foo);
  $close_tags = preg_match_all("|</fn>|", $text, $foo);
  if ($open_tags == $close_tags + 1) {
    $text = $text . '</fn>';
  }
  elseif ($open_tags > $close_tags + 1) {
    trigger_error(t("You have unclosed fn tags. This is invalid and will produce unpredictable results."));
  }

  // Before doing the replacement, the callback function needs to know which options to use.
  _footnotes_replace_callback($filter->settings['footnotes_collapse'], 'prepare');
  $pattern = '|<fn([^>]*)>(.*?)</fn>|s';
  $text = preg_replace_callback($pattern, '_footnotes_replace_callback', $text);
  $text = preg_replace('/\\s+<a class="see-footnote"/', '<a class="see-footnote"', $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');
  $pattern = '|(<footnotes([^\\]]*)>)|';
  if (preg_match($pattern, $text) > 0) {
    $text = preg_replace($pattern, $footer, $text, 1);
    return $text;
  }
  else {
    return $text . "\n\n" . $footer;
  }
}