Last active
December 30, 2015 05:59
-
-
Save GreatPotato/7786746 to your computer and use it in GitHub Desktop.
Minifys and escapes a HTML file ready for deploying via JS
This file contains hidden or 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 | |
| class Ebook { | |
| public $input = 'ebook.html'; | |
| public $output = 'ebook.min.html'; | |
| public function __construct($array) | |
| { | |
| foreach($array as $k => $v) | |
| $this->$k = $v; | |
| } | |
| public function generate_file() | |
| { | |
| $content = $this->get_content(); | |
| $content = $this->minify_escape($content); | |
| file_put_contents($this->output, $content); | |
| } | |
| private function get_content() | |
| { | |
| return file_get_contents($this->input); | |
| } | |
| private function minify_escape($content) | |
| { | |
| $content = preg_replace( | |
| array( | |
| '/ {2,}/', | |
| '/<!--.*?-->|\t|(?:\r?\n[ \t]*)+/s' | |
| ), | |
| array( | |
| ' ', | |
| '' | |
| ), | |
| $content | |
| ); | |
| $content = str_replace( | |
| array( | |
| '"', | |
| '<script', | |
| '</script>', | |
| ), | |
| array( | |
| '\"', | |
| '<scr"+"ipt', | |
| '<"+"/script>', | |
| ), | |
| $content | |
| ); | |
| return $content; | |
| } | |
| } | |
| $ebook = new Ebook(array('ebook.html', 'ebook.min.html')); | |
| $ebook->generate_file(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment