1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of PHP-Typography. |
4
|
|
|
* |
5
|
|
|
* Copyright 2017-2019 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\Node_Fixes; |
28
|
|
|
|
29
|
|
|
use PHP_Typography\Text_Parser; |
30
|
|
|
use PHP_Typography\Settings; |
31
|
|
|
use PHP_Typography\DOM; |
32
|
|
|
use PHP_Typography\Hyphenator\Cache; |
33
|
|
|
use PHP_Typography\Fixes\Token_Fix; |
34
|
|
|
use PHP_Typography\Fixes\Token_Fixes\Hyphenate_Fix; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Tokenizes the content of a textnode and process the individual words separately. |
38
|
|
|
* |
39
|
|
|
* Currently this functions applies the following enhancements: |
40
|
|
|
* - wrapping hard hyphens |
41
|
|
|
* - hyphenation |
42
|
|
|
* - wrapping URLs |
43
|
|
|
* - wrapping email addresses |
44
|
|
|
* |
45
|
|
|
* @author Peter Putzer <[email protected]> |
46
|
|
|
* |
47
|
|
|
* @since 5.0.0 |
48
|
|
|
*/ |
49
|
|
|
class Process_Words_Fix extends Abstract_Node_Fix { |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* An array of token fixes. |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
private $token_fixes = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* A custom parser for \DOMText to separate words, whitespace etc. for HTML injection. |
60
|
|
|
* |
61
|
|
|
* @var Text_Parser |
62
|
|
|
*/ |
63
|
|
|
private $text_parser; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Apply the fix to a given textnode. |
67
|
|
|
* |
68
|
|
|
* @param \DOMText $textnode Required. |
69
|
|
|
* @param Settings $settings Required. |
70
|
|
|
* @param bool $is_title Optional. Default false. |
71
|
|
|
*/ |
72
|
4 |
|
public function apply( \DOMText $textnode, Settings $settings, $is_title = false ) { |
73
|
|
|
// Lazy-load text parser. |
74
|
4 |
|
$text_parser = $this->get_text_parser(); |
75
|
4 |
|
$tokens = []; |
76
|
|
|
|
77
|
|
|
// Set up parameters for word categories. |
78
|
4 |
|
$mixed_caps = empty( $settings[ Settings::HYPHENATE_ALL_CAPS ] ) ? Text_Parser::ALLOW_ALL_CAPS : Text_Parser::NO_ALL_CAPS; |
79
|
4 |
|
$letter_caps = empty( $settings[ Settings::HYPHENATE_ALL_CAPS ] ) ? Text_Parser::NO_ALL_CAPS : Text_Parser::ALLOW_ALL_CAPS; |
80
|
4 |
|
$mixed_compounds = empty( $settings[ Settings::HYPHENATE_COMPOUNDS ] ) ? Text_Parser::ALLOW_COMPOUNDS : Text_Parser::NO_COMPOUNDS; |
81
|
4 |
|
$letter_compounds = empty( $settings[ Settings::HYPHENATE_COMPOUNDS ] ) ? Text_Parser::NO_COMPOUNDS : Text_Parser::ALLOW_COMPOUNDS; |
82
|
|
|
|
83
|
|
|
// Break text down for a bit more granularity. |
84
|
4 |
|
$text_parser->load( $textnode->data ); |
85
|
4 |
|
$tokens[ Token_Fix::MIXED_WORDS ] = $text_parser->get_words( Text_Parser::NO_ALL_LETTERS, $mixed_caps, $mixed_compounds ); // prohibit letter-only words, allow caps, allow compounds (or not). |
86
|
4 |
|
$tokens[ Token_Fix::COMPOUND_WORDS ] = ! empty( $settings[ Settings::HYPHENATE_COMPOUNDS ] ) ? $text_parser->get_words( Text_Parser::NO_ALL_LETTERS, $letter_caps, Text_Parser::REQUIRE_COMPOUNDS ) : []; |
87
|
4 |
|
$tokens[ Token_Fix::WORDS ] = $text_parser->get_words( Text_Parser::REQUIRE_ALL_LETTERS, $letter_caps, $letter_compounds ); // require letter-only words allow/prohibit caps & compounds vice-versa. |
88
|
4 |
|
$tokens[ Token_Fix::OTHER ] = $text_parser->get_other(); |
89
|
|
|
|
90
|
|
|
// Process individual text parts here. |
91
|
4 |
|
foreach ( $this->token_fixes as $fix ) { |
92
|
4 |
|
$t = $fix->target(); |
93
|
|
|
|
94
|
4 |
|
$tokens[ $t ] = $fix->apply( $tokens[ $t ], $settings, $is_title, $textnode ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Apply updates to our text. |
98
|
4 |
|
$text_parser->update( $tokens[ Token_Fix::MIXED_WORDS ] + $tokens[ Token_Fix::COMPOUND_WORDS ] + $tokens[ Token_Fix::WORDS ] + $tokens[ Token_Fix::OTHER ] ); |
99
|
4 |
|
$textnode->data = $text_parser->unload(); |
100
|
4 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Retrieves the text parser instance. |
104
|
|
|
* |
105
|
|
|
* @return \PHP_Typography\Text_Parser |
106
|
|
|
*/ |
107
|
1 |
|
public function get_text_parser() { |
108
|
|
|
// Lazy-load text parser. |
109
|
1 |
|
if ( ! isset( $this->text_parser ) ) { |
110
|
1 |
|
$this->text_parser = new Text_Parser(); |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
return $this->text_parser; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Registers a new token fix. |
118
|
|
|
* |
119
|
|
|
* @param Token_Fix $fix Required. |
120
|
|
|
*/ |
121
|
1 |
|
public function register_token_fix( Token_Fix $fix ) { |
122
|
1 |
|
$this->token_fixes[] = $fix; |
123
|
1 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Sets the hyphenator cache for all registered token fixes (that require one). |
127
|
|
|
* |
128
|
|
|
* @param Cache $cache A hyphenator cache instance. |
129
|
|
|
*/ |
130
|
1 |
|
public function update_hyphenator_cache( Cache $cache ) { |
131
|
1 |
|
foreach ( $this->token_fixes as $fix ) { |
132
|
1 |
|
if ( $fix instanceof Hyphenate_Fix ) { |
133
|
1 |
|
$fix->set_hyphenator_cache( $cache ); |
134
|
|
|
} |
135
|
|
|
} |
136
|
1 |
|
} |
137
|
|
|
} |
138
|
|
|
|