Created
January 29, 2011 06:11
-
-
Save nathansmith/801588 to your computer and use it in GitHub Desktop.
Drupal 7 HTML5 Minification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function THEMENAME_process_html(&$variables) { | |
if (!empty($variables)) { | |
$css_before = array(' type="text/css"', ' media="all"'); | |
$css_after = array('', ''); | |
$js_before = array(' type="text/javascript"', '<!--//--><![CDATA[//><!--', '//--><!]]>'); | |
$js_after = array('', '', ''); | |
$lines_two = "/\n\n/"; | |
$lines_one = "\n"; | |
$tags_before = array("/>\s\s+/", "/\s\s+</", "/>\t+</"); | |
$tags_after = array('> ', ' <', '> <'); | |
$text_before = array("/\s\s+(?=\w)/", "/(?<=\w)\s\s+/"); | |
$text_after = array(' ', ' '); | |
// CSS files. | |
$styles = drupal_get_css(); | |
$styles = str_replace($css_before, $css_after, $styles); | |
$variables['styles'] = $styles; | |
// JavaScript files. | |
$scripts = drupal_get_js(); | |
$scripts = str_replace($js_before, $js_after, $scripts); | |
$scripts = preg_replace($lines_two, $lines_one, $scripts); | |
$variables['scripts'] = $scripts; | |
// Page top. | |
$page_top = $variables['page_top']; | |
$page_top = preg_replace($text_before, $text_after, $page_top); | |
$page_top = preg_replace($tags_before, $tags_after, $page_top); | |
$variables['page_top'] = $page_top; | |
// Page content. | |
if (!preg_match('/<pre|<textarea/', $variables['page'])) { | |
$page = $variables['page']; | |
$page = preg_replace($text_before, $text_after, $page); | |
$page = preg_replace($tags_before, $tags_after, $page); | |
$variables['page'] = $page; | |
} | |
// Footer JavaScript files. | |
$footer_js = drupal_get_js('footer'); | |
$footer_js = str_replace($js_before, $js_after, $footer_js); | |
$footer_js = preg_replace($lines_two, $lines_one, $footer_js); | |
// Page bottom. | |
$page_bottom = $variables['page_bottom']; | |
$page_bottom = preg_replace($text_before, $text_after, $page_bottom); | |
$page_bottom = preg_replace($tags_before, $tags_after, $page_bottom); | |
$variables['page_bottom'] = $page_bottom . $footer_js; | |
} | |
} |
I've updated the gist to reflect that boost in efficiency. Thanks! :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// Page Bottom example.
$page_bottom = $variables['page_bottom'];
$patterns = array("/\s\s+(?=\w)/", "/(?<=\w)\s\s+/", "/>\s\s+/", "/\s\s+</", "/>\t+</");
$replacement = array(' ', ' ', '> ', ' <', '> <');
$page_bottom = preg_replace($patterns, $replacement, $page_bottom);