GeshiFilterCss.php in GeSHi Filter for syntax highlighting 8
File
src/GeshiFilterCss.php
View source
<?php
namespace Drupal\geshifilter;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Url;
class GeshiFilterCss {
public static function generateCss() {
$headers = [];
$headers['Content-type'] = 'text/css';
$css = self::generateLanguagesCssRules();
$response = new Response($css, 200, $headers);
return $response;
}
public static function managedExternalStylesheetPossible() {
$directory = self::languageCssPath(TRUE);
return file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
}
public static function languageCssPath($dironly = FALSE) {
$directory = file_default_scheme() . '://geshi';
if (!$dironly) {
$directory .= '/geshifilter-languages.css';
}
return $directory;
}
public static function generateLanguagesCssRules() {
$output = '';
$geshi_library = GeshiFilter::loadGeshi();
if ($geshi_library['loaded']) {
$languages = GeshiFilter::getEnabledLanguages();
foreach ($languages as $langcode => $language_full_name) {
$geshi = GeshiFilterProcess::geshiFactory('', $langcode);
GeshiFilterProcess::overrideGeshiDefaults($geshi, $langcode);
$output .= $geshi
->get_stylesheet(FALSE) . "\n";
unset($geshi);
}
}
else {
drupal_set_message($this
->t('Error while generating CSS rules: could not load GeSHi library.'), 'error');
}
return $output;
}
public static function generateLanguagesCssFile($force = FALSE) {
$languages = GeshiFilter::getEnabledLanguages();
$languages_hash = serialize($languages);
if ($force || $languages_hash != \Drupal::state()
->get('geshifilter_cssfile_languages')) {
$stylesheet = self::generateLanguagesCssRules();
$stylesheet_filename = self::languageCssPath();
$ret = file_save_data($stylesheet, $stylesheet_filename, FILE_EXISTS_REPLACE);
if ($ret) {
drupal_set_message(t('(Re)generated external CSS style sheet %file.', [
'%file' => $ret
->getFilename(),
]));
}
else {
drupal_set_message(t('Could not generate external CSS file. Check the settings of your <a href="!filesystem">file system</a>.', [
'!filesystem' => Url::fromRoute('system.file_system_settings')
->toString(),
]), 'error');
}
\Drupal::state()
->set('cssfile_languages', $languages_hash);
}
}
}