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 |
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\Token_Fixes; |
28
|
|
|
|
29
|
|
|
use PHP_Typography\Fixes\Token_Fix; |
30
|
|
|
use PHP_Typography\DOM; |
31
|
|
|
use PHP_Typography\Hyphenator; |
32
|
|
|
use PHP_Typography\Hyphenator\Cache; |
33
|
|
|
use PHP_Typography\Settings; |
34
|
|
|
use PHP_Typography\Text_Parser\Token; |
35
|
|
|
use PHP_Typography\U; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Hyphenates a given text fragment (if enabled). |
39
|
|
|
* |
40
|
|
|
* @author Peter Putzer <[email protected]> |
41
|
|
|
* |
42
|
|
|
* @since 5.0.0 |
43
|
|
|
*/ |
44
|
|
|
class Hyphenate_Fix extends Abstract_Token_Fix { |
45
|
|
|
/** |
46
|
|
|
* An array of ( $tag => true ) for quick checking with `isset`. |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private $heading_tags = [ |
51
|
|
|
'h1' => true, |
52
|
|
|
'h2' => true, |
53
|
|
|
'h3' => true, |
54
|
|
|
'h4' => true, |
55
|
|
|
'h5' => true, |
56
|
|
|
'h6' => true, |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The cache for Hyphenator instances. |
61
|
|
|
* |
62
|
|
|
* @var Hyphenator\Cache |
63
|
|
|
*/ |
64
|
|
|
protected $cache; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Creates a new fix instance. |
68
|
|
|
* |
69
|
|
|
* @param Cache|null $cache Optional. Default null. |
70
|
|
|
* @param int $target Optional. Default Token_Fix::WORDS. |
71
|
|
|
* @param bool $feed_compatible Optional. Default false. |
72
|
|
|
*/ |
73
|
1 |
|
public function __construct( Cache $cache = null, $target = Token_Fix::WORDS, $feed_compatible = false ) { |
74
|
1 |
|
parent::__construct( $target, $feed_compatible ); |
75
|
|
|
|
76
|
1 |
|
if ( null === $cache ) { |
77
|
1 |
|
$cache = new Hyphenator\Cache(); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
$this->cache = $cache; |
81
|
1 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Apply the tweak to a given textnode. |
85
|
|
|
* |
86
|
|
|
* @param Token[] $tokens Required. |
87
|
|
|
* @param Settings $settings Required. |
88
|
|
|
* @param bool $is_title Optional. Default false. |
89
|
|
|
* @param \DOMText|null $textnode Optional. Default null. |
90
|
|
|
* |
91
|
|
|
* @return Token[] An array of tokens. |
92
|
|
|
*/ |
93
|
14 |
|
public function apply( array $tokens, Settings $settings, $is_title = false, \DOMText $textnode = null ) { |
94
|
14 |
|
if ( empty( $settings[ Settings::HYPHENATION ] ) ) { |
95
|
7 |
|
return $tokens; // abort. |
96
|
|
|
} |
97
|
|
|
|
98
|
7 |
|
$is_heading = false; |
99
|
7 |
|
if ( null !== $textnode && ! empty( $textnode->parentNode ) ) { |
100
|
7 |
|
$block_level_parent = DOM::get_block_parent_name( $textnode ); |
101
|
|
|
|
102
|
7 |
|
if ( ! empty( $block_level_parent ) && isset( $this->heading_tags[ $block_level_parent ] ) ) { |
103
|
2 |
|
$is_heading = true; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
7 |
|
if ( empty( $settings[ Settings::HYPHENATE_HEADINGS ] ) && ( $is_title || $is_heading ) ) { |
108
|
1 |
|
return $tokens; // abort. |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Call functionality as seperate function so it can be run without test for setting[ Settings::HYPHENATION ] - such as with url wrapping. |
112
|
6 |
|
return $this->do_hyphenate( $tokens, $settings ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Really hyphenates given text fragment. |
117
|
|
|
* |
118
|
|
|
* @param Token[] $tokens Filtered to words. |
119
|
|
|
* @param Settings $settings The settings to apply. |
120
|
|
|
* @param string $hyphen Hyphenation character. Optional. Default is the soft hyphen character (`­`). |
121
|
|
|
* |
122
|
|
|
* @return Token[] The hyphenated text tokens. |
123
|
|
|
*/ |
124
|
3 |
|
protected function do_hyphenate( array $tokens, Settings $settings, $hyphen = U::SOFT_HYPHEN ) { |
125
|
3 |
|
if ( empty( $settings[ Settings::HYPHENATION_MIN_LENGTH ] ) || empty( $settings[ Settings::HYPHENATION_MIN_BEFORE ] ) ) { |
126
|
1 |
|
return $tokens; |
127
|
|
|
} |
128
|
|
|
|
129
|
2 |
|
return $this->get_hyphenator( $settings )->hyphenate( $tokens, $hyphen, ! empty( $settings[ Settings::HYPHENATE_TITLE_CASE ] ), $settings[ Settings::HYPHENATION_MIN_LENGTH ], $settings[ Settings::HYPHENATION_MIN_BEFORE ], $settings[ Settings::HYPHENATION_MIN_AFTER ] ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Retrieves the hyphenator instance. |
134
|
|
|
* |
135
|
|
|
* @param Settings $settings The settings to apply. |
136
|
|
|
* |
137
|
|
|
* @return Hyphenator |
138
|
|
|
*/ |
139
|
1 |
|
public function get_hyphenator( Settings $settings ) { |
140
|
1 |
|
$lang = $settings[ Settings::HYPHENATION_LANGUAGE ]; |
141
|
1 |
|
$exceptions = (array) $settings[ Settings::HYPHENATION_CUSTOM_EXCEPTIONS ]; |
142
|
1 |
|
$hyphenator = $this->cache->get_hyphenator( $lang ); |
143
|
|
|
|
144
|
1 |
|
if ( null === $hyphenator ) { |
145
|
1 |
|
$hyphenator = new Hyphenator( $lang, $exceptions ); |
146
|
1 |
|
$this->cache->set_hyphenator( $lang, $hyphenator ); |
147
|
|
|
} else { |
148
|
1 |
|
$hyphenator->set_language( $lang ); // just for insurance. |
149
|
1 |
|
$hyphenator->set_custom_exceptions( $exceptions ); |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
return $hyphenator; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Injects an existing Hyphenator instance (to facilitate language caching). |
157
|
|
|
* |
158
|
|
|
* @param Hyphenator\Cache $cache Required. |
159
|
|
|
*/ |
160
|
1 |
|
public function set_hyphenator_cache( Hyphenator\Cache $cache ) { |
161
|
1 |
|
$this->cache = $cache; |
162
|
1 |
|
} |
163
|
|
|
} |
164
|
|
|
|