From 2c228273fba3f608583cc937651fadf83cb2c101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ste=CC=81phane=20Goetz?= Date: Wed, 22 Apr 2015 14:05:37 +0200 Subject: [PATCH] Simplify `configure_theme` and `get_theme` --- libs/Daux.php | 4 ++-- libs/DauxHelper.php | 39 +++++++++++++++++++++++++----------- libs/Generator/Generator.php | 4 ---- libs/Generator/Helper.php | 28 -------------------------- libs/Server/Helper.php | 31 +--------------------------- 5 files changed, 30 insertions(+), 76 deletions(-) diff --git a/libs/Daux.php b/libs/Daux.php index 176ab18..ff8abe0 100644 --- a/libs/Daux.php +++ b/libs/Daux.php @@ -138,7 +138,7 @@ $params['date_modified'] = $this->options['date_modified']; $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'], $params['base_url'], $this->local_base, @@ -180,7 +180,7 @@ $params['date_modified'] = $this->options['date_modified']; $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'], $params['base_url'], $this->local_base, diff --git a/libs/DauxHelper.php b/libs/DauxHelper.php index 6bf5f4b..1fb429f 100644 --- a/libs/DauxHelper.php +++ b/libs/DauxHelper.php @@ -54,7 +54,7 @@ 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); $theme = array(); @@ -64,25 +64,40 @@ } $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' => '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, '' => $base_url, '' => $theme_url]; - if (!isset($theme['template'])){ - $theme['template'] = $local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default/default.tpl'; - } else{ - $theme['template'] = str_replace('', $local_base, $theme['template']); + // Substitute some placeholders + $theme['template'] = strtr($theme['template'], $substitutions); + $theme['error-template'] = strtr($theme['error-template'], $substitutions); + $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'; - } else { - $theme['error-template'] = str_replace('', $local_base, $theme['error-template']); + + foreach ($theme['fonts'] as $key => $font) { + $theme['fonts'][$key] = utf8_encode(strtr($font, $substitutions)); + } + + foreach ($theme['js'] as $key => $js) { + $theme['js'][$key] = utf8_encode(strtr($js, $substitutions)); } return $theme; } - - public static function google_analytics($analytics, $host) { $ga = << diff --git a/libs/Generator/Generator.php b/libs/Generator/Generator.php index 6a75200..7bd33d2 100644 --- a/libs/Generator/Generator.php +++ b/libs/Generator/Generator.php @@ -22,8 +22,6 @@ class Generator { private function recursive_generate_static($tree, $output_dir, $params, $base_url = '') { $params['base_url'] = $params['base_page'] = $base_url; $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, $params['image']); if ($base_url !== '') $params['entry_page'] = $tree->first_page; @@ -41,6 +39,4 @@ class Generator { } } } - - } diff --git a/libs/Generator/Helper.php b/libs/Generator/Helper.php index b54f3e3..c985550 100644 --- a/libs/Generator/Helper.php +++ b/libs/Generator/Helper.php @@ -48,32 +48,4 @@ class Helper { } closedir($dir); } - - public static function rebase_theme($theme, $base_url, $theme_url) { - $theme['favicon'] = utf8_encode(str_replace('', $base_url, $theme['favicon'])); - $theme['favicon'] = str_replace('', $theme_url, $theme['favicon']); - - foreach ($theme['css'] as $key => $css) { - $theme['css'][$key] = utf8_encode(str_replace('', $base_url, $css)); - $theme['css'][$key] = utf8_encode(str_replace('', $theme_url, $css)); - } - foreach ($theme['fonts'] as $key => $font) { - $theme['fonts'][$key] = utf8_encode(str_replace('', $base_url, $font)); - $theme['fonts'][$key] = utf8_encode(str_replace('', $theme_url, $font)); - - } - foreach ($theme['js'] as $key => $js) { - $theme['js'][$key] = utf8_encode(str_replace('', $base_url, $js)); - $theme['js'][$key] = utf8_encode(str_replace('', $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'] = 'img/favicon.png'; - - return $theme; - } } diff --git a/libs/Server/Helper.php b/libs/Server/Helper.php index bd340df..25b6d73 100644 --- a/libs/Server/Helper.php +++ b/libs/Server/Helper.php @@ -4,39 +4,10 @@ use Todaymade\Daux\Daux; use Todaymade\Daux\DauxHelper; 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, $theme['favicon'])); - $theme['favicon'] = str_replace('', $theme_url, $theme['favicon']); - } - - foreach ($theme['css'] as $key => $css) { - $theme['css'][$key] = utf8_encode(str_replace('', $base_url, $css)); - $theme['css'][$key] = utf8_encode(str_replace('', $theme_url, $css)); - } - - foreach ($theme['fonts'] as $key => $font) { - $theme['fonts'][$key] = utf8_encode(str_replace('', $base_url, $font)); - $theme['fonts'][$key] = utf8_encode(str_replace('', $theme_url, $font)); - } - - foreach ($theme['js'] as $key => $js) { - $theme['js'][$key] = utf8_encode(str_replace('', $base_url, $js)); - $theme['js'][$key] = utf8_encode(str_replace('', $theme_url, $js)); - } - - return $theme; - } - public static function get_error_params(Daux $daux) { $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'], $params['base_url'], $daux->local_base,