|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the trefoil application. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Miguel Angel Gabriel <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace Trefoil\Plugins\Optional; |
|
11
|
|
|
|
|
12
|
|
|
use Easybook\Events\BaseEvent; |
|
13
|
|
|
use Easybook\Events\EasybookEvents; |
|
14
|
|
|
use Easybook\Events\ParseEvent; |
|
15
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
16
|
|
|
use Trefoil\Util\SimpleReport; |
|
17
|
|
|
use Trefoil\Plugins\BasePlugin; |
|
18
|
|
|
/** |
|
19
|
|
|
* This plugin processes footnotes. |
|
20
|
|
|
* |
|
21
|
|
|
* It first collects all footnotes markup generated by the Markdown parser |
|
22
|
|
|
* (at the end of each book element) and then it generates a consolidated |
|
23
|
|
|
* footnotes list element with all the book's footnotes. |
|
24
|
|
|
* |
|
25
|
|
|
* It also replaces invalid character ':' in footnotes ids generated by the |
|
26
|
|
|
* Markdown parser (so epubcheck will not complain). |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
class FootnotesExtraPlugin extends BasePlugin implements EventSubscriberInterface |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The footnotes |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $footnotes = array(); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Whether or not the footnotes item has been generated |
|
41
|
|
|
* |
|
42
|
|
|
* @var bool |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $generated = false; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param array $footnotes |
|
48
|
|
|
* |
|
49
|
|
|
* @internal Should be protected but made public for PHP 5.3 compat |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function setFootnotes($footnotes) |
|
52
|
|
|
{ |
|
53
|
1 |
|
$this->footnotes = $footnotes; |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return array |
|
58
|
|
|
* |
|
59
|
|
|
* @internal Should be protected but made public for PHP 5.3 compat |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function getFootnotes() |
|
62
|
|
|
{ |
|
63
|
1 |
|
return $this->footnotes; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/* ******************************************************************************** |
|
67
|
|
|
* Event handlers |
|
68
|
|
|
* ******************************************************************************** |
|
69
|
|
|
*/ |
|
70
|
2 |
View Code Duplication |
public static function getSubscribedEvents() |
|
71
|
|
|
{ |
|
72
|
|
|
return array( |
|
73
|
2 |
|
EasybookEvents::PRE_PARSE => array('onItemPreParse', +100), |
|
74
|
2 |
|
EasybookEvents::POST_PARSE => array('onItemPostParse'), |
|
75
|
2 |
|
EasybookEvents::POST_PUBLISH => 'onPostPublish'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
public function onItemPostParse(ParseEvent $event) |
|
79
|
|
|
{ |
|
80
|
2 |
|
$this->init($event); |
|
81
|
|
|
|
|
82
|
2 |
|
$this->processItem(); |
|
83
|
|
|
|
|
84
|
|
|
// reload changed item |
|
85
|
2 |
|
$event->setItem($this->item); |
|
86
|
2 |
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
public function onItemPreParse(ParseEvent $event) |
|
89
|
|
|
{ |
|
90
|
2 |
|
$this->init($event); |
|
91
|
|
|
|
|
92
|
2 |
|
if ('footnotes' == $this->item['config']['element']) { |
|
93
|
2 |
|
$this->saveFootnotes(); |
|
94
|
2 |
|
} |
|
95
|
2 |
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
public function onPostPublish(BaseEvent $event) |
|
98
|
|
|
{ |
|
99
|
2 |
|
$this->init($event); |
|
100
|
|
|
|
|
101
|
|
|
// create the processing report |
|
102
|
2 |
|
$this->createReport(); |
|
103
|
2 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/* ******************************************************************************** |
|
106
|
|
|
* Implementation |
|
107
|
|
|
* ******************************************************************************** |
|
108
|
|
|
*/ |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* For a content item to be processed, extract generated footnotes. |
|
112
|
|
|
*/ |
|
113
|
2 |
|
protected function processItem() |
|
114
|
|
|
{ |
|
115
|
2 |
|
if ($this->item['config']['element'] !== 'footnotes') { |
|
116
|
2 |
|
$this->extractFootnotes(); |
|
117
|
2 |
|
$this->renumberReferences(); |
|
118
|
2 |
|
} else { |
|
119
|
2 |
|
if (!$this->footnotes) { |
|
120
|
|
|
// avoid empty footnotes item appearing in the book |
|
121
|
1 |
|
$this->item['content'] = ''; |
|
122
|
1 |
|
$this->item['toc'] = array(); |
|
123
|
|
|
// instruct the publisher to remove this item |
|
124
|
1 |
|
$this->item['remove'] = true; |
|
125
|
1 |
|
$this->writeLn("No footnotes found in text.",'info'); |
|
126
|
1 |
|
} |
|
127
|
|
|
} |
|
128
|
2 |
|
} |
|
129
|
|
|
|
|
130
|
2 |
|
protected function extractFootnotes() |
|
131
|
|
|
{ |
|
132
|
2 |
|
$content = $this->item['content']; |
|
133
|
|
|
|
|
134
|
2 |
|
$regExp = '/'; |
|
135
|
2 |
|
$regExp .= '<div class="footnotes">.*<ol>(?<fns>.*)<\/ol>.*<\/div>'; |
|
136
|
2 |
|
$regExp .= '/Ums'; // Ungreedy, multiline, dotall |
|
137
|
|
|
|
|
138
|
|
|
// PHP 5.3 compat |
|
139
|
2 |
|
$me = $this; |
|
140
|
|
|
|
|
141
|
2 |
|
$content = preg_replace_callback( |
|
142
|
2 |
|
$regExp, |
|
143
|
|
|
function ($matches) use ($me) { |
|
144
|
|
|
|
|
145
|
1 |
|
$regExp2 = '/'; |
|
146
|
1 |
|
$regExp2 .= '<li.*id="(?<id>.*)">.*'; |
|
147
|
1 |
|
$regExp2 .= '<p>(?<text>.*) <a .*href="#(?<backref>.*)"'; |
|
148
|
1 |
|
$regExp2 .= '/Ums'; // Ungreedy, multiline, dotall |
|
149
|
|
|
|
|
150
|
1 |
|
preg_match_all($regExp2, $matches[0], $matches2, PREG_SET_ORDER); |
|
151
|
|
|
|
|
152
|
1 |
|
if ($matches2) { |
|
153
|
1 |
|
foreach ($matches2 as $match2) { |
|
154
|
1 |
|
$footnotes = $me->getFootnotes(); |
|
155
|
1 |
|
$footnotes[$match2['id']] = array( |
|
156
|
1 |
|
'id' => str_replace(':', '-', $match2['id']), |
|
157
|
1 |
|
'text' => $match2['text'], |
|
158
|
1 |
|
'backref' => str_replace(':', '-', $match2['backref']), |
|
159
|
1 |
|
'new_number' => count($footnotes) + 1 |
|
160
|
1 |
|
); |
|
161
|
1 |
|
$me->setFootnotes($footnotes); |
|
162
|
1 |
|
} |
|
163
|
1 |
|
} |
|
164
|
|
|
|
|
165
|
1 |
|
return ''; |
|
166
|
2 |
|
}, |
|
167
|
|
|
$content |
|
168
|
2 |
|
); |
|
169
|
|
|
|
|
170
|
2 |
|
$this->item['content'] = $content; |
|
171
|
2 |
|
} |
|
172
|
|
|
|
|
173
|
2 |
|
protected function renumberReferences() |
|
174
|
|
|
{ |
|
175
|
2 |
|
$content = $this->item['content']; |
|
176
|
|
|
|
|
177
|
2 |
|
$regExp = '/'; |
|
178
|
2 |
|
$regExp .= '<sup id="(?<supid>fnref:.*)">'; |
|
179
|
2 |
|
$regExp .= '<a(?<prev>.*)href="#(?<href>fn:.*)"(?<post>.*)>(?<number>.*)<\/a>'; |
|
180
|
2 |
|
$regExp .= '/Ums'; // Ungreedy, multiline, dotall |
|
181
|
|
|
|
|
182
|
|
|
// PHP 5.3 compat |
|
183
|
2 |
|
$me = $this; |
|
184
|
|
|
|
|
185
|
2 |
|
$content = preg_replace_callback( |
|
186
|
2 |
|
$regExp, |
|
187
|
2 |
|
function ($matches) use ($me) { |
|
188
|
1 |
|
$footnotes = $me->getFootnotes(); |
|
189
|
1 |
|
$newNumber = $footnotes[$matches['href']]['new_number']; |
|
190
|
|
|
|
|
191
|
1 |
|
$html = sprintf( |
|
192
|
1 |
|
'<sup id="%s"><a%shref="#%s"%s>%s</a>', |
|
193
|
1 |
|
str_replace(':', '-', $matches['supid']), |
|
194
|
1 |
|
$matches['prev'], |
|
195
|
1 |
|
str_replace(':', '-', $matches['href']), |
|
196
|
1 |
|
$matches['post'], |
|
197
|
|
|
$newNumber |
|
198
|
1 |
|
); |
|
199
|
|
|
|
|
200
|
1 |
|
return $html; |
|
201
|
2 |
|
}, |
|
202
|
|
|
$content |
|
203
|
2 |
|
); |
|
204
|
|
|
|
|
205
|
2 |
|
$this->item['content'] = $content; |
|
206
|
2 |
|
} |
|
207
|
|
|
|
|
208
|
2 |
|
protected function saveFootnotes() |
|
209
|
|
|
{ |
|
210
|
2 |
|
$this->app['publishing.footnotes.items'] = $this->footnotes; |
|
211
|
|
|
|
|
212
|
2 |
|
$this->generated = true; |
|
213
|
2 |
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Writes the report with the summary of processing done. |
|
217
|
|
|
*/ |
|
218
|
2 |
|
protected function createReport() |
|
219
|
|
|
{ |
|
220
|
2 |
|
$report = new SimpleReport(); |
|
221
|
2 |
|
$report->setTitle('FootnotesExtraPlugin'); |
|
222
|
|
|
|
|
223
|
2 |
|
$report->setHeaders(array('Old reference', 'New reference', 'Text (fragment)')); |
|
224
|
|
|
|
|
225
|
2 |
|
$report->setColumnsWidth(array(13, 13, 93)); |
|
226
|
2 |
|
$report->setColumnsAlignment(array('center', 'center', 'left')); |
|
227
|
|
|
|
|
228
|
2 |
|
foreach ($this->footnotes as $footnote) { |
|
229
|
1 |
|
$parts = explode('-', $footnote['id']); |
|
230
|
1 |
|
$oldRef = $parts[1]; |
|
231
|
1 |
|
$newRef = $footnote['new_number']; |
|
232
|
1 |
|
$text = substr($footnote['text'], 0, 90); |
|
233
|
1 |
|
$text .= ($text == $footnote['text'] ? '' : '...'); |
|
234
|
1 |
|
$report->addLine(array($oldRef, $newRef, $text)); |
|
235
|
2 |
|
} |
|
236
|
|
|
|
|
237
|
2 |
|
if (!$this->generated) { |
|
238
|
|
|
$this->writeLn( |
|
239
|
|
|
"No footnotes element has been generated, check for missing 'footnotes' contents element.", |
|
240
|
|
|
'error' |
|
241
|
|
|
); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
2 |
|
$outputDir = $this->app['publishing.dir.output']; |
|
245
|
2 |
|
$reportFile = $outputDir . '/report-FootnotesExtraPlugin.txt'; |
|
246
|
|
|
|
|
247
|
2 |
|
file_put_contents($reportFile, $report->getText()); |
|
248
|
2 |
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|