|
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 |
|
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\Hyphenator\Cache; |
|
31
|
|
|
use PHP_Typography\Settings; |
|
32
|
|
|
use PHP_Typography\Text_Parser; |
|
33
|
|
|
use PHP_Typography\Text_Parser\Token; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Hyphenates hyphenated compound words (if enabled). |
|
37
|
|
|
* |
|
38
|
|
|
* Calls hyphenate() on the component words. |
|
39
|
|
|
* |
|
40
|
|
|
* @author Peter Putzer <[email protected]> |
|
41
|
|
|
* |
|
42
|
|
|
* @since 5.0.0 |
|
43
|
|
|
*/ |
|
44
|
|
|
class Hyphenate_Compounds_Fix extends Hyphenate_Fix { |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Creates a new fix instance. |
|
48
|
|
|
* |
|
49
|
|
|
* @param Cache|null $cache Optional. Default null. |
|
50
|
|
|
* @param bool $feed_compatible Optional. Default false. |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function __construct( Cache $cache = null, $feed_compatible = false ) { |
|
53
|
1 |
|
parent::__construct( $cache, Token_Fix::COMPOUND_WORDS, $feed_compatible ); |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Apply the tweak to a given textnode. |
|
58
|
|
|
* |
|
59
|
|
|
* @param Token[] $tokens Required. |
|
60
|
|
|
* @param Settings $settings Required. |
|
61
|
|
|
* @param bool $is_title Optional. Default false. |
|
62
|
|
|
* @param \DOMText|null $textnode Optional. Default null. |
|
63
|
|
|
* |
|
64
|
|
|
* @return Token[] An array of tokens. |
|
65
|
|
|
*/ |
|
66
|
4 |
|
public function apply( array $tokens, Settings $settings, $is_title = false, \DOMText $textnode = null ) { |
|
67
|
4 |
|
if ( empty( $settings[ Settings::HYPHENATE_COMPOUNDS ] ) ) { |
|
68
|
2 |
|
return $tokens; // abort. |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// Hyphenate compound words. |
|
72
|
2 |
|
foreach ( $tokens as $key => $word_token ) { |
|
73
|
2 |
|
$component_words = []; |
|
74
|
2 |
|
foreach ( \preg_split( '/(-)/', $word_token->value, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ) as $word_part ) { |
|
75
|
2 |
|
$component_words[] = new Text_Parser\Token( $word_part, Text_Parser\Token::WORD ); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
$tokens[ $key ] = $word_token->with_value( |
|
79
|
2 |
|
\array_reduce( |
|
80
|
2 |
|
parent::apply( $component_words, $settings, $is_title, $textnode ), |
|
81
|
|
|
function( $carry, $item ) { |
|
82
|
2 |
|
return $carry . $item->value; |
|
83
|
2 |
|
} |
|
84
|
|
|
) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
return $tokens; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|