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\Node_Fixes; |
28
|
|
|
|
29
|
|
|
use PHP_Typography\RE; |
30
|
|
|
use PHP_Typography\Settings; |
31
|
|
|
use PHP_Typography\U; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Applies smart marks (if enabled). |
35
|
|
|
* |
36
|
|
|
* @author Peter Putzer <[email protected]> |
37
|
|
|
* |
38
|
|
|
* @since 5.0.0 |
39
|
|
|
*/ |
40
|
|
|
class Smart_Marks_Fix extends Abstract_Node_Fix { |
41
|
|
|
|
42
|
|
|
const ESCAPE_501C = '/\b(501\()(c)(\)\((?:[1-9]|[1-2][0-9])\))/S'; |
43
|
|
|
|
44
|
|
|
const MARKS = [ |
45
|
|
|
'(c)' => U::COPYRIGHT, |
46
|
|
|
'(C)' => U::COPYRIGHT, |
47
|
|
|
'(r)' => U::REGISTERED_MARK, |
48
|
|
|
'(R)' => U::REGISTERED_MARK, |
49
|
|
|
'(p)' => U::SOUND_COPY_MARK, |
50
|
|
|
'(P)' => U::SOUND_COPY_MARK, |
51
|
|
|
'(sm)' => U::SERVICE_MARK, |
52
|
|
|
'(SM)' => U::SERVICE_MARK, |
53
|
|
|
'(tm)' => U::TRADE_MARK, |
54
|
|
|
'(TM)' => U::TRADE_MARK, |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* An array of marks to match. |
59
|
|
|
* |
60
|
|
|
* @since 6.0.0 |
61
|
|
|
* |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
private $marks; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* An array of replacement marks. |
68
|
|
|
* |
69
|
|
|
* @since 6.0.0 |
70
|
|
|
* |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
private $replacements; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Creates a new fix instance. |
77
|
|
|
* |
78
|
|
|
* @param bool $feed_compatible Optional. Default false. |
79
|
|
|
*/ |
80
|
1 |
|
public function __construct( $feed_compatible = false ) { |
81
|
1 |
|
parent::__construct( $feed_compatible ); |
82
|
|
|
|
83
|
1 |
|
$this->marks = \array_keys( self::MARKS ); |
84
|
1 |
|
$this->replacements = \array_values( self::MARKS ); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Apply the fix to a given textnode. |
89
|
|
|
* |
90
|
|
|
* @param \DOMText $textnode Required. |
91
|
|
|
* @param Settings $settings Required. |
92
|
|
|
* @param bool $is_title Optional. Default false. |
93
|
|
|
*/ |
94
|
26 |
|
public function apply( \DOMText $textnode, Settings $settings, $is_title = false ) { |
95
|
26 |
|
if ( empty( $settings[ Settings::SMART_MARKS ] ) ) { |
96
|
13 |
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Cache textnode content. |
100
|
13 |
|
$node_data = $textnode->data; |
101
|
|
|
|
102
|
|
|
// Escape usage of "501(c)(1...29)" (US non-profit). |
103
|
13 |
|
$node_data = \preg_replace( self::ESCAPE_501C, '$1' . RE::ESCAPE_MARKER . '$2' . RE::ESCAPE_MARKER . '$3', $node_data ); |
104
|
|
|
|
105
|
|
|
// Replace marks. |
106
|
13 |
|
$node_data = \str_replace( $this->marks, $this->replacements, $node_data ); |
107
|
|
|
|
108
|
|
|
// Un-escape escaped sequences & resetore textnode content. |
109
|
13 |
|
$textnode->data = \str_replace( RE::ESCAPE_MARKER, '', $node_data ); |
110
|
13 |
|
} |
111
|
|
|
} |
112
|
|
|
|