implement placeholder parsing for footnote backlink title, aria-label, html
also, no (empty) backlink is generated when the $fn_backlink_html is blank
This commit is contained in:
parent
24a2fcc546
commit
de4d677a4c
1 changed files with 55 additions and 33 deletions
|
|
@ -25,11 +25,10 @@ class MarkdownExtra extends \Michelf\Markdown {
|
|||
public $fn_id_prefix = "";
|
||||
|
||||
/**
|
||||
* Optional title attribute for footnote links and backlinks.
|
||||
* Optional title attribute for footnote links.
|
||||
* @var string
|
||||
*/
|
||||
public $fn_link_title = "";
|
||||
public $fn_backlink_title = "";
|
||||
public $fn_link_title = "";
|
||||
|
||||
/**
|
||||
* Optional class attribute for footnote links and backlinks.
|
||||
|
|
@ -42,15 +41,20 @@ class MarkdownExtra extends \Michelf\Markdown {
|
|||
* Content to be displayed within footnote backlinks. The default is '↩';
|
||||
* the U+FE0E on the end is a Unicode variant selector used to prevent iOS
|
||||
* from displaying the arrow character as an emoji.
|
||||
* Optionally use '^^' and '%%' to refer to the footnote number and
|
||||
* reference number respectively. {@see parseFootnotePlaceholders()}
|
||||
* @var string
|
||||
*/
|
||||
public $fn_backlink_html = '↩︎';
|
||||
|
||||
/**
|
||||
* Optional aria-label attribute for footnote backlinks. Use '{ref}' and
|
||||
* '{fn}' to refer to the reference number and footnote number respectively.
|
||||
* Optional title and aria-label attributes for footnote backlinks for
|
||||
* added accessibility (to ensure backlink uniqueness).
|
||||
* Use '^^' and '%%' to refer to the footnote number and reference number
|
||||
* respectively. {@see parseFootnotePlaceholders()}
|
||||
* @var string
|
||||
*/
|
||||
public $fn_backlink_title = "";
|
||||
public $fn_backlink_label = "";
|
||||
|
||||
/**
|
||||
|
|
@ -1678,19 +1682,13 @@ class MarkdownExtra extends \Michelf\Markdown {
|
|||
* @return void
|
||||
*/
|
||||
protected function _doFootnotes() {
|
||||
$attr = "";
|
||||
$attr = array();
|
||||
if ($this->fn_backlink_class !== "") {
|
||||
$class = $this->fn_backlink_class;
|
||||
$class = $this->encodeAttribute($class);
|
||||
$attr .= " class=\"$class\"";
|
||||
$attr['class'] = " class=\"$class\"";
|
||||
}
|
||||
if ($this->fn_backlink_title !== "") {
|
||||
$title = $this->fn_backlink_title;
|
||||
$title = $this->encodeAttribute($title);
|
||||
$attr .= " title=\"$title\"";
|
||||
}
|
||||
$attr .= " role=\"doc-backlink\"";
|
||||
$backlink_text = $this->fn_backlink_html;
|
||||
$attr['role'] = " role=\"doc-backlink\"";
|
||||
$num = 0;
|
||||
|
||||
$text = "<ol>\n\n";
|
||||
|
|
@ -1707,25 +1705,43 @@ class MarkdownExtra extends \Michelf\Markdown {
|
|||
$footnote = preg_replace_callback('{F\x1Afn:(.*?)\x1A:}',
|
||||
array($this, '_appendFootnotes_callback'), $footnote);
|
||||
|
||||
$attr = str_replace("%%", ++$num, $attr);
|
||||
$num++;
|
||||
$note_id = $this->encodeAttribute($note_id);
|
||||
|
||||
// Prepare backlink, multiple backlinks if multiple references
|
||||
$label = !empty($this->fn_backlink_label)
|
||||
? ' aria-label="'.$this->buildFootnoteLabel($num, 1).'"'
|
||||
: '';
|
||||
$backlink = "<a href=\"#fnref:$note_id\"{$attr}{$label}>$backlink_text</a>";
|
||||
for ($ref_num = 2; $ref_num <= $ref_count; ++$ref_num) {
|
||||
if (!empty($this->fn_backlink_label)) {
|
||||
$label = ' aria-label="'.$this->buildFootnoteLabel($num, $ref_num).'"';
|
||||
// Do not create empty backlinks if the html is blank
|
||||
$backlink = "";
|
||||
if (!empty($this->fn_backlink_html)) {
|
||||
for ($ref_num = 1; $ref_num <= $ref_count; ++$ref_num) {
|
||||
if (!empty($this->fn_backlink_title)) {
|
||||
$attr['title'] = ' title="' . $this->encodeAttribute($this->fn_backlink_title) . '"';
|
||||
}
|
||||
if (!empty($this->fn_backlink_label)) {
|
||||
$attr['label'] = ' aria-label="' . $this->encodeAttribute($this->fn_backlink_label) . '"';
|
||||
}
|
||||
$parsed_attr = $this->parseFootnotePlaceholders(
|
||||
implode('', $attr),
|
||||
$num,
|
||||
$ref_num
|
||||
);
|
||||
$backlink_text = $this->parseFootnotePlaceholders(
|
||||
$this->fn_backlink_html,
|
||||
$num,
|
||||
$ref_num
|
||||
);
|
||||
$ref_count_mark = $ref_num > 1 ? $ref_num : '';
|
||||
$backlink .= " <a href=\"#fnref$ref_count_mark:$note_id\"$parsed_attr>$backlink_text</a>";
|
||||
}
|
||||
$backlink .= " <a href=\"#fnref$ref_num:$note_id\"{$attr}{$label}>$backlink_text</a>";
|
||||
$backlink = trim($backlink);
|
||||
}
|
||||
|
||||
// Add backlink to last paragraph; create new paragraph if needed.
|
||||
if (preg_match('{</p>$}', $footnote)) {
|
||||
$footnote = substr($footnote, 0, -4) . " $backlink</p>";
|
||||
} else {
|
||||
$footnote .= "\n\n<p>$backlink</p>";
|
||||
if (!empty($backlink)) {
|
||||
if (preg_match('{</p>$}', $footnote)) {
|
||||
$footnote = substr($footnote, 0, -4) . " $backlink</p>";
|
||||
} else {
|
||||
$footnote .= "\n\n<p>$backlink</p>";
|
||||
}
|
||||
}
|
||||
|
||||
$text .= "<li id=\"fn:$note_id\" role=\"doc-endnote\">\n";
|
||||
|
|
@ -1786,14 +1802,20 @@ class MarkdownExtra extends \Michelf\Markdown {
|
|||
}
|
||||
|
||||
/**
|
||||
* Build an encoded footnote label from {@see $fn_footnote_label} by
|
||||
* evaluating any '{fn}' and '{ref}' placeholders.
|
||||
* @param int $footnote_number
|
||||
* @param int $reference_number
|
||||
* Build footnote label by evaluating any placeholders.
|
||||
* - ^^ footnote number
|
||||
* - %% footnote reference number (Nth reference to footnote number)
|
||||
* @param string $label
|
||||
* @param int $footnote_number
|
||||
* @param int $reference_number
|
||||
* @return string
|
||||
*/
|
||||
protected function buildFootnoteLabel($footnote_number, $reference_number) {
|
||||
return $this->encodeAttribute(str_replace(array('{fn}', '{ref}'), array($footnote_number, $reference_number), $this->fn_backlink_label));
|
||||
protected function parseFootnotePlaceholders($label, $footnote_number, $reference_number) {
|
||||
return str_replace(
|
||||
array('^^', '%%'),
|
||||
array($footnote_number, $reference_number),
|
||||
$label
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue