/[drupal]/contributions/modules/geshifilter/geshifilter.module
ViewVC logotype

Contents of /contributions/modules/geshifilter/geshifilter.module

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


Revision 1.48 - (show annotations) (download) (as text)
Sun Jul 12 15:00:28 2009 UTC (4 months, 1 week ago) by soxofaan
Branch: MAIN
CVS Tags: HEAD
Changes since 1.47: +3 -3 lines
File MIME type: text/x-php
various i18 fixes and typos (part of #351185 by hass)
1 <?php
2 // $Id: geshifilter.module,v 1.47 2009/07/04 11:05:56 soxofaan Exp $
3
4 /**
5 * @file
6 * An input filter for syntax highlighting using the GeSHi library.
7 */
8
9 define('GESHIFILTER_DEFAULT_PLAINTEXT', 'GESHIFILTER_DEFAULT_PLAINTEXT');
10 define('GESHIFILTER_DEFAULT_DONOTHING', 'GESHIFILTER_DEFAULT_DONOTHING');
11
12 // GeSHi CSS modes
13 // Inline CSS
14 define('GESHIFILTER_CSS_INLINE', 1);
15 // Usage of CSS classes and an automatically managaged external stylesheet
16 define('GESHIFILTER_CSS_CLASSES_AUTOMATIC', 2);
17 // Only add CSS classes to markup, admin/themer is responsible for defining the CSS rules
18 define('GESHIFILTER_CSS_CLASSES_ONLY', 3);
19
20 define('GESHIFILTER_ATTRIBUTES_LANGUAGE', 'type lang language class');
21 define('GESHIFILTER_ATTRIBUTE_LINE_NUMBERING', 'linenumbers');
22 define('GESHIFILTER_ATTRIBUTE_LINE_NUMBERING_START', 'start');
23 define('GESHIFILTER_ATTRIBUTE_FANCY_N', 'fancy');
24
25 define('GESHIFILTER_BRACKETS_ANGLE', 1);
26 define('GESHIFILTER_BRACKETS_SQUARE', 2);
27 define('GESHIFILTER_BRACKETS_BOTH', 3); // Deprecated, only used in upgrade path.
28 define('GESHIFILTER_BRACKETS_DOUBLESQUARE', 4);
29 define('GESHIFILTER_BRACKETS_PHPBLOCK', 8);
30
31
32 define('GESHIFILTER_LINE_NUMBERS_DEFAULT_NONE', 0);
33 define('GESHIFILTER_LINE_NUMBERS_DEFAULT_NORMAL', 1);
34 define('GESHIFILTER_LINE_NUMBERS_DEFAULT_FANCY5', 5);
35 define('GESHIFILTER_LINE_NUMBERS_DEFAULT_FANCY10', 10);
36 define('GESHIFILTER_LINE_NUMBERS_DEFAULT_FANCY20', 20);
37
38 /**
39 * Implementation of hook_help().
40 */
41 function geshifilter_help($path, $arg) {
42 switch ($path) {
43 case 'admin/settings/geshifilter':
44 case 'admin/help#geshifilter':
45 $output = '<p>'. t('The GeSHi filter module provides a filter for syntax highlighting of inline source code or blocks of source code based on the PHP library !GeSHi.', array('!GeSHi' => l('GeSHi (Generic Syntax Highlighter)', 'http://qbnz.com/highlighter/'))) .'</p>';
46 if ($path == 'admin/help#geshifilter') {
47 $output .= '<p>'. t('The GeSHi filter module for Drupal requires the GeSHi library (version 1.0.x) to work. The GeSHi filter is actually just a Drupal wrapper module around the GeSHi library. Because of <a href="!repositorypolicies">drupal.org repository policies</a> however, the GeSHi library is not included in the GeSHi filter package, so you should <a href="!geshi">download</a> and install the GeSHi library separately.', array(
48 '!repositorypolicies' => url('http://drupal.org/node/66113'),
49 '!geshi' => url('http://qbnz.com/highlighter/'),
50 )) .'</p>';
51 $output .= t('<p>Quick overview of how to set up and use the GeSHi filter:</p><ul><li>Install the GeSHi library and specify its path on the <a href="!geshifilter_settings">GeSHi filter administration page</a>.</li><li>Configure the <a href="!geshifilter_settings">general GeSHi filter settings</a>.</li><li><a href="!geshifilter_languages">Enable the relevant languages</a> for your site and set their language tags if needed.</li><li>Enable the GeSHi filter in the desired !inputformats.</li><li>Check for !filterconflicts and resolve them.</li><li>Use the input format during content submission as described in the !filtertips.</li></ul>', array(
52 '!geshifilter_settings' => url('admin/settings/geshifilter'),
53 '!geshifilter_languages' => url('admin/settings/geshifilter/languages/all'),
54 '!inputformats' => l(t('input formats'), 'admin/settings/filters'),
55 '!filterconflicts' => l(t('filter conflicts'), 'admin/settings/geshifilter/filterconflicts'),
56 '!filtertips' => l(t('filter tips'), 'filter/tips'),
57 ));
58 }
59 return $output;
60 break;
61 case 'admin/settings/geshifilter/languages':
62 case 'admin/settings/geshifilter/languages/enabled':
63 case 'admin/settings/geshifilter/languages/all':
64 case 'admin/settings/geshifilter/languages/disabled':
65 $output = '<p>'. t('Here you can enable/disable the desired languages to use. It is suggested to disable languages that are not relevant for you site not only to avoid unnecessary cluttering of the GeSHi filter configuration pages and the !filtertips, but also to make the GeSHi filter processing lighter.', array('!filtertips' => l(t('filter tips'), 'filter/tips'))) .'</p>';
66 if (!geshifilter_use_format_specific_options()) {
67 $output .= '<p>'. t('You can also define the language specific tags here.') .'</p>';
68 }
69 return $output;
70 break;
71 }
72 }
73
74 /**
75 * Implementation of hook_menu().
76 */
77 function geshifilter_menu() {
78 $items = array();
79 $items['admin/settings/geshifilter'] = array(
80 'title' => 'GeSHi Filter',
81 'description' => 'Configure the GeSHi filter.',
82 'file' => 'geshifilter.admin.inc',
83 'page callback' => 'drupal_get_form',
84 'page arguments' => array('geshifilter_admin_general_settings'),
85 'access arguments' => array('administer site configuration'),
86 'type' => MENU_NORMAL_ITEM,
87 );
88 $items['admin/settings/geshifilter/general'] = array(
89 'title' => 'General settings',
90 'description' => 'General GeSHi filter settings.',
91 'access arguments' => array('administer site configuration'),
92 'type' => MENU_DEFAULT_LOCAL_TASK,
93 );
94 $items['admin/settings/geshifilter/filterconflicts'] = array(
95 'title' => 'Filter conflicts',
96 'description' => 'Information on possible conflicts with other filters.',
97 'file' => 'geshifilter.conflicts.inc',
98 'page callback' => 'geshifilter_admin_filter_conflicts',
99 'access arguments' => array('administer site configuration'),
100 'type' => MENU_LOCAL_TASK,
101 'weight' => 10,
102 );
103 // language settings
104 $items['admin/settings/geshifilter/languages'] = array(
105 'title' => 'Languages',
106 'description' => 'Enable the desired languages and configure their settings.',
107 'file' => 'geshifilter.admin.inc',
108 'page callback' => 'drupal_get_form',
109 'page arguments' => array('geshifilter_admin_per_language_settings'),
110 'access arguments' => array('administer site configuration'),
111 'type' => MENU_LOCAL_TASK,
112 );
113 $items['admin/settings/geshifilter/languages/enabled'] = array(
114 'title' => 'Enabled',
115 'description' => 'Show the enabled languages',
116 'access arguments' => array('administer site configuration'),
117 'type' => MENU_DEFAULT_LOCAL_TASK,
118 'weight' => 3,
119 );
120 $items['admin/settings/geshifilter/languages/all'] = array(
121 'title' => 'All',
122 'description' => 'Show all the available languages',
123 'page arguments' => array('geshifilter_admin_per_language_settings', 4),
124 'access arguments' => array('administer site configuration'),
125 'type' => MENU_LOCAL_TASK,
126 'weight' => 1,
127 );
128 $items['admin/settings/geshifilter/languages/disabled'] = array(
129 'title' => 'Disabled',
130 'description' => 'Show the disabled languages',
131 'page arguments' => array('geshifilter_admin_per_language_settings', 4),
132 'access arguments' => array('administer site configuration'),
133 'type' => MENU_LOCAL_TASK,
134 'weight' => 6,
135 );
136 // Callback for generating CSS rules.
137 $items['admin/settings/geshifilter/generate_css'] = array(
138 'page callback' => 'geshifilter_generate_language_css_rules',
139 'access arguments' => array('administer site configuration'),
140 'type' => MENU_CALLBACK,
141 );
142
143 return $items;
144 }
145
146
147 /**
148 * Implementation of hook_init().
149 */
150 function geshifilter_init() {
151 // Since the filtered content is cached, it is not possible to know on which
152 // pages the css file is actually needed. Thus it is included on all pages.
153 if (variable_get('geshifilter_css_mode', GESHIFILTER_CSS_INLINE) == GESHIFILTER_CSS_CLASSES_AUTOMATIC) {
154 drupal_add_css(file_directory_path() .'/geshifilter-languages.css');
155 }
156 drupal_add_css(drupal_get_path('module', 'geshifilter') .'/geshifilter.css');
157 }
158
159 /**
160 * Implementation of hook_filter_tips().
161 */
162 function geshifilter_filter_tips($delta, $format, $long = FALSE) {
163 require_once drupal_get_path('module', 'geshifilter') .'/geshifilter.filtertips.inc';
164 return _geshifilter_filter_tips($delta, $format, $long);
165 }
166
167 /**
168 * Implementation of hook_filter().
169 */
170 function geshifilter_filter($op, $delta = 0, $format = -1, $text = '') {
171 switch ($op) {
172 case 'list':
173 return array('GeSHi filter');
174
175 case 'description':
176 return t('Enables syntax highlighting of inline/block source code using the GeSHi engine');
177
178 case 'prepare':
179 require_once drupal_get_path('module', 'geshifilter') .'/geshifilter.pages.inc';
180 return _geshifilter_prepare($format, $text);
181
182 case 'process':
183 require_once drupal_get_path('module', 'geshifilter') .'/geshifilter.pages.inc';
184 return _geshifilter_process($format, $text);
185
186 case 'settings':
187 require_once drupal_get_path('module', 'geshifilter') .'/geshifilter.admin.inc';
188 return _geshifilter_filter_settings($format);
189
190 case 'no cache':
191 return FALSE;
192
193 default:
194 return $text;
195 }
196 }
197
198 /**
199 * Implementation of hook_theme().
200 */
201 function geshifilter_theme() {
202 return array(
203 'geshifilter_per_language_settings' => array(
204 'arguments' => array('form' => NULL)
205 )
206 );
207 }
208
209 /**
210 * Implementation of hook_requirements().
211 */
212 function geshifilter_requirements($phase) {
213 $requirements = array();
214 if ($phase == 'runtime') {
215 require_once drupal_get_path('module', 'geshifilter') .'/geshifilter.inc';
216 // check if GeSHi library is available
217 $geshi_library = _geshifilter_check_geshi_library();
218 if (!$geshi_library['loaded']) {
219 $requirements[] = array(
220 'title' => 'GeSHi filter',
221 'value' => t('GeSHi library not found.'),
222 'description' => t('You should install the GeSHi library and set its path in the <a href="!geshisettings">GeSHi settings</a>.',
223 array('!geshisettings' => url('admin/settings/geshifilter'))),
224 'severity' => REQUIREMENT_ERROR,
225 );
226 }
227 else {
228 $requirements[] = array(
229 'title' => 'GeSHi filter',
230 'value' => t('Found GeSHi library version %version',
231 array('%version' => GESHI_VERSION)), // GESHI_VERSION is defined in GeSHi library
232 'severity' => REQUIREMENT_OK,
233 );
234 }
235
236 // Warn if GeSHi filter is configured to automatically managed external stylesheet when it's not possible
237 if (variable_get('geshifilter_css_mode', GESHIFILTER_CSS_INLINE) == GESHIFILTER_CSS_CLASSES_AUTOMATIC && !_geshifilter_managed_external_stylesheet_possible()) {
238 $requirements[] = array(
239 'title' => 'GeSHi filter CSS mode',
240 'value' => t('GeSHi filter can not automatically manage an external style sheet when the download method is private.'),
241 'severity' => REQUIREMENT_ERROR,
242 'description' => t('Change the CSS mode of the <a href="!geshi">GeSHi filter</a> or change the <a href="!filesystem">download mode</a> to public.',
243 array('!geshi' => url('admin/settings/geshifilter'), '!filesystem' => url('admin/settings/file-system'))),
244 );
245 }
246
247 // check for filter conflicts
248 require_once drupal_get_path('module', 'geshifilter') .'/geshifilter.conflicts.inc';
249 if (geshifilter_admin_filter_conflicts(TRUE) > 0) {
250 $requirements[] = array(
251 'title' => 'GeSHi filter',
252 'value' => t('Some filter conflicts were detected.'),
253 'description' => l(t('View and resolve the detected filter conflicts'), 'admin/settings/geshifilter/filterconflicts'),
254 'severity' => REQUIREMENT_ERROR,
255 );
256 }
257 }
258 return $requirements;
259 }
260
261
262 /**
263 * Helper function for checking if an automatically managed style sheet is possible
264 *
265 * @return boolean indicating if an automatically managed style sheet is possible
266 */
267 function _geshifilter_managed_external_stylesheet_possible() {
268 $directory = file_directory_path();
269 return is_dir($directory) && is_writable($directory) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC);
270 }
271
272 /**
273 * Callback function for generating the CSS rules for the syntax highlighting
274 */
275 function geshifilter_generate_language_css_rules() {
276 require_once drupal_get_path('module', 'geshifilter') .'/geshifilter.admin.inc';
277 drupal_set_header("Content-type: text/css");
278 $output = _geshifilter_generate_languages_css_rules();
279 print($output);
280 exit();
281 }

  ViewVC Help
Powered by ViewVC 1.1.2