forked from frameless-at/TextformatterSmartQuotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextformatterSmartQuotes.module.php
More file actions
74 lines (64 loc) · 2.13 KB
/
Copy pathTextformatterSmartQuotes.module.php
File metadata and controls
74 lines (64 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
* ProcessWire TextformatterSmartQuotes
*
* Replaces straight double quotes "..." with typographic quotes („…“, “…” or «…»),
* but only in visible text content, leaving HTML tags and attributes untouched.
*
* @author frameless Media
*/
class TextformatterSmartQuotes extends Textformatter implements ConfigurableModule {
public static function getModuleInfo() {
return [
'title' => 'Textformatter Smart Quotes',
'version' => 1,
'summary' => 'Replaces straight quotes "..." with typographic quotes („...“, “...”, or «...»), in visible text only.',
'author' => 'frameless Media',
'autoload' => false,
'singular' => true,
'icon' => 'quote-right',
];
}
public function getModuleConfigInputfields(array $data) {
$inputfields = new InputfieldWrapper();
$field = wire('modules')->get('InputfieldSelect');
$field->name = 'quoteStyle';
$field->label = 'Quote style';
$field->options = [
'german' => 'German: „...”',
'english' => 'English: “...”',
'french' => 'French: « ... »',
];
$field->defaultValue = 'german';
$field->value = $data['quoteStyle'] ?? 'german';
$inputfields->add($field);
return $inputfields;
}
public function formatValue(Page $page, Field $field, &$value) {
$style = $this->quoteStyle ?? 'german';
// Define opening and closing quote chars based on selected style
$quotes = [
'german' => ['open' => '„', 'close' => '“'],
'english' => ['open' => '“', 'close' => '”'],
'french' => ['open' => '« ', 'close' => ' »'], // French uses non-breaking spaces
];
$open = $quotes[$style]['open'] ?? '„';
$close = $quotes[$style]['close'] ?? '“';
// If no HTML, do simple replacement
if (strpos($value, '<') === false) {
$value = preg_replace('/"([^"]+)"/', $open . '$1' . $close, $value);
return;
}
// Replace quotes only in text, not HTML
$value = preg_replace_callback(
'/(<[^>]+>)|([^<]+)/',
function($matches) use ($open, $close) {
if (!empty($matches[2])) {
return preg_replace('/"([^"]+)"/', $open . '$1' . $close, $matches[2]);
}
return $matches[1];
},
$value
);
}
}