Warning, /webapps/ocs-webserver/library/lessphp/plessc is written in an unsupported language. File is not indexed.
0001 #!/usr/bin/env php 0002 <?php 0003 // Command line utility to compile LESS to STDOUT 0004 // Leaf Corcoran <leafot@gmail.com>, 2012 0005 0006 $exe = array_shift($argv); // remove filename 0007 0008 $HELP = <<<EOT 0009 Usage: $exe [options] input-file [output-file] 0010 0011 Options include: 0012 0013 -h, --help Show this message 0014 -v Print the version 0015 -f=format Set the output format, includes "default", "compressed" 0016 -c Keep /* */ comments in output 0017 -r Read from STDIN instead of input-file 0018 -w Watch input-file, and compile to output-file if it is changed 0019 -T Dump formatted parse tree 0020 -X Dump raw parse tree 0021 0022 0023 EOT; 0024 0025 $opts = getopt('hvrwncXTf:', array('help')); 0026 while (count($argv) > 0 && preg_match('/^-([-hvrwncXT]$|[f]=)/', $argv[0])) { 0027 array_shift($argv); 0028 } 0029 0030 function has() { 0031 global $opts; 0032 foreach (func_get_args() as $arg) { 0033 if (isset($opts[$arg])) return true; 0034 } 0035 return false; 0036 } 0037 0038 if (has("h", "help")) { 0039 exit($HELP); 0040 } 0041 0042 error_reporting(E_ALL); 0043 $path = realpath(dirname(__FILE__)).'/'; 0044 0045 require $path."lessc.inc.php"; 0046 0047 $VERSION = lessc::$VERSION; 0048 0049 $fa = "Fatal Error: "; 0050 function err($msg) { 0051 fwrite(STDERR, $msg."\n"); 0052 } 0053 0054 if (php_sapi_name() != "cli") { 0055 err($fa.$argv[0]." must be run in the command line."); 0056 exit(1); 0057 } 0058 0059 function make_less($fname = null) { 0060 global $opts; 0061 $l = new lessc($fname); 0062 0063 if (has("f")) { 0064 $format = $opts["f"]; 0065 if ($format != "default") $l->setFormatter($format); 0066 } 0067 0068 if (has("c")) { 0069 $l->setPreserveComments(true); 0070 } 0071 0072 return $l; 0073 } 0074 0075 function process($data, $import = null) { 0076 global $fa; 0077 0078 $l = make_less(); 0079 if ($import) $l->importDir = $import; 0080 0081 try { 0082 echo $l->parse($data); 0083 exit(0); 0084 } catch (exception $ex) { 0085 err($fa."\n".str_repeat('=', 20)."\n". 0086 $ex->getMessage()); 0087 exit(1); 0088 } 0089 } 0090 0091 if (has("v")) { 0092 exit($VERSION."\n"); 0093 } 0094 0095 if (has("r")) { 0096 if (!empty($argv)) { 0097 $data = $argv[0]; 0098 } else { 0099 $data = ""; 0100 while (!feof(STDIN)) { 0101 $data .= fread(STDIN, 8192); 0102 } 0103 } 0104 exit(process($data)); 0105 } 0106 0107 if (has("w")) { 0108 // need two files 0109 if (!is_file($in = array_shift($argv)) || 0110 null == $out = array_shift($argv)) 0111 { 0112 err($fa.$exe." -w infile outfile"); 0113 exit(1); 0114 } 0115 0116 echo "Watching ".$in. 0117 (has("n") ? ' with notifications' : ''). 0118 ", press Ctrl + c to exit.\n"; 0119 0120 $cache = $in; 0121 $last_action = 0; 0122 while (true) { 0123 clearstatcache(); 0124 0125 // check if anything has changed since last fail 0126 $updated = false; 0127 if (is_array($cache)) { 0128 foreach ($cache['files'] as $fname=>$_) { 0129 if (filemtime($fname) > $last_action) { 0130 $updated = true; 0131 break; 0132 } 0133 } 0134 } else $updated = true; 0135 0136 // try to compile it 0137 if ($updated) { 0138 $last_action = time(); 0139 0140 try { 0141 $cache = lessc::cexecute($cache); 0142 echo "Writing updated file: ".$out."\n"; 0143 if (!file_put_contents($out, $cache['compiled'])) { 0144 err($fa."Could not write to file ".$out); 0145 exit(1); 0146 } 0147 } catch (exception $ex) { 0148 echo "\nFatal Error:\n".str_repeat('=', 20)."\n". 0149 $ex->getMessage()."\n\n"; 0150 0151 if (has("n")) { 0152 `notify-send -u critical "compile failed" "{$ex->getMessage()}"`; 0153 } 0154 } 0155 } 0156 0157 sleep(1); 0158 } 0159 exit(0); 0160 } 0161 0162 if (!$fname = array_shift($argv)) { 0163 echo $HELP; 0164 exit(1); 0165 } 0166 0167 function dumpValue($node, $depth = 0) { 0168 if (is_object($node)) { 0169 $indent = str_repeat(" ", $depth); 0170 $out = array(); 0171 foreach ($node->props as $prop) { 0172 $out[] = $indent . dumpValue($prop, $depth + 1); 0173 } 0174 $out = implode("\n", $out); 0175 if (!empty($node->tags)) { 0176 $out = "+ ".implode(", ", $node->tags)."\n".$out; 0177 } 0178 return $out; 0179 } elseif (is_array($node)) { 0180 if (empty($node)) return "[]"; 0181 $type = $node[0]; 0182 if ($type == "block") 0183 return dumpValue($node[1], $depth); 0184 0185 $out = array(); 0186 foreach ($node as $value) { 0187 $out[] = dumpValue($value, $depth); 0188 } 0189 return "{ ".implode(", ", $out)." }"; 0190 } else { 0191 if (is_string($node) && preg_match("/[\s,]/", $node)) { 0192 return '"'.$node.'"'; 0193 } 0194 return $node; // normal value 0195 } 0196 } 0197 0198 0199 function stripValue($o, $toStrip) { 0200 if (is_array($o) || is_object($o)) { 0201 $isObject = is_object($o); 0202 $o = (array)$o; 0203 foreach ($toStrip as $removeKey) { 0204 if (!empty($o[$removeKey])) { 0205 $o[$removeKey] = "*stripped*"; 0206 } 0207 } 0208 0209 foreach ($o as $k => $v) { 0210 $o[$k] = stripValue($v, $toStrip); 0211 } 0212 0213 if ($isObject) { 0214 $o = (object)$o; 0215 } 0216 } 0217 0218 return $o; 0219 } 0220 0221 function dumpWithoutParent($o, $alsoStrip=array()) { 0222 $toStrip = array_merge(array("parent"), $alsoStrip); 0223 print_r(stripValue($o, $toStrip)); 0224 } 0225 0226 try { 0227 $less = make_less($fname); 0228 if (has("T", "X")) { 0229 $parser = new lessc_parser($less, $fname); 0230 $tree = $parser->parse(file_get_contents($fname)); 0231 if (has("X")) 0232 $out = print_r($tree, 1); 0233 else 0234 $out = dumpValue($tree)."\n"; 0235 } else { 0236 $out = $less->parse(); 0237 } 0238 0239 if (!$fout = array_shift($argv)) { 0240 echo $out; 0241 } else { 0242 file_put_contents($fout, $out); 0243 } 0244 0245 } catch (exception $ex) { 0246 err($fa.$ex->getMessage()); 0247 exit(1); 0248 }