Simplify configure_theme and get_theme

This commit is contained in:
Stéphane Goetz 2015-04-22 14:05:37 +02:00 committed by Stéphane Goetz
parent c79c692042
commit 2c228273fb
5 changed files with 30 additions and 76 deletions

View File

@ -138,7 +138,7 @@
$params['date_modified'] = $this->options['date_modified']; $params['date_modified'] = $this->options['date_modified'];
$params['file_editor'] = false; $params['file_editor'] = false;
$params['theme'] = GeneratorHelper::configure_theme( $params['theme'] = DauxHelper::get_theme(
$this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'], $this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'],
$params['base_url'], $params['base_url'],
$this->local_base, $this->local_base,
@ -180,7 +180,7 @@
$params['date_modified'] = $this->options['date_modified']; $params['date_modified'] = $this->options['date_modified'];
$params['file_editor'] = $this->options['file_editor']; $params['file_editor'] = $this->options['file_editor'];
$params['theme'] = ServerHelper::configure_theme( $params['theme'] = DauxHelper::get_theme(
$this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'], $this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'],
$params['base_url'], $params['base_url'],
$this->local_base, $this->local_base,

View File

@ -54,7 +54,7 @@
return $filename; return $filename;
} }
public static function get_theme($theme_folder, $local_base) { public static function get_theme($theme_folder, $base_url, $local_base, $theme_url) {
$name = static::pathinfo($theme_folder); $name = static::pathinfo($theme_folder);
$theme = array(); $theme = array();
@ -64,25 +64,40 @@
} }
$theme['name'] = $name['filename']; $theme['name'] = $name['filename'];
$theme += ['css' => [], 'js' => [], 'fonts' => [], 'require-jquery' => false, 'bootstrap-js' => false]; //Default parameters for theme
$theme += [
'css' => [],
'js' => [],
'fonts' => [],
'require-jquery' => false,
'bootstrap-js' => false,
'favicon' => '<base_url>img/favicon.png',
'template' => $local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default/default.tpl',
'error-template' => $local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default/error.tpl',
];
$substitutions = ['<local_base>' => $local_base, '<base_url>' => $base_url, '<theme_url>' => $theme_url];
if (!isset($theme['template'])){ // Substitute some placeholders
$theme['template'] = $local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default/default.tpl'; $theme['template'] = strtr($theme['template'], $substitutions);
} else{ $theme['error-template'] = strtr($theme['error-template'], $substitutions);
$theme['template'] = str_replace('<local_base>', $local_base, $theme['template']); $theme['favicon'] = utf8_encode(strtr($theme['favicon'], $substitutions));
foreach ($theme['css'] as $key => $css) {
$theme['css'][$key] = utf8_encode(strtr($css, $substitutions));
} }
if (!isset($theme['error-template'])) {
$theme['error-template'] = $local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default/error.tpl'; foreach ($theme['fonts'] as $key => $font) {
} else { $theme['fonts'][$key] = utf8_encode(strtr($font, $substitutions));
$theme['error-template'] = str_replace('<local_base>', $local_base, $theme['error-template']); }
foreach ($theme['js'] as $key => $js) {
$theme['js'][$key] = utf8_encode(strtr($js, $substitutions));
} }
return $theme; return $theme;
} }
public static function google_analytics($analytics, $host) { public static function google_analytics($analytics, $host) {
$ga = <<<EOT $ga = <<<EOT
<script> <script>

View File

@ -22,8 +22,6 @@ class Generator {
private function recursive_generate_static($tree, $output_dir, $params, $base_url = '') { private function recursive_generate_static($tree, $output_dir, $params, $base_url = '') {
$params['base_url'] = $params['base_page'] = $base_url; $params['base_url'] = $params['base_page'] = $base_url;
$new_params = $params; $new_params = $params;
//changed this as well in order for the templates to be put in the right place
$params['theme'] = Helper::rebase_theme($params['theme'], $base_url, $params['base_url'] . "templates/default/themes/" . $params['theme']['name'] . '/');
// //
$params['image'] = str_replace('<base_url>', $base_url, $params['image']); $params['image'] = str_replace('<base_url>', $base_url, $params['image']);
if ($base_url !== '') $params['entry_page'] = $tree->first_page; if ($base_url !== '') $params['entry_page'] = $tree->first_page;
@ -41,6 +39,4 @@ class Generator {
} }
} }
} }
} }

View File

@ -48,32 +48,4 @@ class Helper {
} }
closedir($dir); closedir($dir);
} }
public static function rebase_theme($theme, $base_url, $theme_url) {
$theme['favicon'] = utf8_encode(str_replace('<base_url>', $base_url, $theme['favicon']));
$theme['favicon'] = str_replace('<theme_url>', $theme_url, $theme['favicon']);
foreach ($theme['css'] as $key => $css) {
$theme['css'][$key] = utf8_encode(str_replace('<base_url>', $base_url, $css));
$theme['css'][$key] = utf8_encode(str_replace('<theme_url>', $theme_url, $css));
}
foreach ($theme['fonts'] as $key => $font) {
$theme['fonts'][$key] = utf8_encode(str_replace('<base_url>', $base_url, $font));
$theme['fonts'][$key] = utf8_encode(str_replace('<theme_url>', $theme_url, $font));
}
foreach ($theme['js'] as $key => $js) {
$theme['js'][$key] = utf8_encode(str_replace('<base_url>', $base_url, $js));
$theme['js'][$key] = utf8_encode(str_replace('<theme_url>', $theme_url, $js));
}
return $theme;
}
public static function configure_theme($theme, $base_url, $local_base, $theme_url) {
$theme = DauxHelper::get_theme($theme, $local_base);
if (!isset($theme['favicon'])) $theme['favicon'] = '<base_url>img/favicon.png';
return $theme;
}
} }

View File

@ -4,39 +4,10 @@ use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper; use Todaymade\Daux\DauxHelper;
class Helper { class Helper {
public static function configure_theme($theme_file, $base_url, $local_base, $theme_url) {
$theme = DauxHelper::get_theme($theme_file, $local_base);
if (!isset($theme['favicon'])){
$theme['favicon'] = utf8_encode($base_url . 'img/favicon.png');
} else {
$theme['favicon'] = utf8_encode(str_replace('<base_url>', $base_url, $theme['favicon']));
$theme['favicon'] = str_replace('<theme_url>', $theme_url, $theme['favicon']);
}
foreach ($theme['css'] as $key => $css) {
$theme['css'][$key] = utf8_encode(str_replace('<base_url>', $base_url, $css));
$theme['css'][$key] = utf8_encode(str_replace('<theme_url>', $theme_url, $css));
}
foreach ($theme['fonts'] as $key => $font) {
$theme['fonts'][$key] = utf8_encode(str_replace('<base_url>', $base_url, $font));
$theme['fonts'][$key] = utf8_encode(str_replace('<theme_url>', $theme_url, $font));
}
foreach ($theme['js'] as $key => $js) {
$theme['js'][$key] = utf8_encode(str_replace('<base_url>', $base_url, $js));
$theme['js'][$key] = utf8_encode(str_replace('<theme_url>', $theme_url, $js));
}
return $theme;
}
public static function get_error_params(Daux $daux) public static function get_error_params(Daux $daux)
{ {
$params = $daux->get_base_params(); $params = $daux->get_base_params();
$params['theme'] = Helper::configure_theme( $params['theme'] = DauxHelper::get_theme(
$daux->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $daux->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $daux->options['theme'], $daux->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $daux->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $daux->options['theme'],
$params['base_url'], $params['base_url'],
$daux->local_base, $daux->local_base,