fitztrev / laravel-html-minify

Minifies the HTML output of Laravel 4 applications
https://github.com/fitztrev/laravel-html-minify/wiki/Laravel-5---5.1-HTML-Minifying
MIT License
414 stars 76 forks source link

Minify any HTML string #32

Closed andrewdworn closed 9 years ago

andrewdworn commented 10 years ago

A have some HTML code stored in database, so it doesn't get minified by this extension. I minify those separately, so I ended up creating a function for this purpose in my controller, which basically includes the regex part:


    public static function compileMinify($value) {
        $replace = array(
            '/<!--[^\[](.*?)[^\]]-->/s' => '',
            "/<\?php/" => '<?php ',
            "/\n([\S])/" => ' $1',
            "/\r/" => '',
            "/\n/" => '',
            "/\t/" => ' ',
            "/ +/" => ' ',
        );

        return preg_replace(
            array_keys($replace), array_values($replace), $value
        );
    }
 

It would be much nicer, if I could access the same piece of code from the html-minify extension. It only requires a bit change in the LaravelHtmlMinifyCompiler class, something like this:


    protected function compileMinify($value)
    {
        if ($this->shouldMinify($value)) {
            return self::compileMinifyCore($value);
        } else {
            return $value;
        }

    }

   public static function compileMinifyCore($value)
    {
        $replace = array(
            '/<!--[^\[](.*?)[^\]]-->/s' => '',
            "/<\?php/"                  => '<?php ',
            "/\n([\S])/"                => '$1',
            "/\r/"                      => '',
            "/\n/"                      => '',
            "/\t/"                      => ' ',
            "/ +/"                      => ' ',
        );

        return preg_replace(
            array_keys($replace), array_values($replace), $value
        );

    }
 

So in this way, I could use LaravelHtmlMinify::compileMinifyCore($html) for my 'out of blade' data compression.

fitztrev commented 10 years ago

@andrewdworn Good idea. Thanks for the suggestion.

GrahamCampbell commented 10 years ago

I have not copied your code. My package is completely written from scratch. Only the regex is the same in that single file.

GrahamCampbell commented 10 years ago

I am only linking people else where if this package doesn't provide what they are asking for. I'm sorry. I'll stop posting links, and I'll add a comment in my blade compiler class with a link back to your regex. Also, the fact that my shouldMinify method even exists, or is similar is pure coincidence. That's a more recent refractor I made.

GrahamCampbell commented 10 years ago

There we go: https://github.com/GrahamCampbell/Laravel-HTMLMin/commit/605ea736c3ad36e157c1be4db94fd3ba35394156. Again sorry about that @fitztrev. If that notice does not meet your attribution requirements, give me a buzz and I'll change it to whatever you want.

carcinocron commented 10 years ago

:+1: