|
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\Settings; |
|
30
|
|
|
use PHP_Typography\DOM; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Applies smart diacritics (if enabled). |
|
34
|
|
|
* |
|
35
|
|
|
* @author Peter Putzer <[email protected]> |
|
36
|
|
|
* |
|
37
|
|
|
* @since 5.0.0 |
|
38
|
|
|
*/ |
|
39
|
|
|
class Smart_Diacritics_Fix extends Abstract_Node_Fix { |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Apply the fix to a given textnode. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \DOMText $textnode Required. |
|
45
|
|
|
* @param Settings $settings Required. |
|
46
|
|
|
* @param bool $is_title Optional. Default false. |
|
47
|
|
|
*/ |
|
48
|
11 |
|
public function apply( \DOMText $textnode, Settings $settings, $is_title = false ) { |
|
49
|
11 |
|
if ( empty( $settings[ Settings::SMART_DIACRITICS ] ) ) { |
|
50
|
5 |
|
return; // abort. |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ( |
|
54
|
6 |
|
! empty( $settings[ Settings::DIACRITIC_REPLACEMENT_DATA ] ) && |
|
55
|
6 |
|
! empty( $settings[ Settings::DIACRITIC_REPLACEMENT_DATA ]['patterns'] ) && |
|
56
|
6 |
|
! empty( $settings[ Settings::DIACRITIC_REPLACEMENT_DATA ]['replacements'] ) |
|
57
|
|
|
) { |
|
58
|
|
|
|
|
59
|
|
|
// Uses "word" => "replacement" pairs from an array to make fast preg_* replacements. |
|
60
|
6 |
|
$replacements = $settings[ Settings::DIACRITIC_REPLACEMENT_DATA ]['replacements']; |
|
61
|
6 |
|
$textnode->data = \preg_replace_callback( |
|
62
|
6 |
|
$settings[ Settings::DIACRITIC_REPLACEMENT_DATA ]['patterns'], |
|
63
|
|
|
function( $match ) use ( $replacements ) { |
|
64
|
5 |
|
if ( isset( $replacements[ $match[0] ] ) ) { |
|
65
|
4 |
|
return $replacements[ $match[0] ]; |
|
66
|
|
|
} else { |
|
67
|
1 |
|
return $match[0]; |
|
68
|
|
|
} |
|
69
|
6 |
|
}, |
|
70
|
6 |
|
$textnode->data |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
6 |
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|