mirror of
https://github.com/VladPolskiy/dokuwiki.git
synced 2025-08-15 22:25:03 +00:00

This simplifies style loading and allows the plugins like farmer to extend the the loading of style files. Also adds some first unit tests. Originally discussed in FS#2703 and #134 Squashed commit of the following: commit 5ed550f6ba61a1d475b7572cbff18f0d790c78c2 Merge:0cefed65e
cab1833b0
Author: Andreas Gohr <gohr@cosmocode.de> Date: Thu Feb 28 13:38:46 2019 +0100 Merge branch 'styleConfCascade' of https://github.com/annda/dokuwiki into annda-styleConfCascade * 'styleConfCascade' of https://github.com/annda/dokuwiki: Clean up StyleUtils Update StyleUtils test Fix array to boolean conversion Use better placeholder string in config cascade Calls to StyleUtils use the new constructor Add test for StyleUtils Refactor StyleUtils WIP add temporary test comparing two implementations of StyleUtils WIP add new style config cascade side by side with the old one commitcab1833b09
Author: Anna Dabrowska <dabrowska@cosmocode.de> Date: Thu Feb 21 17:54:27 2019 +0100 Clean up StyleUtils commit766e3907cf
Author: Anna Dabrowska <dabrowska@cosmocode.de> Date: Thu Feb 21 17:51:15 2019 +0100 Update StyleUtils test commitf5c08787dd
Author: Anna Dabrowska <dabrowska@cosmocode.de> Date: Thu Feb 21 12:51:53 2019 +0100 Fix array to boolean conversion commitc967169899
Author: Anna Dabrowska <dabrowska@cosmocode.de> Date: Thu Feb 21 12:38:55 2019 +0100 Use better placeholder string in config cascade commit4845eff54d
Author: Anna Dabrowska <dabrowska@cosmocode.de> Date: Thu Feb 21 12:24:16 2019 +0100 Calls to StyleUtils use the new constructor commit7a6337a8f8
Author: Anna Dabrowska <dabrowska@cosmocode.de> Date: Thu Feb 21 12:22:44 2019 +0100 Add test for StyleUtils commit5ae9e8f69e
Author: Anna Dabrowska <dabrowska@cosmocode.de> Date: Thu Feb 21 12:16:57 2019 +0100 Refactor StyleUtils commita2adcc9f37
Author: Anna Dabrowska <dabrowska@cosmocode.de> Date: Sat Feb 16 23:58:04 2019 +0100 WIP add temporary test comparing two implementations of StyleUtils commit845c40c4fa
Author: Anna Dabrowska <dabrowska@cosmocode.de> Date: Sat Feb 16 23:59:41 2019 +0100 WIP add new style config cascade side by side with the old one
181 lines
6.2 KiB
PHP
181 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace dokuwiki;
|
|
|
|
class StyleUtils
|
|
{
|
|
|
|
/** @var string current template */
|
|
protected $tpl;
|
|
/** @var bool reinitialize styles config */
|
|
protected $reinit;
|
|
/** @var bool $preview preview mode */
|
|
protected $preview;
|
|
/** @var array default replacements to be merged with custom style configs */
|
|
protected $defaultReplacements = array(
|
|
'__text__' => "#000",
|
|
'__background__' => "#fff",
|
|
'__text_alt__' => "#999",
|
|
'__background_alt__' => "#eee",
|
|
'__text_neu__' => "#666",
|
|
'__background_neu__' => "#ddd",
|
|
'__border__' => "#ccc",
|
|
'__highlight__' => "#ff9",
|
|
'__link__' => "#00f",
|
|
);
|
|
|
|
/**
|
|
* StyleUtils constructor.
|
|
* @param string $tpl template name: if not passed as argument, the default value from $conf will be used
|
|
* @param bool $preview
|
|
* @param bool $reinit whether static style conf should be reinitialized
|
|
*/
|
|
public function __construct($tpl = '', $preview = false, $reinit = false)
|
|
{
|
|
if (!$tpl) {
|
|
global $conf;
|
|
$tpl = $conf['conf'];
|
|
}
|
|
$this->tpl = $tpl;
|
|
$this->reinit = $reinit;
|
|
$this->preview = $preview;
|
|
}
|
|
|
|
/**
|
|
* Load style ini contents
|
|
*
|
|
* Loads and merges style.ini files from template and config and prepares
|
|
* the stylesheet modes
|
|
*
|
|
* @author Andreas Gohr <andi@splitbrain.org>
|
|
* @author Anna Dabrowska <info@cosmocode.de>
|
|
*
|
|
* @return array with keys 'stylesheets' and 'replacements'
|
|
*/
|
|
public function cssStyleini()
|
|
{
|
|
static $combined = [];
|
|
if (!empty($combined) && !$this->reinit) {
|
|
return $combined;
|
|
}
|
|
|
|
global $conf;
|
|
global $config_cascade;
|
|
$stylesheets = array(); // mode, file => base
|
|
|
|
// guaranteed placeholder => value
|
|
$replacements = $this->defaultReplacements;
|
|
|
|
// merge all styles from config cascade
|
|
if (!is_array($config_cascade['styleini'])) {
|
|
trigger_error('Missing config cascade for styleini', E_USER_WARNING);
|
|
}
|
|
|
|
// allow replacement overwrites in preview mode
|
|
if ($this->preview) {
|
|
$config_cascade['styleini']['local'][] = $conf['cachedir'] . '/preview.ini';
|
|
}
|
|
|
|
$combined['stylesheets'] = [];
|
|
$combined['replacements'] = [];
|
|
|
|
foreach (array('default', 'local', 'protected') as $config_group) {
|
|
if (empty($config_cascade['styleini'][$config_group])) continue;
|
|
|
|
// set proper server dirs
|
|
$webbase = $this->getWebbase($config_group);
|
|
|
|
foreach ($config_cascade['styleini'][$config_group] as $inifile) {
|
|
// replace the placeholder with the name of the current template
|
|
$inifile = str_replace('%TEMPLATE%', $this->tpl, $inifile);
|
|
|
|
$incbase = dirname($inifile) . '/';
|
|
|
|
if (file_exists($inifile)) {
|
|
$config = parse_ini_file($inifile, true);
|
|
|
|
if (is_array($config['stylesheets'])) {
|
|
|
|
foreach ($config['stylesheets'] as $inifile => $mode) {
|
|
// validate and include style files
|
|
$stylesheets = array_merge($stylesheets, $this->getValidatedStyles($stylesheets, $inifile, $mode, $incbase, $webbase));
|
|
$combined['stylesheets'] = array_merge($combined['stylesheets'], $stylesheets);
|
|
}
|
|
}
|
|
|
|
if (is_array($config['replacements'])) {
|
|
$replacements = array_replace($replacements, $this->cssFixreplacementurls($config['replacements'], $webbase));
|
|
$combined['replacements'] = array_merge($combined['replacements'], $replacements);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return $combined;
|
|
}
|
|
|
|
/**
|
|
* Checks if configured style files exist and, if necessary, adjusts file extensions in config
|
|
*
|
|
* @param array $stylesheets
|
|
* @param string $file
|
|
* @param string $mode
|
|
* @param string $incbase
|
|
* @param string $webbase
|
|
* @return mixed
|
|
*/
|
|
protected function getValidatedStyles($stylesheets, $file, $mode, $incbase, $webbase)
|
|
{
|
|
global $conf;
|
|
if (!file_exists($incbase . $file)) {
|
|
list($extension, $basename) = array_map('strrev', explode('.', strrev($file), 2));
|
|
$newExtension = $extension === 'css' ? 'less' : 'css';
|
|
if (file_exists($incbase . $basename . '.' . $newExtension)) {
|
|
$stylesheets[$mode][$incbase . $basename . '.' . $newExtension] = $webbase;
|
|
if ($conf['allowdebug']) {
|
|
msg("Stylesheet $file not found, using $basename.$newExtension instead. Please contact developer of \"$this->tpl\" template.", 2);
|
|
}
|
|
} elseif ($conf['allowdebug']) {
|
|
msg("Stylesheet $file not found, please contact the developer of \"$this->tpl\" template.", 2);
|
|
}
|
|
}
|
|
$stylesheets[$mode][fullpath($incbase . $file)] = $webbase;
|
|
return $stylesheets;
|
|
}
|
|
|
|
/**
|
|
* Returns the web base path for the given level/group in config cascade.
|
|
* Style resources are relative to the template directory for the main (default) styles
|
|
* but relative to DOKU_BASE for everything else"
|
|
*
|
|
* @param string $config_group
|
|
* @return string
|
|
*/
|
|
protected function getWebbase($config_group)
|
|
{
|
|
if ($config_group === 'default') {
|
|
return tpl_basedir($this->tpl);
|
|
} else {
|
|
return DOKU_BASE;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Amend paths used in replacement relative urls, refer FS#2879
|
|
*
|
|
* @author Chris Smith <chris@jalakai.co.uk>
|
|
*
|
|
* @param array $replacements with key-value pairs
|
|
* @param string $location
|
|
* @return array
|
|
*/
|
|
protected function cssFixreplacementurls($replacements, $location)
|
|
{
|
|
foreach ($replacements as $key => $value) {
|
|
$replacements[$key] = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#', '\\1' . $location, $value);
|
|
}
|
|
return $replacements;
|
|
}
|
|
}
|