Integrate HTML compression function for Pinghsu theme
in Tutorial with 20 comments
Integrate HTML compression function for Pinghsu theme
in Tutorial with 20 comments

The previous article《 Integrate HTML compression for TpCache 》It is the function of directly adding code compression to plug-ins, which is not very good and naive~

Anyway, it solves the problem of incompatibility with TpCache plug-in

So this time I put the code compression function into the Pinghsu topic, which should be the first topic to put the compressed code into the Typecho topic···

The following is a simple tutorial~

Add Function

Or the compressed code function

 function compressHtml($html_source) { $chunks = preg_split('/(<!--<nocompress>-->.*?<!--<\/nocompress>-->|<nocompress>.*?<\/nocompress>|<pre.*?\/pre>|<textarea.*?\/textarea>|<script.*?\/script>)/msi', $html_source, -1, PREG_SPLIT_DELIM_CAPTURE); $compress = ''; foreach ($chunks as $c) { if (strtolower(substr($c, 0, 19)) == '<!--< nocompress>-->') { $c = substr($c, 19, strlen($c) - 19 - 20); $compress .= $ c; continue; } else if (strtolower(substr($c, 0, 12)) == '<nocompress>') { $c = substr($c, 12, strlen($c) - 12 - 13); $compress .= $ c; continue; } else if (strtolower(substr($c, 0, 4)) == '<pre' || strtolower(substr($c, 0, 9)) == '<textarea') { $compress .= $ c; continue; } else if (strtolower(substr($c, 0, 7)) == '<script' && strpos($c, '//') !=  false && (strpos($c, "\r") !==  false || strpos($c, "\n") !==  false)) { $tmps = preg_split('/(\r|\n)/ms', $c, -1, PREG_SPLIT_NO_EMPTY); $c = ''; foreach ($tmps as $tmp) { if (strpos($tmp, '//') !==  false) { if (substr(trim($tmp), 0, 2) == '//') { continue; } $chars = preg_split('//', $tmp, -1, PREG_SPLIT_NO_EMPTY); $is_quot = $is_apos = false; foreach ($chars as $key => $char) { if ($char == '"' && $chars[$key - 1] != '\\' && !$is_apos) { $is_quot = !$ is_quot; } else if ($char == '\'' && $chars[$key - 1] != '\\' && !$is_quot) { $is_apos = !$ is_apos; } else if ($char == '/' && $chars[$key + 1] == '/' && !$is_quot && !$is_apos) { $tmp = substr($tmp, 0, $key); break; } } } $c .= $ tmp; } } $c = preg_replace('/[\\n\\r\\t]+/', ' ', $c); $c = preg_replace('/\\s{2,}/', ' ', $c); $c = preg_replace('/>\\s</', '> <', $c); $c = preg_replace('/\\/\\*.*?\\*\\//i', '', $c); $c = preg_replace('/<!--[^!]*-->/', '', $c); $compress .= $ c; } return $compress; }

Put it in the function.php End of

Add Enable

Then open footer.php , add the following code at the end

 <? php $html_source = ob_get_contents();  ob_clean();  print compressHtml($html_source);  ob_end_flush(); ?>

Save the two files, and then overwrite the original file

Compatible with other codes

Of course, code compression may be incompatible with some plug-ins. You can use the following methods to modify plug-in code to ensure that these incompatible codes can run

 <!--< nocompress>--> Incompatibility code <!--</ nocompress>--> <nocompress> Incompatibility code </nocompress>

other

After all, some people do not use js in some plug-ins ; It's strange. As soon as the code is compressed, it will hang up directly, so we need to add a layer to determine whether to enable it

If you don't like the trouble, you can go directly from Github Update, and then open it in the appearance settings

If you modify the TpCache plug-in, you can judge whether to change it or not. Anyway, I changed it~ 😂

The background appearance settings are a bit messy. I'll sort them out next time~

Not much content, so far~

Responses
  1. Key

    How can I add a record number address?

    Reply
    1. Key
      @Key

      I found the problem of filing. Ha ha, I didn't see it clearly
      How to set the picture of the article?

      Reply
  2. 2333, found a good method (✪▽ ✪) fool like: when using cdn, open HTML compression. 2333, Devil's Cloud, can

    Reply
  3. mrjucn

    Ask the blogger to recommend a method of obtaining characteristic map by typecho. Recently, my WordPress theme was migrated to typecho. Everything has been done well, but the feature map has not found a suitable method.

    Reply
  4. Thanks for sharing,,,

    Reply
  5. ivz

    Hello, can the homepage be modified to display only articles under a certain category

    Reply
  6. idken

    Hello, I see that your theme is open source. I want to transplant it to Mweb

    Reply
    1. idken
      @idken

      Of course

      Reply