Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minify HTML #27

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions _build/properties.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@
'options' => '',
'value' => '',
),
array(
'name' => 'minifyHTML',
'desc' => 'If enabled, minifies HTML output before cacheing to the filesystem.',
'type' => 'combo-boolean',
'options' => '',
'value' => false,
),
);

return $properties;
5 changes: 4 additions & 1 deletion core/components/statcache/elements/plugins/StaticCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

require_once MODX_CORE_PATH . 'components/statcache/lib/StatCache.php';
require_once MODX_CORE_PATH . 'components/statcache/lib/phpwee/phpwee.php';

if (!empty($contexts)) {
$contexts = explode(',', $contexts);
Expand Down Expand Up @@ -137,7 +138,9 @@
$statcacheFile = StatCache::getInstance($modx, $scriptProperties)->getStaticPath($modx->resource, $scriptProperties);

/* attempt to write the complete Resource output to the static file */
if (!$modx->cacheManager->writeFile($statcacheFile, $modx->resource->_output)) {
$output = (!empty($minifyHTML)) ? trim(PHPWee\Minify::html($modx->resource->_output)) : $modx->resource->_output;
$modx->log(modX::LOG_LEVEL_ERROR, "statcacheFile:$statcacheFile");
if (!$modx->cacheManager->writeFile($statcacheFile, $output)) {
$modx->log(modX::LOG_LEVEL_ERROR, "Error caching output from Resource {$modx->resource->get('id')} to static file {$statcacheFile}", '', __FUNCTION__, __FILE__, __LINE__);
}
}
Expand Down
35 changes: 35 additions & 0 deletions core/components/statcache/lib/phpwee/phpwee.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?
namespace PHPWee;
require_once("src/CssMin/CssMin.php");
require_once("src/HtmlMin/HtmlMin.php");
require_once("src/JsMin/JsMin.php");


// Open-source (BSD) PHP inline minifier functions for HTML, XHTML, HTML5, CSS 1-3 and Javascript.
// BSD Licensed - https://github.com/searchturbine/phpwee-php-minifier/blob/master/LICENSE
//
// Usage
// $output = \PHPWee\Minify::html($any_html);
// $output = \PHPWee\Minify::css($any_css);
// $output = \PHPWee\Minify::js($any_js);





class Minify{

public static function html($html){
return HtmlMin::minify($html);
}

public static function css($css){
return CssMin::minify($css);
}

public static function js($js){
return JsMin::minify($js);
}

}

Loading