GeshiFilterCss.php in GeSHi Filter for syntax highlighting 8.2
File
src/GeshiFilterCss.php
View source
<?php
namespace Drupal\geshifilter;
use Drupal\Core\File\FileSystemInterface;
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 \Drupal::service('file_system')
->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
}
public static function languageCssPath($dironly = FALSE) {
$directory = \Drupal::config('system.file')
->get('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::messenger()
->addError(t('Error while generating CSS rules: could not load GeSHi library.'));
}
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, FileSystemInterface::EXISTS_REPLACE);
if ($ret) {
\Drupal::messenger()
->addStatus(t('(Re)generated external CSS style sheet %file.', [
'%file' => $ret
->getFilename(),
]));
}
else {
\Drupal::messenger()
->addError(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(),
]));
}
\Drupal::state()
->set('cssfile_languages', $languages_hash);
}
}
}