|
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\Plugins\BasePlugin; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* This plugin fixes footnotes. |
|
20
|
|
|
* |
|
21
|
|
|
* @deprecated |
|
22
|
|
|
* |
|
23
|
|
|
* It replaces invalid character ':' in footnotes ids generated by the |
|
24
|
|
|
* Markdown parser (so epubcheck will not complain). |
|
25
|
|
|
* |
|
26
|
|
|
*/ |
|
27
|
|
|
class FootnotesFixPlugin extends BasePlugin implements EventSubscriberInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/* ******************************************************************************** |
|
30
|
|
|
* Event handlers |
|
31
|
|
|
* ******************************************************************************** |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function getSubscribedEvents() |
|
34
|
|
|
{ |
|
35
|
|
|
return array( |
|
36
|
|
|
EasybookEvents::POST_PARSE => array('onItemPostParse'), |
|
37
|
|
|
EasybookEvents::POST_PUBLISH => 'onPostPublish' |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function onItemPostParse(ParseEvent $event) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->init($event); |
|
44
|
|
|
|
|
45
|
|
|
$this->processItem(); |
|
46
|
|
|
|
|
47
|
|
|
// reload changed item |
|
48
|
|
|
$event->setItem($this->item); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function onPostPublish(BaseEvent $event) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->init($event); |
|
54
|
|
|
|
|
55
|
|
|
$this->deprecationNotice(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/* ******************************************************************************** |
|
59
|
|
|
* Implementation |
|
60
|
|
|
* ******************************************************************************** |
|
61
|
|
|
*/ |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* For a content item to be processed, extract generated footnotes. |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function processItem() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->fixFootnotes(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
View Code Duplication |
protected function fixFootnotes() |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
$content = $this->item['content']; |
|
74
|
|
|
|
|
75
|
|
|
// fix footnotes ref in text |
|
76
|
|
|
$content = preg_replace('/id="fnref(\d*):/', 'id="fnref$1-', $content); |
|
77
|
|
|
$content = str_replace('href="#fn:', 'href="#fn-', $content); |
|
78
|
|
|
|
|
79
|
|
|
// fix footnotes |
|
80
|
|
|
$content = str_replace('id="fn:', 'id="fn-', $content); |
|
81
|
|
|
$content = preg_replace('/href="#fnref(\d*):/', 'href="#fnref$1-', $content); |
|
82
|
|
|
|
|
83
|
|
|
// fix return sign used |
|
84
|
|
|
$content = str_replace('↩', '[↵]', $content); |
|
85
|
|
|
|
|
86
|
|
|
$this->item['content'] = $content; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.