/
Footnotes.php
178 lines (165 loc) · 5.37 KB
/
Footnotes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* Footnotes Widget Class file
*
* @author Chris Yates
* @copyright Copyright © 2015 BeastBytes - All Rights Reserved
* @license BSD 3-Clause
* @package EmailObfuscator
*/
namespace beastbytes\footnotes;
use Yii;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/**
* Footnotes Widget Class.
* Generates accessible, automatically numbered footnotes.
* Inspired by {@link http://www.sitepoint.com/accessible-footnotes-css/ Accessible Footnotes with CSS}
*
* Usage:
* $footnotes = Footnotes::begin(); // Before any footnote references on the page
* echo $footnotes->add('Local text', 'Footnote text'); // at every footnote reference
* Footnotes::end(); // where the footnotes are to be rendered
*
* Example:
* ```php
$footnotes = Footnotes::begin();
// Can be other view stuff here
echo Html::tag('p', strtr('This example shows how to use the {Footnotes widget}.', [
'{Footnotes widget}' => $footnotes->add('Footnotes widget', 'The Footnotes widget makes it easy to add accessible footnotes.'),
]));
// More view stuff
echo Html::tag('p', strtr('The Footnotes widget uses the {Yii2 framework}.', [
'{Yii2 framework}' => $footnotes->add('Yii 2 framework', 'The Yii2 framework is the best framework for developing web applications.'),
]));
// More view stuff, maybe with some more footnote references
Footnotes::end(); // renders the footnotes
```
*/
class Footnotes extends Widget
{
/**
* @var array options for the footnotes container element
* In addition to HTML attributes the following options are recognised:
* - encodeFootnote - boolean - whether to encode footnotes; defaults to TRUE.
* Can be overridden for an individual footnote in $options parameter of the
* add() method
* - encodeReference - boolean - whether to encode footnote references;
* defaults to TRUE.
* Can be overridden for an individual footnote in $options parameter of the
* add() method
* - tag - string - the tag for the footnotes section; defaults to section
*/
public $options = ['class' => 'footnotes'];
/**
* @var string Prefix for foonotes
*/
public $prefix = 'footnote';
/**
* @var array HTML option for footnote references. These can be overridden
* for individual references by setting $options in the add() method
*/
public $referenceOptions = [];
/**
* @var boolean whether to register widget assets.
* Set FALSE if you are going to provide your own CSS.
*/
public $registerAssets = true;
/**
* @var string ARIA label for the footnote return link.
*/
public $returnAriaLabel = 'Return to content';
/**
* @var string text for the footnote return link.
*/
public $returnText = '↩';
/**
* @var string Title for the foonotes section
*/
public $title = 'Footnotes';
/**
* @var array the footnotes
*/
private $_footnotes = [];
/**
* @var integer counter for auto-generating footnote reference ids
*/
private $_counter = 0;
/**
* Initializes the widget
*/
public function init()
{
if (isset($this->options['id'])) {
$this->id = $this->options['id'];
} else {
$this->options['id'] = $this->id;
};
if ($this->registerAssets) {
FootnotesAsset::register($this->getView());
}
parent::init();
}
/**
* Adds a footnote reference and saves the footnote for rendering later
*
* @param string text for the footnote reference
* @param array HTML aoptions for the footnote reference
* @return string HTML for the footnote reference
*/
public function add($reference, $footnote, $options = [])
{
if (isset($options['id'])) {
$id = $options['id'];
} else {
$id = $this->id.'-'.$this->_counter++;
$options['id'] = $id;
}
$this->_footnotes[$id] = (ArrayHelper::remove($options, 'encodeFootnote', true)
? Html::encode($footnote)
: $footnote
);
$options = ArrayHelper::merge(
$this->referenceOptions,
$options,
[
'aria-describedby' => $this->prefix.'-label'
]
);
return Html::a(
(ArrayHelper::remove($options, 'encodeReference', true)
? Html::encode($reference)
: $reference
),
'#'.$this->prefix.'-'.$id,
$options
);
}
/**
* Renders the footnotes.
*/
public function run()
{
$tag = ArrayHelper::remove($options, 'tag', 'section');
echo Html::beginTag($tag, $this->options);
echo Html::tag('h1', $this->title, ['id' => $this->prefix.'-label']);
echo Html::ol($this->_footnotes, [
'item' => function ($item, $index)
{
return Html::tag(
'li', $item.Html::a(
$this->returnText,
'#'.$index,
[
'aria-label' => $this->returnAriaLabel,
'class' => 'back-to-content'
]
),
['id' => $this->prefix.'-'.$index]
);
}
]);
echo Html::endTag($tag);
}
}