--- /dev/null
+<?php\r
+/**\r
+ * jsmin.php - PHP implementation of Douglas Crockford's JSMin.\r
+ *\r
+ * This is pretty much a direct port of jsmin.c to PHP with just a few\r
+ * PHP-specific performance tweaks. Also, whereas jsmin.c reads from stdin and\r
+ * outputs to stdout, this library accepts a string as input and returns another\r
+ * string as output.\r
+ *\r
+ * PHP 5 or higher is required.\r
+ *\r
+ * Permission is hereby granted to use this version of the library under the\r
+ * same terms as jsmin.c, which has the following license:\r
+ *\r
+ * --\r
+ * Copyright (c) 2002 Douglas Crockford (www.crockford.com)\r
+ *\r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
+ * this software and associated documentation files (the "Software"), to deal in\r
+ * the Software without restriction, including without limitation the rights to\r
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies\r
+ * of the Software, and to permit persons to whom the Software is furnished to do\r
+ * so, subject to the following conditions:\r
+ *\r
+ * The above copyright notice and this permission notice shall be included in all\r
+ * copies or substantial portions of the Software.\r
+ *\r
+ * The Software shall be used for Good, not Evil.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
+ * SOFTWARE.\r
+ * --\r
+ *\r
+ * @package JSMin\r
+ * @author Ryan Grove <ryan@wonko.com>\r
+ * @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)\r
+ * @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP port)\r
+ * @license http://opensource.org/licenses/mit-license.php MIT License\r
+ * @version 1.1.1 (2008-03-02)\r
+ * @link http://code.google.com/p/jsmin-php/\r
+ */\r
+\r
+class JSMin {\r
+ const ORD_LF = 10;\r
+ const ORD_SPACE = 32;\r
+\r
+ protected $a = '';\r
+ protected $b = '';\r
+ protected $input = '';\r
+ protected $inputIndex = 0;\r
+ protected $inputLength = 0;\r
+ protected $lookAhead = null;\r
+ protected $output = '';\r
+\r
+ // -- Public Static Methods --------------------------------------------------\r
+\r
+ public static function minify($js) {\r
+ $jsmin = new JSMin($js);\r
+ return $jsmin->min();\r
+ }\r
+\r
+ // -- Public Instance Methods ------------------------------------------------\r
+\r
+ public function __construct($input) {\r
+ $this->input = str_replace("\r\n", "\n", $input);\r
+ $this->inputLength = strlen($this->input);\r
+ }\r
+\r
+ // -- Protected Instance Methods ---------------------------------------------\r
+\r
+ protected function action($d) {\r
+ switch($d) {\r
+ case 1:\r
+ $this->output .= $this->a;\r
+\r
+ case 2:\r
+ $this->a = $this->b;\r
+\r
+ if ($this->a === "'" || $this->a === '"') {\r
+ for (;;) {\r
+ $this->output .= $this->a;\r
+ $this->a = $this->get();\r
+\r
+ if ($this->a === $this->b) {\r
+ break;\r
+ }\r
+\r
+ if (ord($this->a) <= self::ORD_LF) {\r
+ throw new JSMinException('Unterminated string literal.');\r
+ }\r
+\r
+ if ($this->a === '\\') {\r
+ $this->output .= $this->a;\r
+ $this->a = $this->get();\r
+ }\r
+ }\r
+ }\r
+\r
+ case 3:\r
+ $this->b = $this->next();\r
+\r
+ if ($this->b === '/' && (\r
+ $this->a === '(' || $this->a === ',' || $this->a === '=' ||\r
+ $this->a === ':' || $this->a === '[' || $this->a === '!' ||\r
+ $this->a === '&' || $this->a === '|' || $this->a === '?')) {\r
+\r
+ $this->output .= $this->a . $this->b;\r
+\r
+ for (;;) {\r
+ $this->a = $this->get();\r
+\r
+ if ($this->a === '/') {\r
+ break;\r
+ } elseif ($this->a === '\\') {\r
+ $this->output .= $this->a;\r
+ $this->a = $this->get();\r
+ } elseif (ord($this->a) <= self::ORD_LF) {\r
+ throw new JSMinException('Unterminated regular expression '.\r
+ 'literal.');\r
+ }\r
+\r
+ $this->output .= $this->a;\r
+ }\r
+\r
+ $this->b = $this->next();\r
+ }\r
+ }\r
+ }\r
+\r
+ protected function get() {\r
+ $c = $this->lookAhead;\r
+ $this->lookAhead = null;\r
+\r
+ if ($c === null) {\r
+ if ($this->inputIndex < $this->inputLength) {\r
+ $c = $this->input[$this->inputIndex];\r
+ $this->inputIndex += 1;\r
+ } else {\r
+ $c = null;\r
+ }\r
+ }\r
+\r
+ if ($c === "\r") {\r
+ return "\n";\r
+ }\r
+\r
+ if ($c === null || $c === "\n" || ord($c) >= self::ORD_SPACE) {\r
+ return $c;\r
+ }\r
+\r
+ return ' ';\r
+ }\r
+\r
+ protected function isAlphaNum($c) {\r
+ return ord($c) > 126 || $c === '\\' || preg_match('/^[\w\$]$/', $c) === 1;\r
+ }\r
+\r
+ protected function min() {\r
+ $this->a = "\n";\r
+ $this->action(3);\r
+\r
+ while ($this->a !== null) {\r
+ switch ($this->a) {\r
+ case ' ':\r
+ if ($this->isAlphaNum($this->b)) {\r
+ $this->action(1);\r
+ } else {\r
+ $this->action(2);\r
+ }\r
+ break;\r
+\r
+ case "\n":\r
+ switch ($this->b) {\r
+ case '{':\r
+ case '[':\r
+ case '(':\r
+ case '+':\r
+ case '-':\r
+ $this->action(1);\r
+ break;\r
+\r
+ case ' ':\r
+ $this->action(3);\r
+ break;\r
+\r
+ default:\r
+ if ($this->isAlphaNum($this->b)) {\r
+ $this->action(1);\r
+ }\r
+ else {\r
+ $this->action(2);\r
+ }\r
+ }\r
+ break;\r
+\r
+ default:\r
+ switch ($this->b) {\r
+ case ' ':\r
+ if ($this->isAlphaNum($this->a)) {\r
+ $this->action(1);\r
+ break;\r
+ }\r
+\r
+ $this->action(3);\r
+ break;\r
+\r
+ case "\n":\r
+ switch ($this->a) {\r
+ case '}':\r
+ case ']':\r
+ case ')':\r
+ case '+':\r
+ case '-':\r
+ case '"':\r
+ case "'":\r
+ $this->action(1);\r
+ break;\r
+\r
+ default:\r
+ if ($this->isAlphaNum($this->a)) {\r
+ $this->action(1);\r
+ }\r
+ else {\r
+ $this->action(3);\r
+ }\r
+ }\r
+ break;\r
+\r
+ default:\r
+ $this->action(1);\r
+ break;\r
+ }\r
+ }\r
+ }\r
+\r
+ return $this->output;\r
+ }\r
+\r
+ protected function next() {\r
+ $c = $this->get();\r
+\r
+ if ($c === '/') {\r
+ switch($this->peek()) {\r
+ case '/':\r
+ for (;;) {\r
+ $c = $this->get();\r
+\r
+ if (ord($c) <= self::ORD_LF) {\r
+ return $c;\r
+ }\r
+ }\r
+\r
+ case '*':\r
+ $this->get();\r
+\r
+ for (;;) {\r
+ switch($this->get()) {\r
+ case '*':\r
+ if ($this->peek() === '/') {\r
+ $this->get();\r
+ return ' ';\r
+ }\r
+ break;\r
+\r
+ case null:\r
+ throw new JSMinException('Unterminated comment.');\r
+ }\r
+ }\r
+\r
+ default:\r
+ return $c;\r
+ }\r
+ }\r
+\r
+ return $c;\r
+ }\r
+\r
+ protected function peek() {\r
+ $this->lookAhead = $this->get();\r
+ return $this->lookAhead;\r
+ }\r
+}\r
+\r
+// -- Exceptions ---------------------------------------------------------------\r
+class JSMinException extends Exception {}\r
+?>
\ No newline at end of file
return $_txt2md->text($txt);
}
+ public static function minify_js($code) {
+ if (strpos($code,'/') === 0) $code = file_get_contents($code);
+ require_once(NB_ROOT.'/lib/php/jsmin.php');
+ return JSMin::minify($code);
+ }
+
+ public static function minify_css($code) {
+ if (strpos($code,'/') === 0) $code = file_get_contents($code);
+
+ #$code = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $code);
+
+ // comments /* */
+ $code = preg_replace(',/\*.*?\*/,s','',$code);
+
+ // new lines
+ $code = preg_replace('/[\r\n]+/','',$code);
+
+ // spaces
+ #$code = preg_replace('/\s*()\s*/','$1',$code);
+
+ // selectors spaces (eg: "a > p")
+ $code = preg_replace_callback('/(^|\s*\})\s*([^\}]+)\s*(\{)\s*/',function($m){
+
+ array_shift($m);
+ foreach ($m as $k=>$v) {
+ #$m[$k] = str_replace(' ','',$m[$k]);
+ #$m[$k] = trim($m[$k]);
+ $m[$k] = preg_replace('/\s*(\W)\s*/','$1',$m[$k]);
+ #$m[$k] = ':'.$m[$k].':';
+ }
+ #bye($m);
+ #debug($m);
+ return join('',$m);
+ },$code);
+
+ // Property
+ $code = preg_replace_callback('/\s*(\{|;)\s*([\w-]+)\s*(:)\s*/',function($m){
+
+ array_shift($m);
+ return join('',$m);
+ #bye($m);
+ foreach ($m as $k=>$v) {
+ $m[$k] = trim($m[$k]);
+ }
+ return join('',$m);
+ },$code);
+
+ // redudant
+ $code = str_replace(':}','}',$code);
+
+ return $code;
+ }
} # < Class
+/*
return;
if (!count($argv) or (string)$argv[1] != 'test') return;
$value = 'Hello World !';
."enc : $enc\n"
."value: ".nb::decrypt($key,$enc) ."\n"
;
+*/
?>