forked from backdrop-contrib/prism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prism.module
executable file
·317 lines (280 loc) · 7.86 KB
/
prism.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/**
* @file
* Prism module file.
*/
/**
* Implements hook_library().
*
*/
function prism_library_info() {
$location = prism_return_library_location();
$csstweak = backdrop_get_path('module', 'prism') . '/css/';
$libraries['prism'] = array(
'title' => 'Prism',
'website' => 'http://prismjs.com',
'version' => array(),
'js' => array(
$location . 'prism.js' => array(),
backdrop_get_path('module', 'prism') . '/js/prism.settings.js' => array(),
),
'css' => array(
$location . 'prism.css' => array(
'type' => 'file',
'media' => 'screen',
),
$csstweak . 'csstweak.css' => array(
'type' => 'file',
'media' => 'screen',
),
),
);
return $libraries;
}
/**
* Return appropriate library location.
*/
function prism_return_library_location() {
$file_base = '/libraries/prism/prism.js';
// Check for a multisite installation.
if (file_exists(conf_path() . $file_base)) {
$location = conf_path() . '/libraries/prism/';
}
else {
// Default.
$location = 'sites/all/libraries/prism/';
}
return $location;
}
/**
* Implements hook_init()
*/
function prism_init() {
backdrop_add_library('prism', 'prism');
}
/**
* Implements hook_filter_info().
*/
function prism_filter_info() {
$filters['filter_prism'] = array(
'title' => t('Prism (syntax highlight)'),
'process callback' => '_prism_filter_info',
'tips callback' => '_prism_filter_info_config',
'weight' => 10,
);
return $filters;
}
/**
* Private callback implemented by prism_filter_info().
*
* @format
* [prism:type]
* [/prism:type]~?
*/
function _prism_filter_info($text, $format) {
$tags = array();
if (preg_match_all('/\[prism:([^\|\\]]+)\|?([^\\]]*)?\]/i', $text, $tag_match)) {
$tags = $tag_match[1];
}
if ($tags) {
foreach (array_unique($tags) as $tag) {
// Ahhh.
if (preg_match_all('#((?<!\[)\[)(prism:' . $tag . ')((\s+[^\]]*)*)(\])(.*?)((?<!\[)\[/\2\s*\]|$)#s', $text, $match)) {
foreach ($match[6] as $value) {
$replace[] = '<div class="prism-wrapper" rel="' . $tag . '"><pre><code class="language-' . $tag . '">' . htmlspecialchars($value) . '</code></pre></div>';
}
foreach ($match[0] as $value) {
$search[] = $value;
}
}
}
return str_replace($search, $replace, $text);
}
return $text;
}
/**
* Private callback implemented by prism_filter_info().
*/
function _prism_filter_info_config($format, $long = FALSE) {
return 'Format: [prism:language] example; [prism:css] code here [/prism:css].';
}
/**
* Implementats hook_field_info().
*/
function prism_field_info() {
return array(
'prism' => array(
'label' => t('Prism textarea'),
'description' => t('This field stores long text in the database.'),
'default_widget' => 'prism_textarea',
'default_formatter' => 'prism_default',
),
);
}
/**
* Implements hook_widget_info().
*/
function prism_field_widget_info() {
return array(
'prism_textarea' => array(
'label' => t('Code highlighting'),
'field types' => array('prism'),
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
);
}
/**
* Implements hook_field_widget_form().
*/
function prism_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$element += array(
'#type' => $instance['widget']['type'],
'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
);
return $element;
}
/**
* Implements hook_field_formatter_settings_form().
*/
function prism_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
if ($display['type'] == 'prism_trim') {
$element['trim_size'] = array(
'#type' => 'textfield',
'#title' => t('Trim Size'),
'#description' => t('The language trim size.'),
'#default_value' => $settings['trim_size'],
);
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function prism_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$elements = array();
if ($display['type'] == 'prism_trim') {
$elements = t('The current trim size is @trim', array(
'@trim' => $settings['trim_size'],
));
}
return $elements;
}
/**
* Implements hook_elements_info().
*/
function prism_element_info() {
$elements = array(
'prism_textarea' => array(
'#input' => TRUE,
'#process' => array('prism_textarea_process'),
),
);
return $elements;
}
/**
* Process callback prism_textarea widget.
*/
function prism_textarea_process($element, $form_state, $complete_form) {
$defaults = $element['#value'];
$field = field_widget_instance($element, $form_state);
// @todo, Allow admin to set languages.
$languages = array(
'http' => 'HTTP',
'groovy' => 'Groovy',
'sql' => 'SQL',
'python' => 'Python',
'cplusplus' => 'C++',
'c' => 'C',
'bash' => 'Bash',
'sass' => 'SASS',
'coffeescript' => 'CoffeeScript',
'php' => 'PHP',
'java' => 'Java',
'javascript' => 'JavaScript',
'css' => 'CSS',
'markup' => 'Markup',
);
$element['codearea'] = array(
'#type' => 'textarea',
'#title' => t('Code area'),
'#default_value' => isset($defaults['codearea']) ? $defaults['codearea'] : '',
'#required' => $element['#required'],
'#rows' => isset($field['widget']['rows']) ? $field['widget']['rows'] : 5,
'#description' => filter_xss($field['description']),
);
$element['languages'] = array(
'#type' => 'select',
'#title' => t('Language'),
'#default_value' => isset($defaults['languages']) ? $defaults['languages'] : '',
'#options' => $languages,
'#description' => t('Select the language to highlight.'),
);
return $element;
}
/**
* Implements hook_field_formatter_info().
*/
function prism_field_formatter_info() {
return array(
'prism_default' => array(
'label' => t('Default'),
'field types' => array('prism'),
),
/*'prism_trim' => array(
'label' => t('Trim'),
'field types' => array('prism'),
'settings' => array(
'trim_size' => 600,
),
),*/
);
}
/**
* Implements hook_field_formatter_view().
*/
function prism_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
switch ($display['type']) {
/*case 'prism_trim':
foreach ($items as $delta => $item) {
$elements[$delta] = array(
'#markup' => '<div id="prism-' . $delta . '" class="prism-wrapper" rel="' . $item['languages'] . '"><pre><code class="language-' . $item['languages'] . '">' . truncate_utf8($item['codearea'], 600, TRUE, TRUE) . '</code></pre></div>',
);
}
break;*/
case 'prism_default':
foreach ($items as $delta => $item) {
$elements[$delta] = array(
'#markup' => '<div id="prism-' . $delta . '" class="prism-wrapper" rel="' . $item['languages'] . '"><pre><code class="language-' . $item['languages'] . '">' . htmlspecialchars($item['codearea']) . '</code></pre></div>',
);
}
break;
}
return $elements;
}
/**
* Implements hook_field_is_empty().
*/
function prism_field_is_empty($item, $field) {
return empty($item['codearea']);
}
/**
* Implements hook_wysiwyg_editor_settings_alter().
*
* @description
* Prevents CKEditor from encoding html entities, such as ampersands.
* @https://drupal.org/node/803562#comment-5630546
*/
function prism_wysiwyg_editor_settings_alter(&$settings, $context) {
if ($context['profile']->editor == 'ckeditor') {
$settings['entities'] = FALSE;
$settings['basicEntities'] = FALSE;
$settings['disableReadonlyStyling'] = TRUE;
}
}