You are here

function _footnotes_replace_callback in Footnotes 5

Same name and namespace in other branches
  1. 5.2 footnotes.module \_footnotes_replace_callback()
  2. 6.2 footnotes.module \_footnotes_replace_callback()
  3. 6 footnotes.module \_footnotes_replace_callback()
  4. 7.3 footnotes.module \_footnotes_replace_callback()
  5. 7.2 footnotes.module \_footnotes_replace_callback()

Helper function called from preg_replace_callback() above

Uses static vars to temporarily store footnotes found. In my understanding, this is not threadsafe?!

1 call to _footnotes_replace_callback()
footnotes_filter in ./footnotes.module
Implementation of hook_filter().
1 string reference to '_footnotes_replace_callback'
footnotes_filter in ./footnotes.module
Implementation of hook_filter().

File

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

Code

function _footnotes_replace_callback($matches, $op = '') {
  static $n = 0;
  static $store_matches = array();
  $str = '';
  if ($op == 'output footer') {
    if ($n > 0) {
      $str = '<ol class="footnotes">';
      for ($m = 1; $m <= $n; $m++) {
        $text = $store_matches[$m - 1][0];
        $randstr = $store_matches[$m - 1][1];
        $str .= '<li><a class="footnote" name="footnote' . $m . '_' . $randstr . '" href="#footnoteref' . $m . '_' . $randstr . '">' . $m . '.</a> ';
        $str .= $text . "</li>\n";
      }
      $str .= "</ol>\n";
    }
    $n = 0;
    $store_matches = array();
    return $str;
  }

  //default op: act as called by preg_replace_callback()

  //Random string used to ensure footnote id's are unique, even

  //when contents of multiple nodes reside on same page. (fixes http://drupal.org/node/194558)
  $randstr = _footnotes_helper_randstr();
  array_push($store_matches, array(
    $matches[1],
    $randstr,
  ));
  $n++;
  $allowed_tags = array();
  $title = filter_xss($matches[1], $allowed_tags);

  //html attribute cannot contain quotes
  $title = str_replace('"', "&quot;", $title);

  //remove newlines. Browsers don't support them anyway and they'll confuse line break converter in filter.module
  $title = str_replace("\n", " ", $title);
  $title = str_replace("\r", "", $title);
  return '<a class="see_footnote" id="footnoteref' . $n . '_' . $randstr . '" title="' . $title . '" href="#footnote' . $n . '_' . $randstr . '">' . $n . '</a>';
}