1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of PHP-Typography. |
4
|
|
|
* |
5
|
|
|
* Copyright 2014-2019 Peter Putzer. |
6
|
|
|
* Copyright 2009-2011 KINGdesk, LLC. |
7
|
|
|
* |
8
|
|
|
* This program is free software; you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU General Public License as published by |
10
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License along |
19
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
20
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
21
|
|
|
* |
22
|
|
|
* *** |
23
|
|
|
* |
24
|
|
|
* @package mundschenk-at/php-typography |
25
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
namespace PHP_Typography\Fixes\Node_Fixes; |
29
|
|
|
|
30
|
|
|
use PHP_Typography\DOM; |
31
|
|
|
use PHP_Typography\RE; |
32
|
|
|
use PHP_Typography\Settings; |
33
|
|
|
use PHP_Typography\U; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Applies smart ordinal suffix (if enabled). |
37
|
|
|
* |
38
|
|
|
* Call before style_numbers. |
39
|
|
|
* |
40
|
|
|
* @author Peter Putzer <[email protected]> |
41
|
|
|
* |
42
|
|
|
* @since 5.0.0 |
43
|
|
|
*/ |
44
|
|
|
class Smart_Ordinal_Suffix_Fix extends Abstract_Node_Fix { |
45
|
|
|
|
46
|
|
|
// Possible suffixes. |
47
|
|
|
const ENGLISH_SUFFIXES = 'st|nd|rd|th'; |
48
|
|
|
const FRENCH_SUFFIXES = 'er|re|e|ère|d|nd|nde|de|me|ème|è'; |
49
|
|
|
const LATIN_SUFFIXES = 'o'; |
50
|
|
|
|
51
|
|
|
// Ordinals with arabic numerals. |
52
|
|
|
const RE_ARABIC_ORDINALS = '/' . |
53
|
|
|
self::WORD_BOUNDARY_START . ' |
54
|
|
|
(\d+) |
55
|
|
|
(' . |
56
|
|
|
self::ENGLISH_SUFFIXES . '|' . |
57
|
|
|
self::FRENCH_SUFFIXES . '|' . |
58
|
|
|
self::LATIN_SUFFIXES . ' |
59
|
|
|
)' . |
60
|
|
|
self::WORD_BOUNDARY_END . ' |
61
|
|
|
/Sxu'; |
62
|
|
|
|
63
|
|
|
// Ordinals with Roman numerals. |
64
|
|
|
const RE_ROMAN_ORDINALS = '/' . |
65
|
|
|
self::WORD_BOUNDARY_START . ' |
66
|
|
|
( |
67
|
|
|
# Prevent single letter numbers other than I, V, and X. |
68
|
|
|
(?=(?:I|V|X|' . self::ROMAN_NUMERALS . '{2,})) |
69
|
|
|
|
70
|
|
|
# Otherwise, allow all valid Roman numbers. |
71
|
|
|
(?=' . self::ROMAN_NUMERALS . ')M*(?:C[MD]|D?C*)(?:X[CL]|L?X*)(?:I[XV]|V?I*) |
72
|
|
|
) |
73
|
|
|
(' . |
74
|
|
|
self::FRENCH_SUFFIXES . '|' . |
75
|
|
|
self::LATIN_SUFFIXES . ' |
76
|
|
|
)' . |
77
|
|
|
self::WORD_BOUNDARY_END . ' |
78
|
|
|
/Sxu'; |
79
|
|
|
|
80
|
|
|
// Additional character classes. |
81
|
|
|
const ROMAN_NUMERALS = '[MDCLXVI]'; |
82
|
|
|
|
83
|
|
|
// Zero-width spaces and soft hyphens should not be treated as word boundaries. |
84
|
|
|
const WORD_BOUNDARY_START = '\b(?<![' . U::SOFT_HYPHEN . U::ZERO_WIDTH_SPACE . '])'; |
85
|
|
|
const WORD_BOUNDARY_END = '\b(?![' . U::SOFT_HYPHEN . U::ZERO_WIDTH_SPACE . '])'; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* The replacement expression (depends on CSS class). |
89
|
|
|
* |
90
|
|
|
* @var string |
91
|
|
|
*/ |
92
|
|
|
private $replacement; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Creates a new smart ordinal suffix fixer. |
96
|
|
|
* |
97
|
|
|
* @param string|null $css_class Optional. Default null. |
98
|
|
|
* @param bool $feed_compatible Optional. Default false. |
99
|
|
|
*/ |
100
|
1 |
|
public function __construct( $css_class = null, $feed_compatible = false ) { |
101
|
1 |
|
parent::__construct( $feed_compatible ); |
102
|
|
|
|
103
|
1 |
|
$ordinal_class = empty( $css_class ) ? '' : ' class="' . $css_class . '"'; |
104
|
1 |
|
$this->replacement = RE::escape_tags( "\$1<sup{$ordinal_class}>\$2</sup>" ); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Apply the fix to a given textnode. |
109
|
|
|
* |
110
|
|
|
* @param \DOMText $textnode Required. |
111
|
|
|
* @param Settings $settings Required. |
112
|
|
|
* @param bool $is_title Optional. Default false. |
113
|
|
|
*/ |
114
|
56 |
|
public function apply( \DOMText $textnode, Settings $settings, $is_title = false ) { |
115
|
56 |
|
if ( empty( $settings[ Settings::SMART_ORDINAL_SUFFIX ] ) ) { |
116
|
22 |
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Always match Arabic numbers. |
120
|
34 |
|
$patterns = [ self::RE_ARABIC_ORDINALS ]; |
121
|
|
|
|
122
|
|
|
// Only match Roman numbers if explicitely enabled. |
123
|
34 |
|
if ( ! empty( $settings[ Settings::SMART_ORDINAL_SUFFIX_ROMAN_NUMERALS ] ) ) { |
124
|
12 |
|
$patterns[] = self::RE_ROMAN_ORDINALS; |
125
|
|
|
} |
126
|
|
|
|
127
|
34 |
|
$textnode->data = \preg_replace( $patterns, $this->replacement, $textnode->data ); |
128
|
34 |
|
} |
129
|
|
|
} |
130
|
|
|
|