You are here

function MarkdownExtra_Parser::appendFootnotes in Markdown 5

Same name and namespace in other branches
  1. 6 markdown.php \MarkdownExtra_Parser::appendFootnotes()

File

./markdown.php, line 2653

Class

MarkdownExtra_Parser

Code

function appendFootnotes($text) {

  #

  # Append footnote list to text.

  #
  $text = preg_replace_callback('{F\\x1Afn:(.*?)\\x1A:}', array(
    &$this,
    '_appendFootnotes_callback',
  ), $text);
  if (!empty($this->footnotes_ordered)) {
    $text .= "\n\n";
    $text .= "<div class=\"footnotes\">\n";
    $text .= "<hr" . MARKDOWN_EMPTY_ELEMENT_SUFFIX . "\n";
    $text .= "<ol>\n\n";
    $attr = " rev=\"footnote\"";
    if ($this->fn_backlink_class != "") {
      $class = $this->fn_backlink_class;
      $class = $this
        ->encodeAttribute($class);
      $attr .= " class=\"{$class}\"";
    }
    if ($this->fn_backlink_title != "") {
      $title = $this->fn_backlink_title;
      $title = $this
        ->encodeAttribute($title);
      $attr .= " title=\"{$title}\"";
    }
    $num = 0;
    while (!empty($this->footnotes_ordered)) {
      $footnote = reset($this->footnotes_ordered);
      $note_id = key($this->footnotes_ordered);
      unset($this->footnotes_ordered[$note_id]);
      $footnote .= "\n";

      # Need to append newline before parsing.
      $footnote = $this
        ->runBlockGamut("{$footnote}\n");
      $footnote = preg_replace_callback('{F\\x1Afn:(.*?)\\x1A:}', array(
        &$this,
        '_appendFootnotes_callback',
      ), $footnote);
      $attr = str_replace("%%", ++$num, $attr);
      $note_id = $this
        ->encodeAttribute($note_id);

      # Add backlink to last paragraph; create new paragraph if needed.
      $backlink = "<a href=\"#fnref:{$note_id}\"{$attr}>&#8617;</a>";
      if (preg_match('{</p>$}', $footnote)) {
        $footnote = substr($footnote, 0, -4) . "&#160;{$backlink}</p>";
      }
      else {
        $footnote .= "\n\n<p>{$backlink}</p>";
      }
      $text .= "<li id=\"fn:{$note_id}\">\n";
      $text .= $footnote . "\n";
      $text .= "</li>\n\n";
    }
    $text .= "</ol>\n";
    $text .= "</div>";
  }
  return $text;
}