Skip to content

Commit dbca8da

Browse files
committed
fixes leafo#395 - bin/pscss: add --iso8859-1 option; defaults to utf-8 (slower but more compatible with ruby scss)
1 parent 4ac664e commit dbca8da

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

bin/pscss

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ $changeDir = false;
3131
$debugInfo = false;
3232
$lineNumbers = false;
3333
$ignoreErrors = false;
34+
$encoding = false;
3435

3536
/**
3637
* Parse argument
@@ -73,6 +74,7 @@ Options include:
7374
--debug-info Annotate selectors with CSS referring to the source file and line number
7475
-f=format Set the output format (compact, compressed, crunched, expanded, or nested)
7576
-i=path Set import path
77+
--iso8859-1 Use iso8859-1 encoding instead of utf-8 (default utf-8)
7678
--line-numbers Annotate selectors with comments referring to the source file and line number
7779
-p=precision Set decimal number precision (default 5)
7880
-T Dump formatted parse tree
@@ -96,6 +98,11 @@ EOT;
9698
continue;
9799
}
98100

101+
if ($argv[$i] === '--iso8859-1') {
102+
$encoding = 'iso8859-1';
103+
continue;
104+
}
105+
99106
if ($argv[$i] === '--line-numbers' || $argv[$i] === '--line-comments') {
100107
$lineNumbers = true;
101108
continue;
@@ -186,6 +193,10 @@ if ($style) {
186193
$scss->setFormatter('Leafo\\ScssPhp\\Formatter\\' . ucfirst($style));
187194
}
188195

196+
if ($encoding) {
197+
$scss->setEncoding($encoding);
198+
}
199+
189200
echo $scss->compile($data, $inputFile);
190201

191202
if ($changeDir) {

src/Compiler.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class Compiler
117117
'global-variable-shadowing' => false,
118118
);
119119

120+
protected $encoding = null;
120121
protected $lineNumberStyle = null;
121122

122123
protected $formatter = 'Leafo\ScssPhp\Formatter\Nested';
@@ -208,7 +209,7 @@ public function compile($code, $path = null)
208209
*/
209210
private function parserFactory($path)
210211
{
211-
$parser = new Parser($path, count($this->sourceNames));
212+
$parser = new Parser($path, count($this->sourceNames), $this->encoding);
212213

213214
$this->sourceNames[] = $path;
214215
$this->addParsedFile($path);
@@ -3239,6 +3240,18 @@ public function findImport($url)
32393240
return null;
32403241
}
32413242

3243+
/**
3244+
* Set encoding
3245+
*
3246+
* @api
3247+
*
3248+
* @param string $encoding
3249+
*/
3250+
public function setEncoding($encoding)
3251+
{
3252+
$this->encoding = $encoding;
3253+
}
3254+
32423255
/**
32433256
* Ignore errors?
32443257
*

src/Parser.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class Parser
6262
private $inParens;
6363
private $eatWhiteDefault;
6464
private $buffer;
65+
private $utf8;
6566

6667
/**
6768
* Constructor
@@ -70,12 +71,14 @@ class Parser
7071
*
7172
* @param string $sourceName
7273
* @param integer $sourceIndex
74+
* @param string $encoding
7375
*/
74-
public function __construct($sourceName, $sourceIndex = 0)
76+
public function __construct($sourceName, $sourceIndex = 0, $encoding = 'utf-8')
7577
{
7678
$this->sourceName = $sourceName ?: '(stdin)';
7779
$this->sourceIndex = $sourceIndex;
7880
$this->charset = null;
81+
$this->utf8 = ! $encoding || strtolower($encoding) === 'utf-8';
7982

8083
if (empty(self::$operatorPattern)) {
8184
self::$operatorPattern = '([*\/%+-]|[!=]\=|\>\=?|\<\=\>|\<\=?|and|or)';
@@ -85,7 +88,9 @@ public function __construct($sourceName, $sourceIndex = 0)
8588
$commentMultiRight = '\*\/';
8689

8790
self::$commentPattern = $commentMultiLeft . '.*?' . $commentMultiRight;
88-
self::$whitePattern = '/' . $commentSingle . '[^\n]*\s*|(' . self::$commentPattern . ')\s*|\s+/AisuS';
91+
self::$whitePattern = $this->utf8
92+
? '/' . $commentSingle . '[^\n]*\s*|(' . self::$commentPattern . ')\s*|\s+/AisuS'
93+
: '/' . $commentSingle . '[^\n]*\s*|(' . self::$commentPattern . ')\s*|\s+/AisS';
8994
}
9095
}
9196

@@ -762,7 +767,7 @@ protected function peek($regex, &$out, $from = null)
762767
$from = $this->count;
763768
}
764769

765-
$r = '/' . $regex . '/Aisu';
770+
$r = $this->utf8 ? '/' . $regex . '/Aisu' : '/' . $regex . '/Ais';
766771
$result = preg_match($r, $this->buffer, $out, null, $from);
767772

768773
return $result;
@@ -842,7 +847,7 @@ protected function match($regex, &$out, $eatWhitespace = null)
842847
$eatWhitespace = $this->eatWhiteDefault;
843848
}
844849

845-
$r = '/' . $regex . '/Aisu';
850+
$r = $this->utf8 ? '/' . $regex . '/Aisu' : '/' . $regex . '/Ais';
846851

847852
if (preg_match($r, $this->buffer, $out, null, $this->count)) {
848853
$this->count += strlen($out[0]);
@@ -2235,7 +2240,9 @@ protected function variable(&$out)
22352240
protected function keyword(&$word, $eatWhitespace = null)
22362241
{
22372242
if ($this->match(
2238-
'(([\pL\w_\-\*!"\']|[\\\\].)([\pL\w\-_"\']|[\\\\].)*)',
2243+
$this->utf8
2244+
? '(([\pL\w_\-\*!"\']|[\\\\].)([\pL\w\-_"\']|[\\\\].)*)'
2245+
: '(([\w_\-\*!"\']|[\\\\].)([\w\-_"\']|[\\\\].)*)',
22392246
$m,
22402247
$eatWhitespace
22412248
)) {
@@ -2256,7 +2263,12 @@ protected function keyword(&$word, $eatWhitespace = null)
22562263
*/
22572264
protected function placeholder(&$placeholder)
22582265
{
2259-
if ($this->match('([\pL\w\-_]+|#[{][$][\pL\w\-_]+[}])', $m)) {
2266+
if ($this->match(
2267+
$this->utf8
2268+
? '([\pL\w\-_]+|#[{][$][\pL\w\-_]+[}])'
2269+
: '([\w\-_]+|#[{][$][\w\-_]+[}])',
2270+
$m
2271+
)) {
22602272
$placeholder = $m[1];
22612273

22622274
return true;

0 commit comments

Comments
 (0)