Commit 3fc51a92 by Scott

Optimize theme output (minify HTML)

parent 02833d53
...@@ -280,6 +280,7 @@ ...@@ -280,6 +280,7 @@
'min_len_q_content' => 0, 'min_len_q_content' => 0,
'min_len_q_title' => 12, 'min_len_q_title' => 12,
'min_num_q_tags' => 0, 'min_num_q_tags' => 0,
'minify_html' => 1,
'moderate_notify_admin' => 1, 'moderate_notify_admin' => 1,
'moderate_points_limit' => 150, 'moderate_points_limit' => 150,
'moderate_update_time' => 1, 'moderate_update_time' => 1,
......
...@@ -142,6 +142,7 @@ ...@@ -142,6 +142,7 @@
'min_len_q_content' => 'Minimum length of question body:', 'min_len_q_content' => 'Minimum length of question body:',
'min_len_q_title' => 'Minimum length of question title:', 'min_len_q_title' => 'Minimum length of question title:',
'min_num_q_tags' => 'Minimum number of tags:', 'min_num_q_tags' => 'Minimum number of tags:',
'minify_html' => 'Minify HTML:',
'moderate_anon_post' => 'Use moderation for anonymous posts:', 'moderate_anon_post' => 'Use moderation for anonymous posts:',
'moderate_by_points' => 'Use moderation for users with few points:', 'moderate_by_points' => 'Use moderation for users with few points:',
'moderate_edited_again' => 'Re-moderate posts after editing:', 'moderate_edited_again' => 'Re-moderate posts after editing:',
......
...@@ -219,6 +219,7 @@ ...@@ -219,6 +219,7 @@
'suspend_register_users' => 'checkbox', 'suspend_register_users' => 'checkbox',
'tag_separator_comma' => 'checkbox', 'tag_separator_comma' => 'checkbox',
'use_microdata' => 'checkbox', 'use_microdata' => 'checkbox',
'minify_html' => 'checkbox',
'votes_separated' => 'checkbox', 'votes_separated' => 'checkbox',
'voting_on_as' => 'checkbox', 'voting_on_as' => 'checkbox',
'voting_on_q_page_only' => 'checkbox', 'voting_on_q_page_only' => 'checkbox',
...@@ -384,7 +385,7 @@ ...@@ -384,7 +385,7 @@
} }
array_push($showoptions, array_push($showoptions,
'show_user_points', 'show_post_update_meta', 'show_compact_numbers', 'use_microdata', '', 'show_user_points', 'show_post_update_meta', 'show_compact_numbers', 'use_microdata', 'minify_html', '',
'sort_answers_by', 'show_selected_first', 'page_size_q_as', 'show_a_form_immediate' 'sort_answers_by', 'show_selected_first', 'page_size_q_as', 'show_a_form_immediate'
); );
......
...@@ -490,6 +490,10 @@ ...@@ -490,6 +490,10 @@
'charset' => $charset, 'charset' => $charset,
'direction' => qa_opt('site_text_direction'), 'direction' => qa_opt('site_text_direction'),
'options' => array(
'minify_html' => qa_opt('minify_html'),
),
'site_title' => qa_html(qa_opt('site_title')), 'site_title' => qa_html(qa_opt('site_title')),
'head_lines' => array(), 'head_lines' => array(),
......
...@@ -45,6 +45,7 @@ class qa_html_theme_base ...@@ -45,6 +45,7 @@ class qa_html_theme_base
public $request; public $request;
public $isRTL; // (boolean) whether text direction is Right-To-Left public $isRTL; // (boolean) whether text direction is Right-To-Left
protected $minifyHtml; // (boolean) whether to indent the HTML
protected $indent = 0; protected $indent = 0;
protected $lines = 0; protected $lines = 0;
protected $context = array(); protected $context = array();
...@@ -65,6 +66,7 @@ class qa_html_theme_base ...@@ -65,6 +66,7 @@ class qa_html_theme_base
$this->rooturl = $rooturl; $this->rooturl = $rooturl;
$this->request = $request; $this->request = $request;
$this->isRTL = isset($content['direction']) && $content['direction'] === 'rtl'; $this->isRTL = isset($content['direction']) && $content['direction'] === 'rtl';
$this->minifyHtml = !empty($content['options']['minify_html']);
} }
/** /**
...@@ -85,17 +87,25 @@ class qa_html_theme_base ...@@ -85,17 +87,25 @@ class qa_html_theme_base
public function output_array($elements) public function output_array($elements)
{ {
foreach ($elements as $element) { foreach ($elements as $element) {
$line = str_replace('/>', '>', $element);
if ($this->minifyHtml) {
if (strlen($line))
echo $line."\n";
}
else {
$delta = substr_count($element, '<') - substr_count($element, '<!') - 2*substr_count($element, '</') - substr_count($element, '/>'); $delta = substr_count($element, '<') - substr_count($element, '<!') - 2*substr_count($element, '</') - substr_count($element, '/>');
if ($delta < 0) { if ($delta < 0) {
$this->indent += $delta; $this->indent += $delta;
} }
echo str_repeat("\t", max(0, $this->indent)).str_replace('/>', '>', $element)."\n"; echo str_repeat("\t", max(0, $this->indent)).$line."\n";
if ($delta > 0) { if ($delta > 0) {
$this->indent += $delta; $this->indent += $delta;
} }
}
$this->lines++; $this->lines++;
} }
...@@ -201,7 +211,7 @@ class qa_html_theme_base ...@@ -201,7 +211,7 @@ class qa_html_theme_base
*/ */
public function finish() public function finish()
{ {
if ($this->indent) { if ($this->indent !== 0 && !$this->minifyHtml) {
echo "<!--\nIt's no big deal, but your HTML could not be indented properly. To fix, please:\n". echo "<!--\nIt's no big deal, but your HTML could not be indented properly. To fix, please:\n".
"1. Use this->output() to output all HTML.\n". "1. Use this->output() to output all HTML.\n".
"2. Balance all paired tags like <td>...</td> or <div>...</div>.\n". "2. Balance all paired tags like <td>...</td> or <div>...</div>.\n".
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment