1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of PHP-Typography. |
4
|
|
|
* |
5
|
|
|
* Copyright 2017 Peter Putzer. |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or modify modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License along |
18
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
19
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
20
|
|
|
* |
21
|
|
|
* *** |
22
|
|
|
* |
23
|
|
|
* @package mundschenk-at/php-typography |
24
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace PHP_Typography\Fixes; |
28
|
|
|
|
29
|
|
|
use PHP_Typography\Settings; |
30
|
|
|
|
31
|
|
|
use PHP_Typography\Hyphenator\Cache; |
32
|
|
|
|
33
|
|
|
use PHP_Typography\Fixes\Node_Fix; |
34
|
|
|
use PHP_Typography\Fixes\Token_Fix; |
35
|
|
|
use PHP_Typography\Fixes\Token_Fixes\Hyphenate_Fix; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Manages the fixes used by PHP_Typography. |
39
|
|
|
* |
40
|
|
|
* @author Peter Putzer <[email protected]> |
41
|
|
|
* |
42
|
|
|
* @since 6.0.0 |
43
|
|
|
*/ |
44
|
|
|
class Registry { |
45
|
|
|
|
46
|
|
|
const CHARACTERS = 10; |
47
|
|
|
const SPACING_PRE_WORDS = 20; |
48
|
|
|
const PROCESS_WORDS = 30; |
49
|
|
|
const SPACING_POST_WORDS = 40; |
50
|
|
|
const HTML_INSERTION = 50; |
51
|
|
|
|
52
|
|
|
const GROUPS = [ self::CHARACTERS, self::SPACING_PRE_WORDS, self::PROCESS_WORDS, self::SPACING_POST_WORDS, self::HTML_INSERTION ]; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* An array of Node_Fix implementations. |
56
|
|
|
* |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
private $node_fixes = [ |
60
|
|
|
self::CHARACTERS => [], |
61
|
|
|
self::SPACING_PRE_WORDS => [], |
62
|
|
|
self::PROCESS_WORDS => [], |
63
|
|
|
self::SPACING_POST_WORDS => [], |
64
|
|
|
self::HTML_INSERTION => [], |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The token fix registry. |
69
|
|
|
* |
70
|
|
|
* @var Node_Fixes\Process_Words_Fix |
71
|
|
|
*/ |
72
|
|
|
private $process_words_fix; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Creates new registry instance. |
76
|
|
|
*/ |
77
|
1 |
|
public function __construct() { |
78
|
|
|
// Parse and process individual words. |
79
|
1 |
|
$this->process_words_fix = new Node_Fixes\Process_Words_Fix(); |
80
|
1 |
|
$this->register_node_fix( $this->process_words_fix, self::PROCESS_WORDS ); |
81
|
1 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Retrieves the registered node fixes. |
85
|
|
|
* |
86
|
|
|
* @return Node_Fix[][] |
87
|
|
|
*/ |
88
|
1 |
|
public function get_node_fixes() { |
89
|
1 |
|
return $this->node_fixes; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Registers a node fix. |
94
|
|
|
* |
95
|
|
|
* @since 5.0.0 |
96
|
|
|
* |
97
|
|
|
* @param Node_Fix $fix Required. |
98
|
|
|
* @param int $group Required. Only the constants CHARACTERS, SPACING_PRE_WORDS, SPACING_POST_WORDS, HTML_INSERTION are valid. |
99
|
|
|
* |
100
|
|
|
* @throws \InvalidArgumentException Group is invalid. |
101
|
|
|
*/ |
102
|
2 |
|
public function register_node_fix( Node_Fix $fix, $group ) { |
103
|
2 |
|
if ( isset( $this->node_fixes[ $group ] ) ) { |
104
|
1 |
|
$this->node_fixes[ $group ][] = $fix; |
105
|
|
|
} else { |
106
|
1 |
|
throw new \InvalidArgumentException( "Invalid fixer group $group." ); |
107
|
|
|
} |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Registers a token fix. |
112
|
|
|
* |
113
|
|
|
* @param Token_Fix $fix Required. |
114
|
|
|
*/ |
115
|
1 |
|
public function register_token_fix( Token_Fix $fix ) { |
116
|
1 |
|
$this->process_words_fix->register_token_fix( $fix ); |
117
|
1 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Sets the hyphenator cache for all registered token fixes (that require one). |
121
|
|
|
* |
122
|
|
|
* @param Cache $cache A hyphenator cache instance. |
123
|
|
|
*/ |
124
|
1 |
|
public function update_hyphenator_cache( Cache $cache ) { |
125
|
1 |
|
$this->process_words_fix->update_hyphenator_cache( $cache ); |
126
|
1 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Applies typography fixes to a textnode. |
130
|
|
|
* |
131
|
|
|
* @param \DOMText $textnode The node to process. |
132
|
|
|
* @param Settings $settings The settings to apply. |
133
|
|
|
* @param bool $is_title Treat as title/heading tag if true. |
134
|
|
|
* @param bool $is_feed Check for feed compatibility if true. |
135
|
|
|
*/ |
136
|
2 |
|
public function apply_fixes( \DOMText $textnode, Settings $settings, $is_title, $is_feed ) { |
137
|
2 |
|
foreach ( $this->node_fixes as $group => $fixes ) { |
138
|
2 |
|
foreach ( $fixes as $fix ) { |
139
|
2 |
|
if ( ! $is_feed || $fix->feed_compatible() ) { |
140
|
2 |
|
$fix->apply( $textnode, $settings, $is_title ); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
2 |
|
} |
145
|
|
|
} |
146
|
|
|
|