From 9b457bb5da92c19c22d0e0ce0715748b5b21421d Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 9 Apr 2016 21:48:08 +0100 Subject: [PATCH] Add option to optimise inline css. --- README.md | 1 + build/index.php | 36 +++++++++++++++++++++++++++++++++++- core.php | 34 +++++++++++++++++++++++++++++++++- settings.fragment.php | 2 ++ 4 files changed, 71 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a059c16..f8771aa 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ Key | Value | Explanation `show_subpages` | boolean | Whether to show a list of subpages at the bottom of the page. `subpages_display_depth` | number | The depth to which we should display when listing subpages at the bottom of the page. `max_recent_changes` | number | The maximum number of recent changes to display on the recent changes page. +`optimize_pages` | boolean | Whether to optimise all webpages generated. Currently this option only minifies inline css. `users` | array of users | An array of usernames and passwords - passwords should be hashed with sha256. Put one user / password on each line, remembering the comma at the end. The last user in the list doesn't need a comma after their details though. Tip: use the `hash` action to hash passwords appropriately for Pepperminty Wiki, but remember to use an incognito window! `require_login_view` | boolean | Whether to require that users login before they do anything else. If you combine this setting with the `data_storage_dir` option to move the storage directory outside your web root, this will completely hide your wiki from anyone who isn't logged in. `data_storage_dir` | path | The directory in which to store all files, except this main index.php. A single dot ('.') denotes the current directory. Remember to omit the trailing slash from the directory name, as it is added automatically by Pepperminty Wiki. Combine with `require_login_view` in order to completely hide your wiki from anonymous users. diff --git a/build/index.php b/build/index.php index 8f767c1..7b7a44d 100644 --- a/build/index.php +++ b/build/index.php @@ -260,6 +260,8 @@ $settings->defaultaction = "view"; // A VIRUS INTO YOUR WIKI $settings->updateurl = "https://raw.githubusercontent.com/sbrl/pepperminty-wiki/master/index.php"; +// Whether to optimise all webpages generated. +$settings->optimize_pages = true; /////////////////////////////////////////////////////////////////////////////// //////////////////////////////// Other Modules //////////////////////////////// @@ -785,6 +787,11 @@ function system_extension_mime_type($ext) { return isset($types[$ext]) ? $types[$ext] : null; } +/** + * Generates a stack trace. + * @param bool $log_trace Whether to send the stack trace to the error log. + * @return string A string prepresentation of a stack trace. + */ function stack_trace($log_trace = true) { $result = ""; @@ -1198,7 +1205,34 @@ class page_renderer if(preg_match("/^[^\/]*\/\/|^\//", $settings->css)) return ""; else - return ""; + { + $css = $settings->css; + if(!empty($settings->optimize_pages)) + { + // CSS Minification ideas by Jean from catswhocode.com + // Link: http://www.catswhocode.com/blog/3-ways-to-compress-css-files-using-php + // Remove comments + $css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', "", $css); + // Cut down whitespace + $css = preg_replace('/\s+/', " ", $css); + // Remove whitespace after colons and semicolons + $css = str_replace([ + " :", + ": ", + "; ", + " { ", + " } " + ], [ + ":", + ":", + ";", + "{", + "}" + ], $css); + + } + return ""; + } } public static $nav_divider = " | "; diff --git a/core.php b/core.php index 1d8386c..0f89e4b 100644 --- a/core.php +++ b/core.php @@ -375,6 +375,11 @@ function system_extension_mime_type($ext) { return isset($types[$ext]) ? $types[$ext] : null; } +/** + * Generates a stack trace. + * @param bool $log_trace Whether to send the stack trace to the error log. + * @return string A string prepresentation of a stack trace. + */ function stack_trace($log_trace = true) { $result = ""; @@ -788,7 +793,34 @@ class page_renderer if(preg_match("/^[^\/]*\/\/|^\//", $settings->css)) return ""; else - return ""; + { + $css = $settings->css; + if(!empty($settings->optimize_pages)) + { + // CSS Minification ideas by Jean from catswhocode.com + // Link: http://www.catswhocode.com/blog/3-ways-to-compress-css-files-using-php + // Remove comments + $css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', "", $css); + // Cut down whitespace + $css = preg_replace('/\s+/', " ", $css); + // Remove whitespace after colons and semicolons + $css = str_replace([ + " :", + ": ", + "; ", + " { ", + " } " + ], [ + ":", + ":", + ";", + "{", + "}" + ], $css); + + } + return ""; + } } public static $nav_divider = " | "; diff --git a/settings.fragment.php b/settings.fragment.php index 2389ef1..5b8b162 100644 --- a/settings.fragment.php +++ b/settings.fragment.php @@ -257,6 +257,8 @@ $settings->defaultaction = "view"; // A VIRUS INTO YOUR WIKI $settings->updateurl = "https://raw.githubusercontent.com/sbrl/pepperminty-wiki/master/index.php"; +// Whether to optimise all webpages generated. +$settings->optimize_pages = true; /////////////////////////////////////////////////////////////////////////////// //////////////////////////////// Other Modules ////////////////////////////////