1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of PHP-Typography. |
4
|
|
|
* |
5
|
|
|
* Copyright 2016-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\DOM; |
30
|
|
|
use PHP_Typography\Settings; |
31
|
|
|
use PHP_Typography\Strings; |
32
|
|
|
use PHP_Typography\U; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Adds a narrow no-break space before |
36
|
|
|
* - exclamation mark (!) |
37
|
|
|
* - question mark (?) |
38
|
|
|
* - semicolon (;) |
39
|
|
|
* - colon (:) |
40
|
|
|
* |
41
|
|
|
* If there already is a space there, it is replaced. |
42
|
|
|
* |
43
|
|
|
* @author Peter Putzer <[email protected]> |
44
|
|
|
* |
45
|
|
|
* @since 5.0.0 |
46
|
|
|
*/ |
47
|
|
|
class French_Punctuation_Spacing_Fix extends Abstract_Node_Fix { |
48
|
|
|
// Regular expressions with mandatary Unicode modifier. |
49
|
|
|
const INSERT_NARROW_SPACE = '/(\w+(?:\s?»)?)(\s?)([?!;])(\s|\Z)/u'; |
50
|
|
|
const INSERT_FULL_SPACE = '/(\w+(?:\s?»)?)(\s?)(:)(\s|\Z)/u'; |
51
|
|
|
const INSERT_SPACE_AFTER_OPENING_QUOTE = '/(\s|\A|[\(\[])(«)(\s?)(\w+)/u'; |
52
|
|
|
const INSERT_SPACE_BEFORE_CLOSING_QUOTE = '/(\w+[.?!]?)(\s?)(»)(\s|[.,?!:\)\]]|\Z)/u'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Apply the fix to a given textnode. |
56
|
|
|
* |
57
|
|
|
* @param \DOMText $textnode Required. |
58
|
|
|
* @param Settings $settings Required. |
59
|
|
|
* @param bool $is_title Optional. Default false. |
60
|
|
|
*/ |
61
|
36 |
|
public function apply( \DOMText $textnode, Settings $settings, $is_title = false ) { |
62
|
36 |
|
if ( empty( $settings[ Settings::FRENCH_PUNCTUATION_SPACING ] ) ) { |
63
|
17 |
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Need to get context of adjacent characters outside adjacent inline tags or HTML comment |
67
|
|
|
// if we have adjacent characters add them to the text. |
68
|
19 |
|
$previous_character = DOM::get_prev_chr( $textnode ); |
69
|
19 |
|
$next_character = DOM::get_next_chr( $textnode ); |
70
|
19 |
|
$node_data = "{$previous_character}{$textnode->data}"; // $next_character is not included on purpose. |
71
|
19 |
|
$f = Strings::functions( "{$node_data}{$next_character}" ); // Include $next_character for determining encodiing. |
72
|
|
|
|
73
|
19 |
|
$node_data = \preg_replace( |
74
|
|
|
[ |
75
|
19 |
|
self::INSERT_SPACE_BEFORE_CLOSING_QUOTE, |
76
|
19 |
|
self::INSERT_NARROW_SPACE, |
77
|
19 |
|
self::INSERT_FULL_SPACE, |
78
|
|
|
], |
79
|
|
|
[ |
80
|
19 |
|
'$1' . U::NO_BREAK_NARROW_SPACE . '$3$4', |
81
|
|
|
'$1' . U::NO_BREAK_NARROW_SPACE . '$3$4', |
82
|
|
|
'$1' . U::NO_BREAK_SPACE . '$3$4', |
83
|
|
|
], |
84
|
19 |
|
$node_data |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
// The next rule depends on the following characters as well. |
88
|
19 |
|
$node_data = \preg_replace( self::INSERT_SPACE_AFTER_OPENING_QUOTE, '$1$2' . U::NO_BREAK_NARROW_SPACE . '$4', "{$node_data}{$next_character}" ); |
89
|
|
|
|
90
|
|
|
// If we have adjacent characters remove them from the text. |
91
|
19 |
|
$textnode->data = self::remove_adjacent_characters( $node_data, $f['strlen'], $f['substr'], $f['strlen']( $previous_character ), $f['strlen']( $next_character ) ); |
92
|
19 |
|
} |
93
|
|
|
} |
94
|
|
|
|