You are here

protected function MarkdownExtra::appendFootnotes in Express 8

* Append footnote list to text *

Parameters

string $text: * @return string

File

vendor/michelf/php-markdown/Michelf/MarkdownExtra.php, line 1601

Class

MarkdownExtra
Markdown Extra Parser Class

Namespace

Michelf

Code

protected function appendFootnotes($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 = "";
    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}\"";
    }
    $backlink_text = $this->fn_backlink_html;
    $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}>{$backlink_text}</a>";
      for ($ref_num = 2; $ref_num <= $ref_count; ++$ref_num) {
        $backlink .= " <a href=\"#fnref{$ref_num}:{$note_id}\"{$attr}>{$backlink_text}</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;
}