pscss 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * SCSSPHP
  5. *
  6. * @copyright 2012-2015 Leaf Corcoran
  7. *
  8. * @license http://opensource.org/licenses/MIT MIT
  9. *
  10. * @link http://leafo.github.io/scssphp
  11. */
  12. error_reporting(E_ALL);
  13. if (version_compare(PHP_VERSION, '5.4') < 0) {
  14. die('Requires PHP 5.4 or above');
  15. }
  16. include __DIR__ . '/../scss.inc.php';
  17. use Leafo\ScssPhp\Compiler;
  18. use Leafo\ScssPhp\Parser;
  19. use Leafo\ScssPhp\Version;
  20. $style = null;
  21. $loadPaths = null;
  22. $precision = null;
  23. $dumpTree = false;
  24. $inputFile = null;
  25. $changeDir = false;
  26. $debugInfo = false;
  27. $lineNumbers = false;
  28. $ignoreErrors = false;
  29. $encoding = false;
  30. /**
  31. * Parse argument
  32. *
  33. * @param integer $i
  34. * @param array $options
  35. *
  36. * @return string|null
  37. */
  38. function parseArgument(&$i, $options) {
  39. global $argc;
  40. global $argv;
  41. if (! preg_match('/^(?:' . implode('|', (array) $options) . ')=?(.*)/', $argv[$i], $matches)) {
  42. return;
  43. }
  44. if (strlen($matches[1])) {
  45. return $matches[1];
  46. }
  47. if ($i + 1 < $argc) {
  48. $i++;
  49. return $argv[$i];
  50. }
  51. }
  52. for ($i = 1; $i < $argc; $i++) {
  53. if ($argv[$i] === '-h' || $argv[$i] === '--help') {
  54. $exe = $argv[0];
  55. $HELP = <<<EOT
  56. Usage: $exe [options] [input-file]
  57. Options include:
  58. -h, --help Show this message
  59. --continue-on-error Continue compilation (as best as possible) when error encountered
  60. --debug-info Annotate selectors with CSS referring to the source file and line number
  61. -f=format Set the output format (compact, compressed, crunched, expanded, or nested)
  62. -i=path Set import path
  63. --iso8859-1 Use iso8859-1 encoding instead of utf-8 (default utf-8)
  64. --line-numbers Annotate selectors with comments referring to the source file and line number
  65. -p=precision Set decimal number precision (default 5)
  66. -T Dump formatted parse tree
  67. -v, --version Print the version
  68. EOT;
  69. exit($HELP);
  70. }
  71. if ($argv[$i] === '-v' || $argv[$i] === '--version') {
  72. exit(Version::VERSION . "\n");
  73. }
  74. if ($argv[$i] === '--continue-on-error') {
  75. $ignoreErrors = true;
  76. continue;
  77. }
  78. if ($argv[$i] === '--debug-info') {
  79. $debugInfo = true;
  80. continue;
  81. }
  82. if ($argv[$i] === '--iso8859-1') {
  83. $encoding = 'iso8859-1';
  84. continue;
  85. }
  86. if ($argv[$i] === '--line-numbers' || $argv[$i] === '--line-comments') {
  87. $lineNumbers = true;
  88. continue;
  89. }
  90. if ($argv[$i] === '-T') {
  91. $dumpTree = true;
  92. continue;
  93. }
  94. $value = parseArgument($i, array('-f', '--style'));
  95. if (isset($value)) {
  96. $style = $value;
  97. continue;
  98. }
  99. $value = parseArgument($i, array('-i', '--load_paths'));
  100. if (isset($value)) {
  101. $loadPaths = $value;
  102. continue;
  103. }
  104. $value = parseArgument($i, array('-p', '--precision'));
  105. if (isset($value)) {
  106. $precision = $value;
  107. continue;
  108. }
  109. if (file_exists($argv[$i])) {
  110. $inputFile = $argv[$i];
  111. continue;
  112. }
  113. }
  114. if ($inputFile) {
  115. $data = file_get_contents($inputFile);
  116. $newWorkingDir = dirname(realpath($inputFile));
  117. $oldWorkingDir = getcwd();
  118. if ($oldWorkingDir !== $newWorkingDir) {
  119. $changeDir = chdir($newWorkingDir);
  120. $inputFile = basename($inputFile);
  121. }
  122. } else {
  123. $data = '';
  124. while (! feof(STDIN)) {
  125. $data .= fread(STDIN, 8192);
  126. }
  127. }
  128. if ($dumpTree) {
  129. $parser = new Parser($inputFile);
  130. print_r(json_decode(json_encode($parser->parse($data)), true));
  131. exit();
  132. }
  133. $scss = new Compiler();
  134. if ($debugInfo && $inputFile) {
  135. $scss->setLineNumberStyle(Compiler::DEBUG_INFO);
  136. }
  137. if ($lineNumbers && $inputFile) {
  138. $scss->setLineNumberStyle(Compiler::LINE_COMMENTS);
  139. }
  140. if ($ignoreErrors) {
  141. $scss->setIgnoreErrors($ignoreErrors);
  142. }
  143. if ($loadPaths) {
  144. $scss->setImportPaths(explode(PATH_SEPARATOR, $loadPaths));
  145. }
  146. if ($precision) {
  147. $scss->setNumberPrecision($precision);
  148. }
  149. if ($style) {
  150. $scss->setFormatter('Leafo\\ScssPhp\\Formatter\\' . ucfirst($style));
  151. }
  152. if ($encoding) {
  153. $scss->setEncoding($encoding);
  154. }
  155. echo $scss->compile($data, $inputFile);
  156. if ($changeDir) {
  157. chdir($oldWorkingDir);
  158. }