|
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\Settings; |
|
31
|
|
|
use PHP_Typography\Text_Parser\Token; |
|
32
|
|
|
use PHP_Typography\U; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Wraps hard hypens with zero-width spaces (if enabled). |
|
36
|
|
|
* |
|
37
|
|
|
* @author Peter Putzer <[email protected]> |
|
38
|
|
|
* |
|
39
|
|
|
* @since 5.0.0 |
|
40
|
|
|
*/ |
|
41
|
|
|
class Wrap_Hard_Hyphens_Fix extends Abstract_Token_Fix { |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* An array of "hyphen-like" characters. |
|
45
|
|
|
* |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $hyphens_array; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The regular expression to strip the space from hyphen-like characters at the end of a string. |
|
52
|
|
|
* |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $remove_ending_space_regex; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Creates a new fix instance. |
|
59
|
|
|
* |
|
60
|
|
|
* @param bool $feed_compatible Optional. Default false. |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function __construct( $feed_compatible = false ) { |
|
63
|
1 |
|
parent::__construct( Token_Fix::MIXED_WORDS, $feed_compatible ); |
|
64
|
|
|
|
|
65
|
1 |
|
$this->hyphens_array = \array_unique( [ '-', U::HYPHEN ] ); |
|
66
|
1 |
|
$this->remove_ending_space_regex = '/(' . \implode( '|', $this->hyphens_array ) . ')' . U::ZERO_WIDTH_SPACE . '$/'; |
|
67
|
1 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Apply the tweak to a given textnode. |
|
71
|
|
|
* |
|
72
|
|
|
* @param Token[] $tokens Required. |
|
73
|
|
|
* @param Settings $settings Required. |
|
74
|
|
|
* @param bool $is_title Optional. Default false. |
|
75
|
|
|
* @param \DOMText|null $textnode Optional. Default null. |
|
76
|
|
|
* |
|
77
|
|
|
* @return Token[] An array of tokens. |
|
78
|
|
|
*/ |
|
79
|
4 |
|
public function apply( array $tokens, Settings $settings, $is_title = false, \DOMText $textnode = null ) { |
|
80
|
4 |
|
if ( ! empty( $settings[ Settings::HYPHEN_HARD_WRAP ] ) ) { |
|
81
|
|
|
|
|
82
|
2 |
|
foreach ( $tokens as $index => $text_token ) { |
|
83
|
2 |
|
$value = $text_token->value; |
|
84
|
|
|
|
|
85
|
2 |
|
if ( isset( $settings[ Settings::HYPHEN_HARD_WRAP ] ) && $settings[ Settings::HYPHEN_HARD_WRAP ] ) { |
|
86
|
2 |
|
$value = \str_replace( $this->hyphens_array, '-' . U::ZERO_WIDTH_SPACE, $value ); |
|
87
|
2 |
|
$value = \str_replace( '_', '_' . U::ZERO_WIDTH_SPACE, $value ); |
|
88
|
2 |
|
$value = \str_replace( '/', '/' . U::ZERO_WIDTH_SPACE, $value ); |
|
89
|
|
|
|
|
90
|
2 |
|
$value = \preg_replace( $this->remove_ending_space_regex, '$1', $value ); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
$tokens[ $index ] = $text_token->with_value( $value ); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
4 |
|
return $tokens; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|