Refactor css minification code out into own function

This commit is contained in:
Starbeamrainbowlabs 2019-09-29 15:54:40 +01:00
parent a239f5c044
commit 6120fa8842
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
3 changed files with 119 additions and 119 deletions

View File

@ -8,6 +8,7 @@ This file holds the changelog for Pepperminty Wiki. This is the master list of t
- TODO Update the first-run wizard to ask whether people want the theme auto-updater to turned on because privacy and inadvertent requests to my web server
- NOTE Don't forget to tell people how to turn the theme auto-updater off in this changelog
- Added automatic dark mode to default theme using `prefers-color-scheme`
- [Module API] Added new `minify_css` module API function by refactoring the page renderer
### Fixed
- Fixed a bug in the search query performance metrics

View File

@ -279,8 +279,7 @@ function hide_email($str)
* of $haystack.
* @return bool Whether $needle can be found at the beginning of $haystack.
*/
function starts_with($haystack, $needle)
{
function starts_with($haystack, $needle) {
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
@ -328,8 +327,7 @@ function startsWith($haystack, $needle) {
* @param string $end The substring test for.
* @return bool Whether $whole ends in $end.
*/
function endsWith($whole, $end)
{
function endsWith($whole, $end) {
return (strpos($whole, $end, strlen($whole) - strlen($end)) !== false);
}
/**
@ -340,8 +338,7 @@ function endsWith($whole, $end)
* @param string $subject The string ot perform the search and replace on.
* @return string The source string after the find and replace has been performed.
*/
function str_replace_once($find, $replace, $subject)
{
function str_replace_once($find, $replace, $subject) {
$index = strpos($subject, $find);
if($index !== false)
return substr_replace($subject, $replace, $index, strlen($find));
@ -357,8 +354,7 @@ function str_replace_once($find, $replace, $subject)
* @author Edited by Starbeamrainbowlabs
* @return array An array of mime type mappings.
*/
function system_mime_type_extensions()
{
function system_mime_type_extensions() {
global $settings;
$out = array();
$file = fopen($settings->mime_extension_mappings_location, 'r');
@ -386,8 +382,7 @@ function system_mime_type_extensions()
* @param string $type The mime type to convert.
* @return string The extension for the given mime type.
*/
function system_mime_type_extension($type)
{
function system_mime_type_extension($type) {
static $exts;
if(!isset($exts))
$exts = system_mime_type_extensions();
@ -402,8 +397,7 @@ function system_mime_type_extension($type)
* @author Edited by Starbeamrainbowlabs
* @return array An array mapping file extensions to their associated mime types.
*/
function system_extension_mime_types()
{
function system_extension_mime_types() {
global $settings;
$out = array();
$file = fopen($settings->mime_extension_mappings_location, 'r');
@ -505,8 +499,7 @@ if (!function_exists('getallheaders')) {
* @package core
* @todo Identify which platforms don't have it and whether we still need this
*/
function getallheaders()
{
function getallheaders() {
if (!is_array($_SERVER))
return [];
@ -525,8 +518,7 @@ if (!function_exists('getallheaders')) {
* @param int $timestamp The timestamp to render.
* @return string HTML representing the given timestamp.
*/
function render_timestamp($timestamp)
{
function render_timestamp($timestamp) {
return "<time class='cursor-query' title='" . date("l jS \of F Y \a\\t h:ia T", $timestamp) . "'>" . human_time_since($timestamp) . "</time>";
}
/**
@ -535,8 +527,7 @@ function render_timestamp($timestamp)
* @param object $rchange The recent change to render as a page name
* @return string HTML representing the name of the given page.
*/
function render_pagename($rchange)
{
function render_pagename($rchange) {
global $pageindex;
$pageDisplayName = $rchange->page;
if(isset($pageindex->$pageDisplayName) and !empty($pageindex->$pageDisplayName->redirect))
@ -550,11 +541,34 @@ function render_pagename($rchange)
* @param string $editorName The name of the editor to render.
* @return string HTML representing the given editor's name.
*/
function render_editor($editorName)
{
function render_editor($editorName) {
return "<span class='editor'>&#9998; $editorName</span>";
}
/**
* Minifies CSS. Uses simple computationally-cheap optimisations to reduce size.
* CSS Minification ideas by Jean from catswhocode.com
* @source http://www.catswhocode.com/blog/3-ways-to-compress-css-files-using-php
* @apiVersion 0.20.0
* @param string $css_str The string of CSS to minify.
* @return string The minified CSS string.
*/
function minify_css($css_str) {
// 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 $css;
}
/**
* Saves the settings file back to peppermint.json.
* @package core

View File

@ -303,36 +303,21 @@ class page_renderer
{
global $settings, $defaultCSS;
$result = "";
if(self::is_css_url()) {
if($settings->css[0] === "/") // Push it if it's a relative resource
self::add_server_push_indicator("style", $settings->css);
return "<link rel='stylesheet' href='$settings->css' />\n";
$result .= "<link rel='stylesheet' href='$settings->css' />\n";
} else {
$css = $settings->css == "auto" ? $defaultCSS : $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);
if(!empty($settings->optimize_pages))
$css = minify_css($css);
$result .= "<style>$css</style>\n";
}
return "<style>$css</style>\n";
if(!empty($settings->css_custom)) {
$css .= "\n/*** Custom CSS ***/\n$settings->css_custom\n/******************/\n";
}
}