You are here

function _syntaxhighlighter_setup_autoloader_script in Syntax Highlighter 8

Same name and namespace in other branches
  1. 6.2 syntaxhighlighter.module \_syntaxhighlighter_setup_autoloader_script()
  2. 6 syntaxhighlighter.module \_syntaxhighlighter_setup_autoloader_script()
  3. 7.2 syntaxhighlighter.module \_syntaxhighlighter_setup_autoloader_script()
  4. 7 syntaxhighlighter.module \_syntaxhighlighter_setup_autoloader_script()

Create the autoload setup script file.

Must call this whenever the lib location and/or the enable brushes change. Make sure never call this if the js lib is not found.

2 calls to _syntaxhighlighter_setup_autoloader_script()
SyntaxHighlighterSettingsForm::submitForm in src/Form/SyntaxHighlighterSettingsForm.php
Form submission handler.
syntaxhighlighter_install in ./syntaxhighlighter.install
Implements hook_install().

File

./syntaxhighlighter.module, line 348
Syntax highlight code using the SyntaxHighlighter Javascript library.

Code

function _syntaxhighlighter_setup_autoloader_script(Config $config) {
  $dir = 'public://js';
  $path = $dir . '/syntaxhighlighter.autoloader.js';
  if ($config
    ->get('use_autoloader')) {
    $script_path = base_path() . libraries_get_path('syntaxhighlighter') . '/scripts/';
    $script_data = <<<__HERE__
/*
 * This file is generated by the Syntax Highlighter module
 */
__HERE__;
    $script_data .= "\nfunction syntaxhighlighterAutoloaderSetup() {\n  SyntaxHighlighter.autoloader(\n";
    $need_ending = FALSE;
    $brushes = $config
      ->get('enabled_languages');
    foreach ($brushes as $b) {
      if ($b) {
        if ($need_ending) {
          $script_data .= ",\n";
        }
        $alias = strtolower(substr(substr($b, 7), 0, -3));
        $script_data .= "    '{$alias} {$script_path}{$b}'";
        $need_ending = TRUE;
      }
    }
    $script_data .= "\n);\n}\n";
    file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
    file_unmanaged_save_data($script_data, $path, FILE_EXISTS_REPLACE);
  }
  elseif (file_exists($path)) {
    file_unmanaged_delete($path);
    @\Drupal::service('file_system')
      ->rmdir($dir);
  }
}