From db772b6a5eb0c64716356f06535b1c32ee1382a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Goetz?= Date: Sat, 25 Apr 2020 22:32:43 +0200 Subject: [PATCH] Allow to create a `style.css` file in the documentation to override some styles. #180 --- docs/10_For_Developers/Creating_a_Theme.md | 5 +++++ libs/DauxHelper.php | 20 +++++++++++++++++++- templates/layout/00_layout.php | 5 ----- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/docs/10_For_Developers/Creating_a_Theme.md b/docs/10_For_Developers/Creating_a_Theme.md index 09608f7..4174a66 100644 --- a/docs/10_For_Developers/Creating_a_Theme.md +++ b/docs/10_For_Developers/Creating_a_Theme.md @@ -2,6 +2,11 @@ In its simplest form, a theme is an empty folder with a `config.json` file conta After that, every setting is optional, but you can override everything if you'd like to. +> **Overriding styles** +> +> If you want to tweak a few styles, you can create a `style.css` file at the root of your documentation +> directory and it will be included automatically. By doing this, you don't need to create a new theme. + ## `config.json` options Here is an example `config.json` file : diff --git a/libs/DauxHelper.php b/libs/DauxHelper.php index c429f00..79431f1 100644 --- a/libs/DauxHelper.php +++ b/libs/DauxHelper.php @@ -32,11 +32,18 @@ class DauxHelper */ protected static function getTheme(Config $config, $current_url) { + static $cache = []; + $htmlTheme = $config->getHTML()->getTheme(); $theme_folder = $config->getThemesPath() . DIRECTORY_SEPARATOR . $htmlTheme; $theme_url = $config->getBaseUrl() . 'themes/' . $htmlTheme . '/'; + $cache_key = "$current_url-$htmlTheme"; + if (array_key_exists($cache_key, $cache)) { + return $cache[$cache_key]; + } + $theme = []; if (is_file($theme_folder . DIRECTORY_SEPARATOR . 'config.json')) { $theme = json_decode(file_get_contents($theme_folder . DIRECTORY_SEPARATOR . 'config.json'), true); @@ -45,7 +52,7 @@ class DauxHelper } } - //Default parameters for theme + // Default parameters for theme $theme += [ 'name' => $htmlTheme, 'css' => [], @@ -54,6 +61,7 @@ class DauxHelper 'favicon' => 'themes/daux/img/favicon.png', 'templates' => $theme_folder . DIRECTORY_SEPARATOR . 'templates', 'variants' => [], + 'with_search' => $config->getHTML()->hasSearch() ]; if ($config->getHTML()->hasThemeVariant()) { @@ -77,6 +85,14 @@ class DauxHelper } } + if ($theme['with_search']) { + $theme['css'][] = 'daux_libraries/search.css'; + } + + if (is_file($config->getDocumentationDirectory() . DIRECTORY_SEPARATOR . 'style.css')) { + $theme['css'][]= 'style.css'; + } + $substitutions = [ '' => $config->getLocalBase(), '' => $current_url, @@ -93,6 +109,8 @@ class DauxHelper } } + $cache[$cache_key] = $theme; + return $theme; } diff --git a/templates/layout/00_layout.php b/templates/layout/00_layout.php index c4f7297..82ebd5b 100755 --- a/templates/layout/00_layout.php +++ b/templates/layout/00_layout.php @@ -48,11 +48,6 @@ getTheme()->getCSS() as $css) { ?> - - getHTML()->hasSearch()) { ?> - - - section('content'); ?>