/[drupal]/contributions/modules/geshifilter/geshifilter.admin.inc
ViewVC logotype

Contents of /contributions/modules/geshifilter/geshifilter.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.24 - (show annotations) (download) (as text)
Mon Aug 17 21:00:16 2009 UTC (3 months, 1 week ago) by soxofaan
Branch: MAIN
CVS Tags: HEAD
Changes since 1.23: +21 -8 lines
File MIME type: text/x-php
#518828: added support for new GeSHi feature to implement line numbering with copy/paste friendly table container
1 <?php
2 // $Id: geshifilter.admin.inc,v 1.23 2009/07/12 23:24:48 soxofaan Exp $
3
4 /**
5 * @file
6 * Implementation of administration functionality.
7 */
8
9 require_once drupal_get_path('module', 'geshifilter') .'/geshifilter.inc';
10
11 define('GESHIFILTER_FLUSH_LANGUAGE_DEFINITION_CACHE_TEXT', 'Flush the GeSHi language definition cache');
12
13 /**
14 * Form (items) for filter settings.
15 */
16 function _geshifilter_filter_settings($format) {
17 $form = array();
18 $form['geshifilter'] = array(
19 '#type' => 'fieldset',
20 '#title' => t('GeSHi filter'),
21 '#collapsible' => TRUE,
22 );
23 if (geshifilter_use_format_specific_options()) {
24 // tags and attributes
25 $form['geshifilter']['general_tags'] = _geshifilter_general_highlight_tags_settings($format);
26 $form['#validate'][] = '_geshifilter_tag_styles_validate';
27 // per language tags
28 $form['geshifilter']['per_language_settings'] = array(
29 '#type' => 'fieldset',
30 '#title' => t('Per language tags'),
31 '#collapsible' => TRUE,
32 'table' => geshifilter_per_language_settings($format, 'enabled', FALSE, TRUE),
33 );
34 }
35 else {
36 $form['geshifilter']['info'] = array(
37 '#value' => '<p>'. t('GeSHi filter is configured to use global tag settings. For separate settings per input format, enable this option in the <a href="!geshi_admin_url">general GeSHi filter settings</a>.', array('!geshi_admin_url' => url('admin/settings/geshifilter'))) .'</p>'
38 );
39 }
40 $form['#validate'][] = 'geshifilter_per_language_settings_validate';
41 return $form;
42 }
43
44 /**
45 * Validation handler for the tag styles form element.
46 * Covers usage in the general form of geshifilter_admin_general_settings()
47 * and the format specific (sub)form of _geshifilter_filter_settings().
48 */
49 function _geshifilter_tag_styles_validate($form, &$form_state) {
50 // If we're coming from the _geshifilter_filter_settings (sub)form, we should
51 // take the input format into account.
52 $f = isset($form_state['values']['format']) ? '_'. $form_state['values']['format'] : '';
53
54 // Check that at least one tag style is selected.
55 if (0 == count(array_filter($form_state['values']["geshifilter_tag_styles{$f}"]))) {
56 form_set_error("geshifilter_tag_styles{$f}", t('At least one tag style should be selected.'));
57 }
58 }
59
60 /**
61 * General settings form for the GeSHi filter.
62 */
63 function geshifilter_admin_general_settings(&$form_state) {
64 $form = array();
65
66 // Try to load GeSHi library and get version if successful.
67 $geshi_library = _geshifilter_check_geshi_library();
68
69 // GeSHi library settings (constant GESHI_VERSION is defined in GeSHi library)
70 $form['geshifilter_library'] = array(
71 '#type' => 'fieldset',
72 '#title' => defined('GESHI_VERSION') ? t('GeSHi library version @version detected', array('@version' => GESHI_VERSION)) : t('GeSHi library'),
73 '#description' => t('The GeSHi filter requires the GeSHi library (which needs to be <a href="!geshi">downloaded</a> and installed seperately).',
74 array('!geshi' => url('http://qbnz.com/highlighter/'))),
75 '#collapsible' => TRUE,
76 '#collapsed' => $geshi_library['success'],
77 );
78 $form['geshifilter_library']['geshifilter_geshi_dir'] = array(
79 '#type' => 'textfield',
80 '#title' => t('Path to GeSHi library'),
81 '#default_value' => _geshifilter_get_geshi_dir(),
82 '#description' => t('Specify the path to the GeSHi library directory (which contains a file called <em>geshi.php</em>).')
83 );
84 $form['geshifilter_library']['#after_build'] = array('geshifilter_admin_general_settings_after_build');
85
86 // If the GeSHi library is loaded, show all the options and settings.
87 if ($geshi_library['loaded']) {
88
89 // Option for flushing the GeSHi language definition cache.
90 $form['geshifilter_library']['geshi_language_definition_caching'] = array(
91 '#type' => 'item',
92 '#title' => t('GeSHi language definition caching'),
93 '#description' => t('The GeSHi library uses languages definition files to define the properties and highlight rules of the supported languages. In most scenarios these language definition files do not change and a lot of derivative data, such as the list of available languages or the CSS style sheet, can be cached for efficiency reasons. Sometimes however, this cache needs to be flushed and the languages definition files need to be reparsed, for example after an upgrade of the GeSHi library or after adding/editing some language definition files manually.'),
94 );
95 // Non-submitting button for flushing the GeSHi language definition file cache.
96 $form['geshifilter_library']['geshi_language_definition_caching']['flush_geshi_language_definition_cache'] = array(
97 '#type' => 'button',
98 '#value' => t(GESHIFILTER_FLUSH_LANGUAGE_DEFINITION_CACHE_TEXT),
99 );
100 // Handle the button for flushing the GeSHi language definition cache.
101 // This is done here instead of in a submit handler because the button is
102 // not a submitting button.
103 if (isset($form_state['post']['op']) && $form_state['post']['op'] == t(GESHIFILTER_FLUSH_LANGUAGE_DEFINITION_CACHE_TEXT)) {
104 _geshifilter_flush_language_definition_cache();
105 }
106
107 // GeSHi filter tags and delimiters options
108 $form['geshifilter_tag_options'] = array(
109 '#type' => 'fieldset',
110 '#title' => t('GeSHi filter tags and delimiters'),
111 '#collapsible' => TRUE,
112 );
113 // Usage of format specific options.
114 $form['geshifilter_tag_options']['geshifilter_format_specific_options'] = array(
115 '#type' => 'checkbox',
116 '#title' => t('Use input format specific tag settings.'),
117 '#default_value' => geshifilter_use_format_specific_options(),
118 '#description' => t('Enable seperate tag settings of the GeSHi filter for each <a href="!input_formats">input format</a> instead of global tag settings.', array('!input_formats' => url('admin/settings/filters'))),
119 );
120 // Generic tags settings.
121 // @todo: do this conditional form part showing/hiding in jQuery instead of in the form builder.
122 if (!geshifilter_use_format_specific_options()) {
123 $form['geshifilter_tag_options']['geshifilter_general_tags'] = _geshifilter_general_highlight_tags_settings();
124 $form['#validate'][] = '_geshifilter_tag_styles_validate';
125 }
126
127 // GeSHi filter highlighting options
128 $form['geshifilter_highlighting_options'] = array(
129 '#type' => 'fieldset',
130 '#title' => t('Syntax highlighting options'),
131 '#collapsible' => TRUE,
132 );
133 // default language
134 $languages = _geshifilter_get_enabled_languages();
135 $form['geshifilter_highlighting_options']['geshifilter_default_highlighting'] = array(
136 '#type' => 'select',
137 '#title' => t('Default highlighting mode'),
138 '#default_value' => variable_get('geshifilter_default_highlighting', GESHIFILTER_DEFAULT_PLAINTEXT),
139 '#options' => array(
140 t('No highlighting') => array(
141 GESHIFILTER_DEFAULT_DONOTHING => t('Do nothing'),
142 GESHIFILTER_DEFAULT_PLAINTEXT => t('As plain text'),
143 ),
144 t('Languages') => $languages,
145 ),
146 '#description' => t('Select the default highlighting mode to use when no language is defined with a language attribute in the tag.')
147 );
148 // Default line numbering scheme
149 $form['geshifilter_highlighting_options']['geshifilter_default_line_numbering'] = array(
150 '#type' => 'select',
151 '#title' => t('Default line numbering'),
152 '#default_value' => variable_get('geshifilter_default_line_numbering', GESHIFILTER_LINE_NUMBERS_DEFAULT_NONE),
153 '#options' => array(
154 GESHIFILTER_LINE_NUMBERS_DEFAULT_NONE => t('no line numbers'),
155 GESHIFILTER_LINE_NUMBERS_DEFAULT_NORMAL => t('normal line numbers'),
156 GESHIFILTER_LINE_NUMBERS_DEFAULT_FANCY5 => t('fancy line numbers (every @n lines)', array('@n' => GESHIFILTER_LINE_NUMBERS_DEFAULT_FANCY5)),
157 GESHIFILTER_LINE_NUMBERS_DEFAULT_FANCY10 => t('fancy line numbers (every @n lines)', array('@n' => GESHIFILTER_LINE_NUMBERS_DEFAULT_FANCY10)),
158 GESHIFILTER_LINE_NUMBERS_DEFAULT_FANCY20 => t('fancy line numbers (every @n lines)', array('@n' => GESHIFILTER_LINE_NUMBERS_DEFAULT_FANCY20)),
159 ),
160 '#description' => t('Select the default line numbering scheme: no line numbers, normal line numbers or fancy line numbers. With fancy line numbers every n<sup>th</sup> line number is highlighted. (GeSHi documentation: <a href="!link">Line numbers</a>).',
161 array('!link' => 'http://qbnz.com/highlighter/geshi-doc.html#line-numbers')),
162 );
163 // highlight_string usage option
164 $form['geshifilter_highlighting_options']['geshifilter_use_highlight_string_for_php'] = array(
165 '#type' => 'checkbox',
166 '#title' => t('Use built-in PHP function <code>highlight_string()</code> for PHP source code.'),
167 '#description' => t('When enabled, PHP source code will be syntax highlighted with the built-in PHP function <code><a href="!highlight_string">highlight_string()</a></code> instead of with the GeSHi library. GeSHi features, like line numbering and usage of an external CSS stylesheet for example, are not available.',
168 array('!highlight_string' => 'http://php.net/manual/en/function.highlight-string.php')),
169 '#default_value' => variable_get('geshifilter_use_highlight_string_for_php', FALSE),
170 );
171 // Option to disable Keyword URL's
172 $form['geshifilter_highlighting_options']['geshifilter_enable_keyword_urls'] = array(
173 '#type' => 'checkbox',
174 '#title' => t('Enable GeSHi keyword URLs'),
175 '#description' => t('For some languages GeSHi can link language keywords (e.g. standard library functions) to their online documentation. (GeSHi documentation: <a href="!link">Keyword URLs</a>).',
176 array('!link' => 'http://qbnz.com/highlighter/geshi-doc.html#keyword-urls')),
177 '#default_value' => variable_get('geshifilter_enable_keyword_urls', TRUE),
178 );
179
180 // Styling, layout and CSS
181 $form['geshifilter_styling'] = array(
182 '#type' => 'fieldset',
183 '#title' => t('Styling, layout and CSS'),
184 '#collapsible' => TRUE,
185 );
186
187 // CSS mode
188 $form['geshifilter_styling']['geshifilter_css_mode'] = array(
189 '#type' => 'radios',
190 '#title' => t('CSS mode for syntax highlighting'),
191 '#options' => array(
192 GESHIFILTER_CSS_INLINE => t('Inline CSS style attributes.'),
193 GESHIFILTER_CSS_CLASSES_AUTOMATIC => t('Use CSS classes and an automatically managed external CSS style sheet.'),
194 GESHIFILTER_CSS_CLASSES_ONLY => t('Only add CSS classes to the markup.'),
195 ),
196 '#default_value' => variable_get('geshifilter_css_mode', GESHIFILTER_CSS_INLINE),
197 '#description' => t('Inline CSS is easier to set up, does not depend on an external style sheets and is consequently more robust to copy/paste operations like content aggregation. However, usage of CSS classes and an external stylesheet requires much less markup code and bandwidth. The external style sheet can be managed automatically by the GeSHi filter module, but this feature requires the public <a href="!filesystem">download method</a>. If the GeSHi filter is configured to only add the CSS classes to the markup, the administrator or themer is responsible for adding the appropriate CSS rules to the pages (e.g. based on <a href="!cssdefaults">these defaults</a>). (GeSHi documentation: <a href="!geshidoc">Using CSS Classes</a>).',
198 array(
199 '!geshidoc' => 'http://qbnz.com/highlighter/geshi-doc.html#using-css-classes',
200 '!filesystem' => url('admin/settings/file-system'),
201 '!cssdefaults' => url('admin/settings/geshifilter/generate_css'),
202 )),
203 );
204
205 // Code container
206 $container_options = array(
207 GESHI_HEADER_PRE => t('%val: uses a @cnt wrapper, efficient whitespace coding, no automatic line wrapping, generates invalid HTML with line numbering.',
208 array('%val' => 'GESHI_HEADER_PRE', '@cnt' => '<pre>')),
209 GESHI_HEADER_DIV => t('%val: uses a @cnt wrapper, enables automatic line wrapping.',
210 array('%val' => 'GESHI_HEADER_DIV', '@cnt' => '<div>')),
211 );
212 if (version_compare(GESHI_VERSION, '1.0.8', '>=')) {
213 $container_options[GESHI_HEADER_PRE_VALID] = t('%val: uses @pre wrappers, ensures valid HTML with line numbering, but generates more markup.',
214 array('%val' => 'GESHI_HEADER_PRE_VALID', '@pre' => '<pre>', '@li' => '<li>'));
215 $container_options[GESHI_HEADER_PRE_TABLE] = t('%val: uses a @table construction for adding line numbers which avoids selection/copy/paste problems.',
216 array('%val' => 'GESHI_HEADER_PRE_TABLE', '@table' => '<table>'));
217 }
218 if (version_compare(GESHI_VERSION, '1.0.7.2', '>=')) {
219 $container_options[GESHI_HEADER_NONE] = t('%val: uses no wrapper.',
220 array('%val' => 'GESHI_HEADER_NONE'));
221 }
222
223 $form['geshifilter_styling']['geshifilter_code_container'] = array(
224 '#type' => 'radios',
225 '#title' => t('Code container, wrapping technique'),
226 '#description' => t('Define the wrapping technique to use for code blocks. (GeSHi documentation: <a href="!link">The Code Container</a>).',
227 array('!link' => 'http://qbnz.com/highlighter/geshi-doc.html#the-code-container')
228 ),
229 '#options' => $container_options,
230 '#default_value' => variable_get('geshifilter_code_container', GESHI_HEADER_PRE),
231 );
232 }
233 // Make it a system settings form.
234 $form = system_settings_form($form);
235 // Set custom validate/submit handlers (to be run after system_settings_form submit).
236 $form['#validate'][] = 'geshifilter_admin_general_settings_validate';
237 $form['#submit'][] = 'geshifilter_admin_general_settings_submit';
238 return $form;
239 }
240
241 /**
242 * After build function for admin settings
243 */
244 function geshifilter_admin_general_settings_after_build($form, &$form_state) {
245 $geshi_library = _geshifilter_check_geshi_library(FALSE, $form_state['values']['geshifilter_geshi_dir'], FALSE);
246 if (!$geshi_library['success']) {
247 form_set_error('geshifilter_geshi_dir', $geshi_library['message']);
248 }
249 return $form;
250 }
251
252 /**
253 * Validate function for admin settings
254 */
255 function geshifilter_admin_general_settings_validate($form, &$form_state) {
256 // Check if automatically managed style sheet is posible
257 if ($form_state['values']['geshifilter_css_mode'] == GESHIFILTER_CSS_CLASSES_AUTOMATIC && !_geshifilter_managed_external_stylesheet_possible()) {
258 form_set_error('geshifilter_css_mode', t('GeSHi filter can not automatically manage an external CSS style sheet when the download method is private.'));
259 }
260 }
261
262 /**
263 * Submit function for admin settings
264 */
265 function geshifilter_admin_general_settings_submit($form, &$form_state) {
266 // Cache of available languages should be cleared when GeSHi library dir changes
267 // _geshifilter_get_geshi_dir() returns the new value set by
268 // system_settings_form_submit() which runs before
269 // geshifilter_admin_general_settings_submit(),
270 // $form['geshifilter_library']['geshifilter_geshi_dir']['#default_value'] is
271 // the old value
272 if (_geshifilter_get_geshi_dir() != $form['geshifilter_library']['geshifilter_geshi_dir']['#default_value']) {
273 variable_del('geshifilter_available_languages_cache');
274 }
275 // Regenerate language_css
276 // Note the use of variable_get('geshifilter_css_mode') which is alread set
277 // to $form_state['values']['geshifilter_css_mode'] by
278 // system_settings_form_submit, which is run before this submit handler.
279 if (variable_get('geshifilter_css_mode', GESHIFILTER_CSS_INLINE) == GESHIFILTER_CSS_CLASSES_AUTOMATIC) {
280 _geshifilter_generate_languages_css_file();
281 }
282 _geshifilter_clear_filter_cache();
283 }
284
285 /**
286 * Menu callback for per language settings
287 */
288 function geshifilter_admin_per_language_settings($form_state, $view = 'enabled') {
289 $form = array();
290 // check if GeSHi library is available
291 $geshi_library = _geshifilter_check_geshi_library();
292 if (!$geshi_library['success']) {
293 drupal_set_message($geshi_library['message'], 'error');
294 return;
295 }
296 $add_checkbox = TRUE;
297 $add_tag_option = (!geshifilter_use_format_specific_options());
298 $form['language_settings'] = geshifilter_per_language_settings(NULL, $view, $add_checkbox, $add_tag_option);
299
300 // Make it a system settings form.
301 $form = system_settings_form($form);
302
303 // set custom validate and submit handlers
304 $form['#validate'][] = 'geshifilter_per_language_settings_validate';
305 $form['#submit'][] = 'geshifilter_admin_per_language_settings_submit';
306
307 return $form;
308 }
309
310 /**
311 * Validate function for per language settings
312 *
313 * Check that language tags differ between languages and fro the generic tags.
314 */
315 function geshifilter_per_language_settings_validate($form, &$form_state) {
316
317 // if we're coming from the _geshifilter_filter_settings (sub)form, we should
318 // take the input format into account
319 $format = isset($form_state['values']['format']) ? $form_state['values']['format'] : NULL;
320 $f = ($format === NULL) ? '' : "_$format";
321
322 // language tags should differ from each other
323 $languages = _geshifilter_get_available_languages();
324 foreach ($languages as $language1 => $language_data1) {
325 // iterate over the enabled languages: skip disabled ones
326 $field = "geshifilter_language_enabled_{$language1}";
327 if (!(isset($form_state['values'][$field]) ? $form_state['values'][$field] : variable_get($field, FALSE))) {
328 continue;
329 }
330 // get the associated tags as $tags1
331 $field = "geshifilter_language_tags_{$language1}{$f}";
332 if (isset($form_state['values'][$field])) {
333 $tags1 = _geshifilter_tag_split($form_state['values'][$field]);
334 }
335 else {
336 $tags1 = _geshifilter_tag_split(geshifilter_language_tags($language1, $format));
337 }
338 // also include the generic tags
339 $field = "geshifilter_tags{$f}";
340 $generic_tags = isset($form_state['values'][$field]) ? $form_state['values'][$field] : geshifilter_tags($format);
341 $tags1 = array_merge($tags1, _geshifilter_tag_split($generic_tags));
342
343 // check that other languages do not use these tags
344 foreach ($languages as $language2 => $language_data2) {
345 // check these tags against the tags of other enabled languages
346 $field = "geshifilter_language_enabled_{$language2}";
347 if ($language1 == $language2 || !(isset($form_state['values'][$field]) ? $form_state['values'][$field] : variable_get($field, FALSE))) {
348 continue;
349 }
350 // get tags for $language2, or skip when not in $form_state['values']
351 $field = "geshifilter_language_tags_{$language2}{$f}";
352 if (isset($form_state['values'][$field])) {
353 $tags2 = _geshifilter_tag_split($form_state['values'][$field]);
354 }
355 else {
356 continue;
357 }
358 // and now we can check tags1 against tags2
359 foreach ($tags1 as $tag1) {
360 foreach ($tags2 as $tag2) {
361 if ($tag1 == $tag2) {
362 form_set_error("geshifilter_language_tags_{$language2}{$f}", t('The language tags should differ between languages and from the generic tags.'));
363 }
364 }
365 }
366 }
367 }
368 }
369
370 /**
371 * Submit function for per language settings
372 */
373 function geshifilter_admin_per_language_settings_submit($form, &$form_state) {
374 // regenerate language_css
375 if (variable_get('geshifilter_css_mode', GESHIFILTER_CSS_INLINE) == GESHIFILTER_CSS_CLASSES_AUTOMATIC) {
376 _geshifilter_generate_languages_css_file();
377 }
378 _geshifilter_clear_filter_cache();
379 }
380
381 /**
382 * Helper function for some settings form fields usable as general and specific settings
383 */
384 function _geshifilter_general_highlight_tags_settings($format=NULL) {
385 $form = array();
386 $f = ($format === NULL) ? '' : "_$format";
387 // generic tags
388 $form["geshifilter_tags$f"] = array(
389 '#type' => 'textfield',
390 '#title' => t('Generic syntax highlighting tags'),
391 '#default_value' => geshifilter_tags($format),
392 '#description' => t('Tags that should activate the GeSHi syntax highlighting. Specify a space-separated list of tagnames.'),
393 );
394 // Container tag styles.
395 $form["geshifilter_tag_styles$f"] = array(
396 '#type' => 'checkboxes',
397 '#title' => t('Container tag style'),
398 '#options' => array(
399 GESHIFILTER_BRACKETS_ANGLE => '<code>'. check_plain('<foo> ... </foo>') .'</code>',
400 GESHIFILTER_BRACKETS_SQUARE => '<code>'. check_plain('[foo] ... [/foo]') .'</code>',
401 GESHIFILTER_BRACKETS_DOUBLESQUARE => '<code>'. check_plain('[[foo]] ... [[/foo]]') .'</code>',
402 GESHIFILTER_BRACKETS_PHPBLOCK => t('PHP style source code blocks: !php and !percent', array(
403 '!php' => '<code>'. check_plain('<?php ... ?>'). '</code>',
404 '!percent' => '<code>'. check_plain('<% ... %>'). '</code>',
405 )),
406 ),
407 '#default_value' => _geshifilter_tag_styles($format),
408 '#description' => t('Select the container tag styles that should trigger GeSHi syntax highlighting.'),
409 );
410 return $form;
411 }
412
413 /**
414 * function for generating a form table for per language settings
415 */
416 function geshifilter_per_language_settings($format, $view, $add_checkbox, $add_tag_option) {
417 $form = array(
418 '#theme' => 'geshifilter_per_language_settings',
419 );
420 // table header
421 $form['header'] = array(
422 '#type' => 'value',
423 '#value' => array(),
424 );
425 $form['header']['#value'][] = t('Language');
426 $form['header']['#value'][] = t('GeSHi language code');
427 if ($add_tag_option) {
428 $form['header']['#value'][] = t('Tag/language attribute value');
429 }
430 // table body
431 $form['languages'] = array();
432 $languages = _geshifilter_get_available_languages();
433 foreach ($languages as $language => $language_data) {
434 $enabled = variable_get("geshifilter_language_enabled_{$language}", FALSE);
435 // skip items to hide
436 if (($view == 'enabled' && !$enabled) || ($view == 'disabled' && $enabled)) {
437 continue;
438 }
439 // build language row
440 $form['languages'][$language] = array();
441 // add enable/disable checkbox
442 if ($add_checkbox) {
443 $form['languages'][$language]["geshifilter_language_enabled_{$language}"] = array(
444 '#type' => 'checkbox',
445 '#default_value' => $enabled,
446 '#title' => $language_data['fullname'],
447 );
448 }
449 else {
450 $form['languages'][$language]['fullname'] = array(
451 '#type' => 'markup',
452 '#value' => $language_data['fullname'],
453 );
454 }
455 // language code
456 $form['languages'][$language]['name'] = array(
457 '#type' => 'markup',
458 '#value' => $language,
459 );
460 // add a textfield for tags
461 if ($add_tag_option) {
462 $f = ($format === NULL) ? '' : "_$format";
463 $form['languages'][$language]["geshifilter_language_tags_{$language}{$f}"] = array(
464 '#type' => 'textfield',
465 '#default_value' => geshifilter_language_tags($language, $format),
466 '#size' => 20,
467 );
468 }
469 }
470 return $form;
471 }
472
473 /**
474 * theming function for the per language settings table
475 */
476 function theme_geshifilter_per_language_settings($form) {
477 // build table body
478 $rows = array();
479 foreach (element_children($form['languages']) as $language) {
480 $row = array();
481 foreach (element_children($form['languages'][$language]) as $key) {
482 $row[] = drupal_render($form['languages'][$language][$key]);
483 }
484 $rows[] = $row;
485 }
486 // build table header
487 $header = array();
488 foreach ($form['header']['#value'] as $cell) {
489 if ($cell == t('Enabled')) {
490 // add JavaScript 'select all/select none' stuff
491 $header[] = array('data' => $cell) + theme('table_select_header_cell');
492 }
493 else {
494 $header[] = $cell;
495 }
496 }
497 // return themed table
498 return theme('table', $header, $rows);
499 }
500
501 /**
502 * Helper function for flushing the GeSHi language definition cache
503 */
504 function _geshifilter_flush_language_definition_cache() {
505 if (GESHIFILTER_CSS_CLASSES_AUTOMATIC == variable_get('geshifilter_css_mode', GESHIFILTER_CSS_INLINE)) {
506 // Forced regeneration of the CSS file
507 _geshifilter_generate_languages_css_file(TRUE);
508 }
509 // Flush cache of available languages
510 variable_del('geshifilter_available_languages_cache');
511 drupal_set_message(t('Flushed the GeSHi language definition cache.'));
512 }
513
514 /**
515 * Helper function for clearing the appropriate filter cache entries
516 */
517 function _geshifilter_clear_filter_cache() {
518 $message = FALSE;
519 // iterate over input formats
520 foreach (filter_formats() as $format => $input_format) {
521 // Get the filters in this input format
522 $filters = filter_list_format($format);
523 // look if GeSHi is enabled in this input format
524 if (isset($filters['geshifilter/0'])) {
525 cache_clear_all($format .':', 'cache_filter', TRUE);
526 $message = TRUE;
527 }
528 }
529 // show a message that the filter cache was cleared
530 if ($message) {
531 drupal_set_message(t('GeSHi filter cleared the filter cache for the appropriate input formats.'));
532 }
533 }
534
535 /**
536 * Helper function for generating the CSS rules
537 *
538 * @return string with the CSS rules
539 */
540 function _geshifilter_generate_languages_css_rules() {
541 $output = '';
542 $geshi_library = _geshifilter_check_geshi_library();
543 if ($geshi_library['loaded']) {
544 require_once drupal_get_path('module', 'geshifilter') .'/geshifilter.pages.inc';
545 $languages = _geshifilter_get_enabled_languages();
546 foreach ($languages as $langcode => $language_full_name) {
547 // create GeSHi object
548 $geshi = _geshifilter_GeSHi_factory('', $langcode);
549 _geshifilter_override_geshi_defaults($geshi, $langcode);
550 // add CSS rules for current language
551 $output .= $geshi->get_stylesheet(FALSE) ."\n";
552 // release GeSHi object
553 unset($geshi);
554 }
555 }
556 else {
557 drupal_set_message(t('Error while generating CSS rules: could not load GeSHi library.'), 'error');
558 }
559 return $output;
560 }
561
562 /**
563 * Function for generating the external stylesheet.
564 *
565 * @param $force force the regeneration of the CSS file
566 */
567 function _geshifilter_generate_languages_css_file($force=FALSE) {
568 $languages = _geshifilter_get_enabled_languages();
569 // Serialize the array of enabled languages as sort of hash.
570 $languages_hash = serialize($languages);
571
572 // Check if generation of the CSS file is needed
573 if ($force || $languages_hash != variable_get('geshifilter_cssfile_languages', '')) {
574 // Build stylesheet.
575 $stylesheet = _geshifilter_generate_languages_css_rules();
576 // Save stylesheet.
577 $stylesheet_filename = file_directory_path() .'/geshifilter-languages.css';
578 $ret = file_save_data($stylesheet, $stylesheet_filename, FILE_EXISTS_REPLACE);
579 if ($ret) {
580 drupal_set_message(t('(Re)generated external CSS style sheet %file.', array('%file' => $ret)));
581 }
582 else {
583 drupal_set_message(t('Could not generate external CSS file. Check the settings of your <a href="!filesystem">file system</a>.',
584 array('!filesystem' => url('admin/settings/file-system'))), 'error');
585 }
586 // Remember for which list of languages the CSS file was generated
587 variable_set('geshifilter_cssfile_languages', $languages_hash);
588 }
589 }

  ViewVC Help
Powered by ViewVC 1.1.2