|
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\EasybookEvents; |
|
13
|
|
|
use Easybook\Events\ParseEvent; |
|
14
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
15
|
|
|
use Trefoil\Helpers\DropCaps; |
|
16
|
|
|
use Trefoil\Plugins\BasePlugin; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Add drop caps to the book. |
|
20
|
|
|
* |
|
21
|
|
|
* For formats: all |
|
22
|
|
|
* |
|
23
|
|
|
* It provides two working modes: |
|
24
|
|
|
* |
|
25
|
|
|
* 1.- Automatic dropcaps: Depending on the options set in the plugins configuration |
|
26
|
|
|
* section inside the book's config.yml: |
|
27
|
|
|
* |
|
28
|
|
|
* editions: |
|
29
|
|
|
* <edition-name> |
|
30
|
|
|
* plugins: |
|
31
|
|
|
* ... |
|
32
|
|
|
* options: |
|
33
|
|
|
* DropCaps: |
|
34
|
|
|
* levels: [1] # 1 to 6 (default: 1) |
|
35
|
|
|
* mode: letter # letter, word (default: letter) |
|
36
|
|
|
* length: 1 # number of letters or words to highlight (default: 1) |
|
37
|
|
|
* coverage: ['chapter'] # book elements to process |
|
38
|
|
|
* |
|
39
|
|
|
* 2. Manual dropcaps: Besides adding the HTML markup directly, which off course is still |
|
40
|
|
|
* possible, a Markdown-like markup is provided for greater convenience: |
|
41
|
|
|
* |
|
42
|
|
|
* [[T]]his text has first-letter dropcaps. |
|
43
|
|
|
* |
|
44
|
|
|
* [[But]] this text has first-word dropcaps. |
|
45
|
|
|
* |
|
46
|
|
|
*/ |
|
47
|
|
|
class DropCapsPlugin extends BasePlugin implements EventSubscriberInterface |
|
48
|
|
|
{ |
|
49
|
2 |
|
public static function getSubscribedEvents() |
|
50
|
2 |
|
{ |
|
51
|
|
|
return array( |
|
52
|
2 |
|
EasybookEvents::POST_PARSE => array('onItemPostParse', -1100) |
|
53
|
2 |
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
public function onItemPostParse(ParseEvent $event) |
|
57
|
|
|
{ |
|
58
|
2 |
|
$this->init($event); |
|
59
|
|
|
|
|
60
|
2 |
|
$content = $event->getItemProperty('content'); |
|
61
|
|
|
|
|
62
|
2 |
|
$content = $this->addDropCaps($content); |
|
63
|
|
|
|
|
64
|
2 |
|
$event->setItemProperty('content', $content); |
|
65
|
2 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Add drop caps markup |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $content |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
2 |
|
protected function addDropCaps($content) |
|
75
|
|
|
{ |
|
76
|
2 |
|
$length = $this->getEditionOption('plugins.options.DropCaps.length', 1); |
|
77
|
2 |
|
$mode = $this->getEditionOption('plugins.options.DropCaps.mode', 'letter'); |
|
78
|
2 |
|
$levels = $this->getEditionOption('plugins.options.DropCaps.levels', array(1)); |
|
79
|
2 |
|
$elements = $this->getEditionOption('plugins.options.DropCaps.elements', array('chapter')); |
|
80
|
|
|
|
|
81
|
|
|
// ensure levels is an array |
|
82
|
2 |
|
if (!is_array($levels)) { |
|
83
|
|
|
$levels = array(1); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
2 |
|
if (!in_array($this->item['config']['element'], $elements)) { |
|
87
|
|
|
// not for this element |
|
88
|
|
|
return $content; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
$dropCaps = new DropCaps($content, $mode, $length); |
|
92
|
|
|
|
|
93
|
|
|
// first of all, process the Markdown-style markup |
|
94
|
2 |
|
$dropCaps->createForMarkdownStyleMarkup(); |
|
95
|
|
|
|
|
96
|
|
|
/* the paragraph that starts the text must be treated separately |
|
97
|
|
|
* because it doesn't have a preceding heading tag (it will be under |
|
98
|
|
|
* the H1 item title tag that is not there yet), |
|
99
|
|
|
*/ |
|
100
|
2 |
|
if (in_array(1, $levels)) { |
|
101
|
2 |
|
$dropCaps->createForFirstParagraph(); |
|
102
|
2 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
// process the other paragraphs in the text that follow a heading tag |
|
105
|
2 |
|
$dropCaps->createForHeadings($levels); |
|
106
|
|
|
|
|
107
|
|
|
// process the paragraphs with manually-added dropcaps markup |
|
108
|
2 |
|
$dropCaps->processManualMarkup(); |
|
109
|
|
|
|
|
110
|
2 |
|
$content = $dropCaps->getOutput(); |
|
111
|
|
|
|
|
112
|
2 |
|
return $content; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|