-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdtp.module
113 lines (95 loc) · 3.17 KB
/
dtp.module
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
// $id$
function dtp_filter_info() {
return array(
'dtp' => array(
'title' => t('Scribus'),
'description' => t('Adds DTP refinements'),
'process callback' => '_dtp_filter_process',
'settings callback' => '_dtp_filter_settings',
'default settings' => array(
'use_emdash' => 1,
'replace_spaces' => 1,
'no_dot_spaces' => 1,
'replace_quotes' => 1,
),
),
);
}
function _dtp_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) {
// Step 1: Replace multiple spaces with one
if ($filter->settings['replace_spaces']) {
$text = preg_replace('!\s+!', ' ', $text);
$text = preg_replace('!\t+!', ' ', $text);
}
// Step 2: Remove spaces before question, aclamation marks etc.
if ($filter->settings['no_dot_spaces']) {
$text = str_replace(' .', '.', $text);
$text = str_replace(' ;', ';', $text);
$text = str_replace(' :', ':', $text);
$text = str_replace(' !', '!', $text);
$text = str_replace(' ?', '?', $text);
$text = str_replace(' )', ')', $text);
$text = str_replace('( ', '(', $text);
$text = str_replace(' ]', ']', $text);
$text = str_replace('[ ', '[', $text);
$text = str_replace(' }', '}', $text);
$text = str_replace('{ ', '{', $text);
$text = str_replace(',, ', ',,', $text);
$text = str_replace(" ''", "''", $text);
$text = str_replace(' "', '"', $text);
}
// Step 3: Replace -- and --- with – and —
$text = str_replace('---', '—', $text);
if ($filter->settings['use_emdash']) {
$text = str_replace('--', '—', $text);
}
else {
$text = str_replace('--', '–', $text);
}
// Step 4: Replace ,, with „ and '' with ”
if ($filter->settings['replace_quotes']) {
$text = str_replace(',,', '„', $text);
$text = str_replace("''", '”', $text);
$text = str_replace('"', '”', $text);
}
// This is special case for commas in polish language
if ($filter->settings['no_dot_spaces']) {
$text = str_replace(' ,', ',', $text);
}
return $text;
}
function _dtp_filter_settings(&$form_state, $filter, $defaults, $format, $filters) {
$defaults->settings += array(
'use_emdash' => 1,
'replace_spaces' => 1,
'no_dot_spaces' => 1,
'replace_quotes' => 1,
);
$form = array();
$form['help'] = array(
'#type' => 'markup',
'#value' => '<p>Enable following refinements:</p>',
);
$form['use_emdash'] = array(
'#type' => 'checkbox',
'#title' => t('Replace en-dashes -- with --- em-dashes'),
'#default_value' => $defaults->settings['use_emdash'],
);
$form['replace_spaces'] = array(
'#type' => 'checkbox',
'#title' => t('Replace multiple spaces'),
'#default_value' => $defaults->settings['replace_spaces'],
);
$form['replace_quotes'] = array(
'#type' => 'checkbox',
'#title' => t('Replace quotes'),
'#default_value' => $defaults->settings['replace_quotes'],
);
$form['no_dot_spaces'] = array(
'#type' => 'checkbox',
'#title' => t('Remove spaces before quotation, aclamation marks etc.'),
'#default_value' => $defaults->settings['no_dot_spaces'],
);
return $form;
}