| 1 |
<?php
|
| 2 |
// $Id: geshifilter.inc,v 1.7 2009/07/04 11:05:56 soxofaan Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* General GeSHi filter helper functions.
|
| 7 |
*/
|
| 8 |
|
| 9 |
function _geshifilter_get_geshi_dir() {
|
| 10 |
return variable_get('geshifilter_geshi_dir', drupal_get_path('module', 'geshifilter') .'/geshi');
|
| 11 |
}
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Helper function for loading/checking the GeSHi library v 1.0.x (if not already)
|
| 15 |
* Returns an array with keys 'success', 'loaded' and 'message'
|
| 16 |
*/
|
| 17 |
function _geshifilter_check_geshi_library($use_cache=TRUE, $geshi_dir=NULL, $load_when_found=TRUE) {
|
| 18 |
static $geshi_library_cache = NULL;
|
| 19 |
if ($use_cache && $geshi_library_cache !== NULL) {
|
| 20 |
// get from cache
|
| 21 |
$geshi_library = $geshi_library_cache;
|
| 22 |
}
|
| 23 |
else {
|
| 24 |
// initialisation
|
| 25 |
$geshi_library = array('success' => NULL, 'loaded' => FALSE, 'message' => NULL);
|
| 26 |
// no cache
|
| 27 |
if (!$geshi_dir) {
|
| 28 |
$geshi_dir = _geshifilter_get_geshi_dir();
|
| 29 |
}
|
| 30 |
if (!is_dir($geshi_dir)) {
|
| 31 |
$geshi_library['success'] = FALSE;
|
| 32 |
$geshi_library['message'] = t('GeSHi library error: %dir is not a directory.', array('%dir' => $geshi_dir));
|
| 33 |
}
|
| 34 |
elseif (is_file($geshi_dir .'/geshi.php')) {
|
| 35 |
// GeSHi 1.0.x found (probably, we can only be sure by loading it)
|
| 36 |
$geshi_library['success'] = TRUE;
|
| 37 |
if ($load_when_found) {
|
| 38 |
require_once($geshi_dir .'/geshi.php');
|
| 39 |
// check version
|
| 40 |
$geshi_library_version = explode('.', GESHI_VERSION);
|
| 41 |
if (!($geshi_library_version[0] == '1' && $geshi_library_version[1] == '0')) {
|
| 42 |
$geshi_library['success'] = FALSE;
|
| 43 |
$geshi_library['loaded'] = FALSE;
|
| 44 |
$geshi_library['message'] = t('GeSHi library error: The detected version of GeSHi library (%version) is not supported. A version from the 1.0.x branch is required.', array('%version' => GESHI_VERSION));
|
| 45 |
}
|
| 46 |
else {
|
| 47 |
$geshi_library['loaded'] = TRUE;
|
| 48 |
}
|
| 49 |
}
|
| 50 |
}
|
| 51 |
else {
|
| 52 |
$geshi_library['success'] = FALSE;
|
| 53 |
$geshi_library['message'] = t('GeSHi library error: Could not find a known version of the GeSHi library in directory %dir.' , array('%dir' => $geshi_dir));
|
| 54 |
}
|
| 55 |
// store in cache if needed
|
| 56 |
if ($use_cache) {
|
| 57 |
$geshi_library_cache = $geshi_library;
|
| 58 |
}
|
| 59 |
}
|
| 60 |
return $geshi_library;
|
| 61 |
}
|
| 62 |
|
| 63 |
/**
|
| 64 |
* List of available languages.
|
| 65 |
* @return an array mapping language code to array with the language path and full language name
|
| 66 |
*/
|
| 67 |
function _geshifilter_get_available_languages() {
|
| 68 |
// try to get it from cache (database actually)
|
| 69 |
$available_languages = variable_get('geshifilter_available_languages_cache', NULL);
|
| 70 |
if ($available_languages === NULL) {
|
| 71 |
// not in cache: build the array of available_languages
|
| 72 |
$geshi_library = _geshifilter_check_geshi_library();
|
| 73 |
$available_languages = array();
|
| 74 |
if ($geshi_library['success']) {
|
| 75 |
$dirs = array(_geshifilter_get_geshi_dir() .'/geshi', drupal_get_path('module', 'geshifilter') .'/geshi-extra');
|
| 76 |
foreach ($dirs as $dir) {
|
| 77 |
foreach (file_scan_directory($dir, '\.[pP][hH][pP]$') as $filename => $fileinfo) {
|
| 78 |
// short name
|
| 79 |
$name = $fileinfo->name;
|
| 80 |
// get full name
|
| 81 |
$geshi = new GeSHi('', $name);
|
| 82 |
$geshi->set_language_path($dir);
|
| 83 |
$fullname = $geshi->get_language_name();
|
| 84 |
unset($geshi);
|
| 85 |
// store
|
| 86 |
$available_languages[$name] = array('language_path' => $dir, 'fullname' => $fullname);
|
| 87 |
}
|
| 88 |
}
|
| 89 |
ksort($available_languages);
|
| 90 |
// save array to database
|
| 91 |
variable_set('geshifilter_available_languages_cache', $available_languages);
|
| 92 |
}
|
| 93 |
}
|
| 94 |
return $available_languages;
|
| 95 |
}
|
| 96 |
|
| 97 |
/**
|
| 98 |
* List of enabled languages.
|
| 99 |
* (with caching)
|
| 100 |
* @return array with enabled languages mapping language code to full name.
|
| 101 |
*/
|
| 102 |
function _geshifilter_get_enabled_languages() {
|
| 103 |
static $enabled_languages = NULL;
|
| 104 |
if ($enabled_languages === NULL) {
|
| 105 |
$enabled_languages = array();
|
| 106 |
$languages = _geshifilter_get_available_languages();
|
| 107 |
foreach ($languages as $language => $language_data) {
|
| 108 |
if (variable_get('geshifilter_language_enabled_'. $language, FALSE)) {
|
| 109 |
$enabled_languages[$language] = $language_data['fullname'];
|
| 110 |
}
|
| 111 |
}
|
| 112 |
}
|
| 113 |
return $enabled_languages;
|
| 114 |
}
|
| 115 |
|
| 116 |
/**
|
| 117 |
* Helper function for gettings the tags
|
| 118 |
* (with caching)
|
| 119 |
*/
|
| 120 |
function _geshifilter_get_tags($format) {
|
| 121 |
static $geshifilter_tags_cache = array();
|
| 122 |
if (!isset($geshifilter_tags_cache[$format])) {
|
| 123 |
$generic_code_tags = _geshifilter_tag_split(geshifilter_tags($format));
|
| 124 |
$language_tags = array();
|
| 125 |
$tag_to_lang = array();
|
| 126 |
$enabled_languages = _geshifilter_get_enabled_languages();
|
| 127 |
foreach ($enabled_languages as $language => $fullname) {
|
| 128 |
$lang_tags = _geshifilter_tag_split(geshifilter_language_tags($language, $format));
|
| 129 |
foreach ($lang_tags as $lang_tag) {
|
| 130 |
$language_tags[] = $lang_tag;
|
| 131 |
$tag_to_lang[$lang_tag] = $language;
|
| 132 |
}
|
| 133 |
}
|
| 134 |
$geshifilter_tags_cache[$format] = array($generic_code_tags, $language_tags, $tag_to_lang);
|
| 135 |
}
|
| 136 |
return $geshifilter_tags_cache[$format];
|
| 137 |
}
|
| 138 |
|
| 139 |
/**
|
| 140 |
* helper function for generating a GeSHi object
|
| 141 |
* @param $language the language to generate a GeSHi object for
|
| 142 |
*/
|
| 143 |
function _geshifilter_geshi_factory($source_code, $language) {
|
| 144 |
$available_languages = _geshifilter_get_available_languages();
|
| 145 |
$geshi = new GeSHi($source_code, $language);
|
| 146 |
$geshi->set_language_path($available_languages[$language]['language_path']);
|
| 147 |
return $geshi;
|
| 148 |
}
|
| 149 |
|
| 150 |
/**
|
| 151 |
* Helper function for splitting a string on white spaces.
|
| 152 |
* Using explode(' ', $string) is not enough because it returns empty elements
|
| 153 |
* if $string contains consecutive spaces.
|
| 154 |
*/
|
| 155 |
function _geshifilter_whitespace_explode($string) {
|
| 156 |
return preg_split('/\s+/', $string, -1, PREG_SPLIT_NO_EMPTY);
|
| 157 |
}
|
| 158 |
|
| 159 |
function _geshifilter_tag_split($string) {
|
| 160 |
return preg_split('/\s+|<|>|\[|\]/', $string, -1, PREG_SPLIT_NO_EMPTY);
|
| 161 |
}
|
| 162 |
|
| 163 |
// General settings
|
| 164 |
function geshifilter_use_format_specific_options() {
|
| 165 |
return variable_get('geshifilter_format_specific_options', FALSE);
|
| 166 |
}
|
| 167 |
|
| 168 |
function geshifilter_tags($format = NULL) {
|
| 169 |
if (!geshifilter_use_format_specific_options() || $format === NULL) {
|
| 170 |
return variable_get('geshifilter_tags', 'code blockcode');
|
| 171 |
}
|
| 172 |
return variable_get("geshifilter_tags_{$format}", geshifilter_tags());
|
| 173 |
}
|
| 174 |
|
| 175 |
function _geshifilter_tag_styles($format = NULL) {
|
| 176 |
if (!geshifilter_use_format_specific_options() || $format === NULL) {
|
| 177 |
return variable_get('geshifilter_tag_styles', array(
|
| 178 |
GESHIFILTER_BRACKETS_ANGLE => GESHIFILTER_BRACKETS_ANGLE,
|
| 179 |
GESHIFILTER_BRACKETS_SQUARE => GESHIFILTER_BRACKETS_SQUARE,
|
| 180 |
));
|
| 181 |
}
|
| 182 |
return variable_get("geshifilter_tag_styles_{$format}", _geshifilter_tag_styles());
|
| 183 |
}
|
| 184 |
|
| 185 |
function geshifilter_language_tags($language, $format = NULL) {
|
| 186 |
if (!geshifilter_use_format_specific_options() || $format === NULL) {
|
| 187 |
return variable_get("geshifilter_language_tags_{$language}", '');
|
| 188 |
}
|
| 189 |
return variable_get("geshifilter_language_tags_{$language}_{$format}", geshifilter_language_tags($language));
|
| 190 |
}
|