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 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
|
|
|
* An abstract base class for providing simple fixes via a single regular expression replacement. |
34
|
|
|
* |
35
|
|
|
* @author Peter Putzer <[email protected]> |
36
|
|
|
* |
37
|
|
|
* @since 5.0.0 |
38
|
|
|
*/ |
39
|
|
|
abstract class Simple_Regex_Replacement_Fix extends Abstract_Node_Fix { |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The setting string used to enable/disable the fix (e.g. 'styleAmpersands'). |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $settings_switch; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The regular expressions used to match the text that should be wrapped in spans. |
50
|
|
|
* |
51
|
|
|
* It must contain a single matching expression. |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $regex; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The replacement expression. |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
protected $replacement; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Creates a new node fix with a class. |
66
|
|
|
* |
67
|
|
|
* @param string $regex Regular expression to match the text. |
68
|
|
|
* @param string $replacement A replacement expression. |
69
|
|
|
* @param string $settings_switch On/off switch for fix. |
70
|
|
|
* @param bool $feed_compatible Optional. Default false. |
71
|
|
|
*/ |
72
|
1 |
|
public function __construct( $regex, $replacement, $settings_switch, $feed_compatible = false ) { |
73
|
1 |
|
parent::__construct( $feed_compatible ); |
74
|
|
|
|
75
|
1 |
|
$this->regex = $regex . 'S'; // Add "Study" modifier. |
76
|
1 |
|
$this->settings_switch = $settings_switch; |
77
|
1 |
|
$this->replacement = $replacement; |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Apply the fix to a given textnode. |
82
|
|
|
* |
83
|
|
|
* @param \DOMText $textnode Required. |
84
|
|
|
* @param Settings $settings Required. |
85
|
|
|
* @param bool $is_title Optional. Default false. |
86
|
|
|
*/ |
87
|
2 |
|
public function apply( \DOMText $textnode, Settings $settings, $is_title = false ) { |
88
|
2 |
|
if ( empty( $settings[ $this->settings_switch ] ) ) { |
89
|
1 |
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
$textnode->data = \preg_replace( $this->regex, $this->replacement, $textnode->data ); |
93
|
1 |
|
} |
94
|
|
|
} |
95
|
|
|
|