You are here

function MarkdownExtra_Parser::appendFootnotes in Markdown 6

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

File

./markdown.php, line 3079

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" . $this->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]);
      $ref_count = $this->footnotes_ref_count[$note_id];
      unset($this->footnotes_ref_count[$note_id]);
      unset($this->footnotes[$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);

      # Prepare backlink, multiple backlinks if multiple references
      $backlink = "<a href=\"#fnref:{$note_id}\"{$attr}>&#8617;</a>";
      for ($ref_num = 2; $ref_num <= $ref_count; ++$ref_num) {
        $backlink .= " <a href=\"#fnref{$ref_num}:{$note_id}\"{$attr}>&#8617;</a>";
      }

      # Add backlink to last paragraph; create new paragraph if needed.
      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;
}