mirror of
https://github.com/sbrl/Pepperminty-Wiki.git
synced 2024-11-22 16:33:00 +00:00
Refactor css minification code out into own function
This commit is contained in:
parent
a239f5c044
commit
6120fa8842
3 changed files with 119 additions and 119 deletions
|
@ -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
|
- 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
|
- 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`
|
- 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
|
||||||
- Fixed a bug in the search query performance metrics
|
- Fixed a bug in the search query performance metrics
|
||||||
|
|
|
@ -279,8 +279,7 @@ function hide_email($str)
|
||||||
* of $haystack.
|
* of $haystack.
|
||||||
* @return bool Whether $needle can be found at the beginning 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);
|
$length = strlen($needle);
|
||||||
return (substr($haystack, 0, $length) === $needle);
|
return (substr($haystack, 0, $length) === $needle);
|
||||||
}
|
}
|
||||||
|
@ -328,8 +327,7 @@ function startsWith($haystack, $needle) {
|
||||||
* @param string $end The substring test for.
|
* @param string $end The substring test for.
|
||||||
* @return bool Whether $whole ends in $end.
|
* @return bool Whether $whole ends in $end.
|
||||||
*/
|
*/
|
||||||
function endsWith($whole, $end)
|
function endsWith($whole, $end) {
|
||||||
{
|
|
||||||
return (strpos($whole, $end, strlen($whole) - strlen($end)) !== false);
|
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.
|
* @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.
|
* @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);
|
$index = strpos($subject, $find);
|
||||||
if($index !== false)
|
if($index !== false)
|
||||||
return substr_replace($subject, $replace, $index, strlen($find));
|
return substr_replace($subject, $replace, $index, strlen($find));
|
||||||
|
@ -357,8 +354,7 @@ function str_replace_once($find, $replace, $subject)
|
||||||
* @author Edited by Starbeamrainbowlabs
|
* @author Edited by Starbeamrainbowlabs
|
||||||
* @return array An array of mime type mappings.
|
* @return array An array of mime type mappings.
|
||||||
*/
|
*/
|
||||||
function system_mime_type_extensions()
|
function system_mime_type_extensions() {
|
||||||
{
|
|
||||||
global $settings;
|
global $settings;
|
||||||
$out = array();
|
$out = array();
|
||||||
$file = fopen($settings->mime_extension_mappings_location, 'r');
|
$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.
|
* @param string $type The mime type to convert.
|
||||||
* @return string The extension for the given mime type.
|
* @return string The extension for the given mime type.
|
||||||
*/
|
*/
|
||||||
function system_mime_type_extension($type)
|
function system_mime_type_extension($type) {
|
||||||
{
|
|
||||||
static $exts;
|
static $exts;
|
||||||
if(!isset($exts))
|
if(!isset($exts))
|
||||||
$exts = system_mime_type_extensions();
|
$exts = system_mime_type_extensions();
|
||||||
|
@ -402,8 +397,7 @@ function system_mime_type_extension($type)
|
||||||
* @author Edited by Starbeamrainbowlabs
|
* @author Edited by Starbeamrainbowlabs
|
||||||
* @return array An array mapping file extensions to their associated mime types.
|
* @return array An array mapping file extensions to their associated mime types.
|
||||||
*/
|
*/
|
||||||
function system_extension_mime_types()
|
function system_extension_mime_types() {
|
||||||
{
|
|
||||||
global $settings;
|
global $settings;
|
||||||
$out = array();
|
$out = array();
|
||||||
$file = fopen($settings->mime_extension_mappings_location, 'r');
|
$file = fopen($settings->mime_extension_mappings_location, 'r');
|
||||||
|
@ -505,8 +499,7 @@ if (!function_exists('getallheaders')) {
|
||||||
* @package core
|
* @package core
|
||||||
* @todo Identify which platforms don't have it and whether we still need this
|
* @todo Identify which platforms don't have it and whether we still need this
|
||||||
*/
|
*/
|
||||||
function getallheaders()
|
function getallheaders() {
|
||||||
{
|
|
||||||
if (!is_array($_SERVER))
|
if (!is_array($_SERVER))
|
||||||
return [];
|
return [];
|
||||||
|
|
||||||
|
@ -525,8 +518,7 @@ if (!function_exists('getallheaders')) {
|
||||||
* @param int $timestamp The timestamp to render.
|
* @param int $timestamp The timestamp to render.
|
||||||
* @return string HTML representing the given timestamp.
|
* @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>";
|
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
|
* @param object $rchange The recent change to render as a page name
|
||||||
* @return string HTML representing the name of the given page.
|
* @return string HTML representing the name of the given page.
|
||||||
*/
|
*/
|
||||||
function render_pagename($rchange)
|
function render_pagename($rchange) {
|
||||||
{
|
|
||||||
global $pageindex;
|
global $pageindex;
|
||||||
$pageDisplayName = $rchange->page;
|
$pageDisplayName = $rchange->page;
|
||||||
if(isset($pageindex->$pageDisplayName) and !empty($pageindex->$pageDisplayName->redirect))
|
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.
|
* @param string $editorName The name of the editor to render.
|
||||||
* @return string HTML representing the given editor's name.
|
* @return string HTML representing the given editor's name.
|
||||||
*/
|
*/
|
||||||
function render_editor($editorName)
|
function render_editor($editorName) {
|
||||||
{
|
|
||||||
return "<span class='editor'>✎ $editorName</span>";
|
return "<span class='editor'>✎ $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.
|
* Saves the settings file back to peppermint.json.
|
||||||
* @package core
|
* @package core
|
||||||
|
|
|
@ -303,36 +303,21 @@ class page_renderer
|
||||||
{
|
{
|
||||||
global $settings, $defaultCSS;
|
global $settings, $defaultCSS;
|
||||||
|
|
||||||
|
$result = "";
|
||||||
if(self::is_css_url()) {
|
if(self::is_css_url()) {
|
||||||
if($settings->css[0] === "/") // Push it if it's a relative resource
|
if($settings->css[0] === "/") // Push it if it's a relative resource
|
||||||
self::add_server_push_indicator("style", $settings->css);
|
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 {
|
} else {
|
||||||
$css = $settings->css == "auto" ? $defaultCSS : $settings->css;
|
$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";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue