. /** * Used to convert a bootswatch file from https://bootswatch.com/ to a Moodle preset. * * @package theme_boost * @subpackage cli * @copyright 2016 Damyon Wiese * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ define('CLI_SCRIPT', true); require(__DIR__.'/../../../config.php'); require_once($CFG->libdir.'/clilib.php'); $usage = " Utility to convert a Bootswatch theme to a Moodle preset compatible with Bootstrap 4. Download _variables.scss and _bootswatch.scss files from https://bootswatch.com/ Run this script. It will generate a new file 'preset.scss' which can be used as a Moodle preset. Usage: # php import-bootswatch.php [--help|-h] # php import-bootswatch.php --variables= --bootswatch= --preset= Options: -h --help Print this help. --variables= Path to the input variables file, defaults to _variables.scss --bootswatch= Path to the input bootswatch file, defauls to _bootswatch.scss --preset= Path to the output preset file, defaults to preset.scss "; list($options, $unrecognised) = cli_get_params([ 'help' => false, 'variables' => '_variables.scss', 'bootswatch' => '_bootswatch.scss', 'preset' => 'preset.scss', ], [ 'h' => 'help', ]); if ($unrecognised) { $unrecognised = implode(PHP_EOL.' ', $unrecognised); cli_error(get_string('cliunknowoption', 'core_admin', $unrecognised)); } if ($options['help']) { cli_writeln($usage); exit(2); } if (is_readable($options['variables'])) { $sourcevariables = file_get_contents($options['variables']); } else { cli_writeln($usage); cli_error('Error reading the variables file: '.$options['variables']); } if (is_readable($options['bootswatch'])) { $sourcebootswatch = file_get_contents($options['bootswatch']); } else { cli_writeln($usage); cli_error('Error reading the bootswatch file: '.$options['bootswatch']); } /** * Local helper function replacing only the first occurrence of a substring. * * @param string $needle Substring to be searched for * @param string $replace New text replacing the old substring * @param string $haystack The text where the replacement happens * @return string */ function str_replace_one($needle, $replace, $haystack) { $pos = strpos($haystack, $needle); if ($pos !== false) { return substr_replace($haystack, $replace, $pos, strlen($needle)); } else { return $haystack; } } // Now start tweaking the variables strings. $workingvariables = $sourcevariables; // Insert a lightest grey colour. $newrule = '$gray-lightest: lighten($gray-lighter, 13.5%);'; $nextline = '$brand-primary'; $workingvariables = str_replace_one($nextline, "$newrule\n\n$nextline", $workingvariables); // Set body-color to text-color. $newrule = '$body-color: $text-color;'; $nextline = '//** Global textual link color.'; $workingvariables = str_replace_one($nextline, "$newrule\n\n$nextline", $workingvariables); // Add a font-size-root the same as font-size-base. $newrule = '$font-size-root: $font-size-base;'; $nextline = '$font-size-large'; $workingvariables = str_replace_one($nextline, "$newrule\n\n$nextline", $workingvariables); // Replace all 'large' with 'lg'. $workingvariables = str_replace('large', 'lg', $workingvariables); // Replace all 'small' with 'sm'. $workingvariables = str_replace('small', 'sm', $workingvariables); // Replace all 'vertical' with 'y'. $workingvariables = str_replace('vertical', 'y', $workingvariables); // Replace all 'horizontal' with 'x'. $workingvariables = str_replace('horizontal', 'x', $workingvariables); // Replace all 'border-radius-base' with 'border-radius'. $workingvariables = str_replace('border-radius-base', 'border-radius', $workingvariables); // Replace all 'condensed-cell' with 'sm-cell'. $workingvariables = str_replace('condensed-cell', 'sm-cell', $workingvariables); // Add styles for btn-secondary. $newrule = '$btn-secondary-color: $btn-default-color; $btn-secondary-bg: $btn-default-bg; $btn-secondary-border: $btn-default-border; '; $nextline = '$btn-primary-color'; $workingvariables = str_replace_one($nextline, "$newrule\n\n$nextline", $workingvariables); // Add a input-border rule matching input-border-color. $newrule = '$input-border-color: $input-border;'; $nextline = '$input-border-radius:'; $workingvariables = str_replace_one($nextline, "$newrule\n\n$nextline", $workingvariables); // Replace all 'input-height-base:' with 'input-height:'. $workingvariables = str_replace('input-height-base:', 'input-height:', $workingvariables); // Replace all '-default-' with '-light-'. $workingvariables = str_replace('navbar-default-', 'navbar-light-', $workingvariables); // Replace all '-inverse-' with '-dark-'. $workingvariables = str_replace('navbar-inverse-', 'navbar-dark-', $workingvariables); // Add a pagination-border-color rule matching pagination-border. $newrule = '$pagination-border-color: $pagination-border;'; $nextline = '$pagination-hover-color'; $workingvariables = str_replace_one($nextline, "$newrule\n\n$nextline", $workingvariables); // Replace all 'label-' with 'tag-'. $workingvariables = str_replace('label-', 'tag-', $workingvariables); // Replace all 'panel-' with 'card-'. $workingvariables = str_replace('panel-', 'card-', $workingvariables); // Write the preset file. $out = fopen($options['preset'], 'w'); if (!$out) { cli_error('Error writing to the preset file'); } fwrite($out, $workingvariables); fwrite($out, ' @import "moodle"; '); // Now replacements on the bootswatch. $workingbootswatch = $sourcebootswatch; $mixins = <<