From: Nicolas Boisselier Date: Sun, 15 Jan 2017 01:02:11 +0000 (+0000) Subject: Bed X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=51f85948b68a7cf31c9c6226d71be147c1133485;p=nb.git Bed --- diff --git a/bin/nb-commit b/bin/nb-commit index 82f02194..b0f4fa2a 100755 --- a/bin/nb-commit +++ b/bin/nb-commit @@ -10,6 +10,8 @@ set -e cd $NB_ROOT || exit +shell_replace -i $(grep -rIEl '^\s*.{1,3}SHELL_REPLACE') . /dev/null + for f in www/*/update.sh; do [ -x "$f" ] || continue $f diff --git a/etc/profile.d/functions b/etc/profile.d/functions index 589add99..fe3b9ccb 100644 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -736,13 +736,18 @@ while (@_) { shell_replace() { perl -ne ' -if (/^.{1,3}>SHELL_REPLACE (.*)$/ .. /^.{1,3}SHELL_REPLACE (.*)$/ .. /^\s*.{1,3}; print; print map{s/^/$idt/;$_} `$2`; $ch = 1; +} else {$i=0;print +} +END { + warn $ARGV if 0 and $INPLACE_EDIT and $ch; +} ' $@ } diff --git a/etc/profile.d/php.sh b/etc/profile.d/php.sh index 16f96910..1ecf585c 100644 --- a/etc/profile.d/php.sh +++ b/etc/profile.d/php.sh @@ -9,10 +9,11 @@ php425() { which php > /dev/null || return 0 php_check() { - php --define error_reporting=22519 --define display_errors=1 --define log_errors=1 --define html_errors=0 $@; + #=SHELL_REPLACE echo $(dbq t=mime id=php -cut 3) \$@ + php --define error_reporting=22519 --define display_errors=1 --define log_errors=1 --define html_errors=0 $@ } php_run() { local code="$1"; shift - php -f $NB_ROOT/lib/php/db.php -r "require '$NB_ROOT/lib/php/page.php';$code;" $@ + php -r "require '$NB_ROOT/lib/php/cmd.php';$code;" $@ } diff --git a/lib/php/cmd.php b/lib/php/cmd.php new file mode 100644 index 00000000..aa8926bc --- /dev/null +++ b/lib/php/cmd.php @@ -0,0 +1,8 @@ + diff --git a/lib/php/jsmin.php b/lib/php/jsmin.php new file mode 100644 index 00000000..2f3bcc75 --- /dev/null +++ b/lib/php/jsmin.php @@ -0,0 +1,291 @@ + + * @copyright 2002 Douglas Crockford (jsmin.c) + * @copyright 2008 Ryan Grove (PHP port) + * @license http://opensource.org/licenses/mit-license.php MIT License + * @version 1.1.1 (2008-03-02) + * @link http://code.google.com/p/jsmin-php/ + */ + +class JSMin { + const ORD_LF = 10; + const ORD_SPACE = 32; + + protected $a = ''; + protected $b = ''; + protected $input = ''; + protected $inputIndex = 0; + protected $inputLength = 0; + protected $lookAhead = null; + protected $output = ''; + + // -- Public Static Methods -------------------------------------------------- + + public static function minify($js) { + $jsmin = new JSMin($js); + return $jsmin->min(); + } + + // -- Public Instance Methods ------------------------------------------------ + + public function __construct($input) { + $this->input = str_replace("\r\n", "\n", $input); + $this->inputLength = strlen($this->input); + } + + // -- Protected Instance Methods --------------------------------------------- + + protected function action($d) { + switch($d) { + case 1: + $this->output .= $this->a; + + case 2: + $this->a = $this->b; + + if ($this->a === "'" || $this->a === '"') { + for (;;) { + $this->output .= $this->a; + $this->a = $this->get(); + + if ($this->a === $this->b) { + break; + } + + if (ord($this->a) <= self::ORD_LF) { + throw new JSMinException('Unterminated string literal.'); + } + + if ($this->a === '\\') { + $this->output .= $this->a; + $this->a = $this->get(); + } + } + } + + case 3: + $this->b = $this->next(); + + if ($this->b === '/' && ( + $this->a === '(' || $this->a === ',' || $this->a === '=' || + $this->a === ':' || $this->a === '[' || $this->a === '!' || + $this->a === '&' || $this->a === '|' || $this->a === '?')) { + + $this->output .= $this->a . $this->b; + + for (;;) { + $this->a = $this->get(); + + if ($this->a === '/') { + break; + } elseif ($this->a === '\\') { + $this->output .= $this->a; + $this->a = $this->get(); + } elseif (ord($this->a) <= self::ORD_LF) { + throw new JSMinException('Unterminated regular expression '. + 'literal.'); + } + + $this->output .= $this->a; + } + + $this->b = $this->next(); + } + } + } + + protected function get() { + $c = $this->lookAhead; + $this->lookAhead = null; + + if ($c === null) { + if ($this->inputIndex < $this->inputLength) { + $c = $this->input[$this->inputIndex]; + $this->inputIndex += 1; + } else { + $c = null; + } + } + + if ($c === "\r") { + return "\n"; + } + + if ($c === null || $c === "\n" || ord($c) >= self::ORD_SPACE) { + return $c; + } + + return ' '; + } + + protected function isAlphaNum($c) { + return ord($c) > 126 || $c === '\\' || preg_match('/^[\w\$]$/', $c) === 1; + } + + protected function min() { + $this->a = "\n"; + $this->action(3); + + while ($this->a !== null) { + switch ($this->a) { + case ' ': + if ($this->isAlphaNum($this->b)) { + $this->action(1); + } else { + $this->action(2); + } + break; + + case "\n": + switch ($this->b) { + case '{': + case '[': + case '(': + case '+': + case '-': + $this->action(1); + break; + + case ' ': + $this->action(3); + break; + + default: + if ($this->isAlphaNum($this->b)) { + $this->action(1); + } + else { + $this->action(2); + } + } + break; + + default: + switch ($this->b) { + case ' ': + if ($this->isAlphaNum($this->a)) { + $this->action(1); + break; + } + + $this->action(3); + break; + + case "\n": + switch ($this->a) { + case '}': + case ']': + case ')': + case '+': + case '-': + case '"': + case "'": + $this->action(1); + break; + + default: + if ($this->isAlphaNum($this->a)) { + $this->action(1); + } + else { + $this->action(3); + } + } + break; + + default: + $this->action(1); + break; + } + } + } + + return $this->output; + } + + protected function next() { + $c = $this->get(); + + if ($c === '/') { + switch($this->peek()) { + case '/': + for (;;) { + $c = $this->get(); + + if (ord($c) <= self::ORD_LF) { + return $c; + } + } + + case '*': + $this->get(); + + for (;;) { + switch($this->get()) { + case '*': + if ($this->peek() === '/') { + $this->get(); + return ' '; + } + break; + + case null: + throw new JSMinException('Unterminated comment.'); + } + } + + default: + return $c; + } + } + + return $c; + } + + protected function peek() { + $this->lookAhead = $this->get(); + return $this->lookAhead; + } +} + +// -- Exceptions --------------------------------------------------------------- +class JSMinException extends Exception {} +?> \ No newline at end of file diff --git a/lib/php/nb.php b/lib/php/nb.php index 54749a0d..5ff972aa 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -911,8 +911,61 @@ class NB { 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 !'; @@ -926,4 +979,5 @@ echo '' ."enc : $enc\n" ."value: ".nb::decrypt($key,$enc) ."\n" ; +*/ ?> diff --git a/lib/php/page.php b/lib/php/page.php index 188527a9..7f323086 100644 --- a/lib/php/page.php +++ b/lib/php/page.php @@ -1,5 +1,5 @@