SassyJSON 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +18 -18
  3. data/README.md +112 -112
  4. data/lib/JsonImporter.rb +228 -228
  5. data/lib/SassyJSON.rb +12 -14
  6. data/stylesheets/SassyJSON.scss +5 -5
  7. data/stylesheets/decode/api/_json.scss +26 -26
  8. data/stylesheets/decode/decode.scss +28 -28
  9. data/stylesheets/decode/helpers/all/_throw.scss +11 -11
  10. data/stylesheets/decode/helpers/all/_value.scss +49 -49
  11. data/stylesheets/decode/helpers/color/_color.scss +50 -50
  12. data/stylesheets/decode/helpers/color/_get-color-value.scss +22 -22
  13. data/stylesheets/decode/helpers/color/_hex-to-dec.scss +19 -19
  14. data/stylesheets/decode/helpers/color/_hex.scss +39 -39
  15. data/stylesheets/decode/helpers/color/_hsl.scss +46 -46
  16. data/stylesheets/decode/helpers/color/_rgb.scss +46 -46
  17. data/stylesheets/decode/helpers/map/_consume.scss +33 -33
  18. data/stylesheets/decode/helpers/number/_find-digits.scss +39 -39
  19. data/stylesheets/decode/helpers/number/_find-exponent.scss +51 -51
  20. data/stylesheets/decode/helpers/number/_find-integer.scss +37 -37
  21. data/stylesheets/decode/helpers/number/_pow.scss +22 -22
  22. data/stylesheets/decode/helpers/string/_find-ending-quote.scss +57 -57
  23. data/stylesheets/decode/helpers/string/_length.scss +42 -42
  24. data/stylesheets/decode/helpers/string/_strip-token.scss +16 -16
  25. data/stylesheets/decode/types/_bool.scss +40 -40
  26. data/stylesheets/decode/types/_list.scss +54 -54
  27. data/stylesheets/decode/types/_map.scss +78 -78
  28. data/stylesheets/decode/types/_null.scss +19 -19
  29. data/stylesheets/decode/types/_number.scss +63 -63
  30. data/stylesheets/decode/types/_string.scss +41 -41
  31. data/stylesheets/encode/api/_json.scss +17 -17
  32. data/stylesheets/encode/encode.scss +17 -17
  33. data/stylesheets/encode/helpers/_quote.scss +9 -9
  34. data/stylesheets/encode/mixins/_json.scss +36 -36
  35. data/stylesheets/encode/types/_bool.scss +9 -9
  36. data/stylesheets/encode/types/_color.scss +9 -9
  37. data/stylesheets/encode/types/_list.scss +13 -13
  38. data/stylesheets/encode/types/_map.scss +13 -13
  39. data/stylesheets/encode/types/_null.scss +9 -9
  40. data/stylesheets/encode/types/_number.scss +9 -9
  41. data/stylesheets/encode/types/_string.scss +9 -9
  42. metadata +8 -10
@@ -1,37 +1,37 @@
1
- // Parses a JSON encoded number to find the integer part
2
- // --------------------------------------------------------------------------------
3
- // @param [string] $source: JSON complete source
4
- // @param [number] $pointer: current pointer
5
- // --------------------------------------------------------------------------------
6
- // @throw "Unexpected token $token."
7
- // --------------------------------------------------------------------------------
8
- // @return [list|false] (new pointer, parsed number)
9
-
10
- @function _find-integer($source, $pointer) {
11
- $source: to-lower-case($source);
12
- $length: str-length($source);
13
- $numbers: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9';
14
- $result: 0;
15
-
16
- @while $pointer <= $length {
17
- $token: str-slice($source, $pointer, $pointer);
18
- $index: index($numbers, $token);
19
-
20
- @if $token == '-' {
21
- // do nothing
22
- }
23
- @else if $index {
24
- $result: $result * 10 + ($index - 1);
25
- }
26
- @else {
27
- @if index('e' '.' ' ' ',' ']' '}', $token) {
28
- @return $pointer, $result;
29
- }
30
- @return _throw("Unexpected token `" + $token + "`.", $pointer);
31
- }
32
-
33
- $pointer: $pointer + 1;
34
- }
35
-
36
- @return $pointer, $result;
37
- }
1
+ // Parses a JSON encoded number to find the integer part
2
+ // --------------------------------------------------------------------------------
3
+ // @param [string] $source: JSON complete source
4
+ // @param [number] $pointer: current pointer
5
+ // --------------------------------------------------------------------------------
6
+ // @throw "Unexpected token $token."
7
+ // --------------------------------------------------------------------------------
8
+ // @return [list|false] (new pointer, parsed number)
9
+
10
+ @function _find-integer($source, $pointer) {
11
+ $source: to-lower-case($source);
12
+ $length: str-length($source);
13
+ $numbers: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9';
14
+ $result: 0;
15
+
16
+ @while $pointer <= $length {
17
+ $token: str-slice($source, $pointer, $pointer);
18
+ $index: index($numbers, $token);
19
+
20
+ @if $token == '-' {
21
+ // do nothing
22
+ }
23
+ @else if $index {
24
+ $result: $result * 10 + ($index - 1);
25
+ }
26
+ @else {
27
+ @if index('e' '.' ' ' ',' ']' '}', $token) {
28
+ @return $pointer, $result;
29
+ }
30
+ @return _throw("Unexpected token `" + $token + "`.", $pointer);
31
+ }
32
+
33
+ $pointer: $pointer + 1;
34
+ }
35
+
36
+ @return $pointer, $result;
37
+ }
@@ -1,22 +1,22 @@
1
- // Power function
2
- // --------------------------------------------------------------------------------
3
- // @param [number] $x: number
4
- // @param [number] $n: power
5
- // --------------------------------------------------------------------------------
6
- // @return [number] $x ^ $n
7
-
8
- @function _pow($x, $n) {
9
- $ret: 1;
10
-
11
- @if $n >= 0 {
12
- @for $i from 1 through $n {
13
- $ret: $ret * $x;
14
- }
15
- } @else {
16
- @for $i from $n to 0 {
17
- $ret: $ret / $x;
18
- }
19
- }
20
-
21
- @return $ret;
22
- }
1
+ // Power function
2
+ // --------------------------------------------------------------------------------
3
+ // @param [number] $x: number
4
+ // @param [number] $n: power
5
+ // --------------------------------------------------------------------------------
6
+ // @return [number] $x ^ $n
7
+
8
+ @function _pow($x, $n) {
9
+ $ret: 1;
10
+
11
+ @if $n >= 0 {
12
+ @for $i from 1 through $n {
13
+ $ret: $ret * $x;
14
+ }
15
+ } @else {
16
+ @for $i from $n to 0 {
17
+ $ret: $ret / $x;
18
+ }
19
+ }
20
+
21
+ @return $ret;
22
+ }
@@ -1,57 +1,57 @@
1
- // Will find the first non escaped quote in a JSON String
2
- // --------------------------------------------------------------------------------
3
- // @param [string] $string: to search in
4
- // --------------------------------------------------------------------------------
5
- // @return [number] position of the first non escaped quote
6
-
7
- @function _find-ending-quote($string){
8
- $backslash: str-slice('\\', 1, 1); // Dirty hack to have a single backslash
9
- $escaped-chars: '"\/bfnrtu'; // Characters that can be escaped in JSON
10
- $hexadecimal-chars: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '0' 'a' 'b' 'c' 'd' 'e' 'f';
11
-
12
- $pos: 1;
13
- $length: str-length($string);
14
- $backslash-found: false;
15
-
16
- @while $pos <= $length {
17
- $char: to-lower-case(str-slice($string, $pos, $pos));
18
-
19
- // Previous char was a backslash
20
- @if $backslash-found {
21
-
22
- // Special case, the 'u' character
23
- @if $char == 'u' {
24
- // Next 4 characters must be hexadecimal
25
- @if not index($hexadecimal-chars, str-slice($string, $pos + 1, $pos + 1)) or
26
- not index($hexadecimal-chars, str-slice($string, $pos + 2, $pos + 2)) or
27
- not index($hexadecimal-chars, str-slice($string, $pos + 3, $pos + 3)) or
28
- not index($hexadecimal-chars, str-slice($string, $pos + 4, $pos + 4)) {
29
- @return 0;
30
- }
31
-
32
- $pos: $pos + 4;
33
- }
34
-
35
- // Invalid character escaped
36
- @else if not str-index($escaped-chars, $char) {
37
- @return 0;
38
- }
39
-
40
- $backslash-found: false;
41
- }
42
-
43
- @else if $char == $backslash {
44
- $backslash-found: true;
45
- }
46
-
47
- // Unescaped quote found
48
- @else if $char == '"' {
49
- @return $pos;
50
- }
51
-
52
- $pos: $pos + 1;
53
- }
54
-
55
- // No end of string found
56
- @return 0;
57
- }
1
+ // Will find the first non escaped quote in a JSON String
2
+ // --------------------------------------------------------------------------------
3
+ // @param [string] $string: to search in
4
+ // --------------------------------------------------------------------------------
5
+ // @return [number] position of the first non escaped quote
6
+
7
+ @function _find-ending-quote($string){
8
+ $backslash: str-slice('\\', 1, 1); // Dirty hack to have a single backslash
9
+ $escaped-chars: '"\/bfnrtu'; // Characters that can be escaped in JSON
10
+ $hexadecimal-chars: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '0' 'a' 'b' 'c' 'd' 'e' 'f';
11
+
12
+ $pos: 1;
13
+ $length: str-length($string);
14
+ $backslash-found: false;
15
+
16
+ @while $pos <= $length {
17
+ $char: to-lower-case(str-slice($string, $pos, $pos));
18
+
19
+ // Previous char was a backslash
20
+ @if $backslash-found {
21
+
22
+ // Special case, the 'u' character
23
+ @if $char == 'u' {
24
+ // Next 4 characters must be hexadecimal
25
+ @if not index($hexadecimal-chars, str-slice($string, $pos + 1, $pos + 1)) or
26
+ not index($hexadecimal-chars, str-slice($string, $pos + 2, $pos + 2)) or
27
+ not index($hexadecimal-chars, str-slice($string, $pos + 3, $pos + 3)) or
28
+ not index($hexadecimal-chars, str-slice($string, $pos + 4, $pos + 4)) {
29
+ @return 0;
30
+ }
31
+
32
+ $pos: $pos + 4;
33
+ }
34
+
35
+ // Invalid character escaped
36
+ @else if not str-index($escaped-chars, $char) {
37
+ @return 0;
38
+ }
39
+
40
+ $backslash-found: false;
41
+ }
42
+
43
+ @else if $char == $backslash {
44
+ $backslash-found: true;
45
+ }
46
+
47
+ // Unescaped quote found
48
+ @else if $char == '"' {
49
+ @return $pos;
50
+ }
51
+
52
+ $pos: $pos + 1;
53
+ }
54
+
55
+ // No end of string found
56
+ @return 0;
57
+ }
@@ -1,43 +1,43 @@
1
- // Parses a JSON encoded string to see if it's a CSS length
2
- // --------------------------------------------------------------------------------
3
- // @param [string] $string: JSON string
4
- // --------------------------------------------------------------------------------
5
- // @return [number|string] string or number, depending on the match
6
-
7
- @function _length($string) {
8
- @if type-of($string) == number {
9
- @return $string;
10
- }
11
-
12
- $strings: 'px' 'cm' 'mm' '%' 'ch' 'pica' 'in' 'em' 'rem' 'pt' 'pc' 'ex' 'vw' 'vh' 'vmin' 'vmax';
13
- $units: 1px 1cm 1mm 1% 1ch 1pica 1in 1em 1rem 1pt 1pc 1ex 1vw 1vh 1vmin 1vmax;
14
- $number: "";
15
- $unit: "";
16
-
17
- @for $i from 1 through str-length($string) {
18
- $c: str-slice($string, $i, $i);
19
- @if $c == ' ' {
20
- @if $number != "" {
21
- @return $string;
22
- }
23
- }
24
- @else if index('0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '-' '.', $c) {
25
- $number: $number + $c;
26
- }
27
- @else {
28
- @if $number == "" {
29
- @return $string;
30
- }
31
- $unit: $unit + $c;
32
- }
33
- }
34
-
35
- $number: nth(_json-decode--number($number, 2), 2);
36
- $index: index($strings, to-lower-case($unit));
37
-
38
- @if $index and $index > 0 {
39
- @return $number * nth($units, $index);
40
- }
41
-
42
- @return $string;
1
+ // Parses a JSON encoded string to see if it's a CSS length
2
+ // --------------------------------------------------------------------------------
3
+ // @param [string] $string: JSON string
4
+ // --------------------------------------------------------------------------------
5
+ // @return [number|string] string or number, depending on the match
6
+
7
+ @function _length($string) {
8
+ @if type-of($string) == number {
9
+ @return $string;
10
+ }
11
+
12
+ $strings: 'px' 'cm' 'mm' '%' 'ch' 'pica' 'in' 'em' 'rem' 'pt' 'pc' 'ex' 'vw' 'vh' 'vmin' 'vmax';
13
+ $units: 1px 1cm 1mm 1% 1ch 1pica 1in 1em 1rem 1pt 1pc 1ex 1vw 1vh 1vmin 1vmax;
14
+ $number: "";
15
+ $unit: "";
16
+
17
+ @for $i from 1 through str-length($string) {
18
+ $c: str-slice($string, $i, $i);
19
+ @if $c == ' ' {
20
+ @if $number != "" {
21
+ @return $string;
22
+ }
23
+ }
24
+ @else if index('0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '-' '.', $c) {
25
+ $number: $number + $c;
26
+ }
27
+ @else {
28
+ @if $number == "" {
29
+ @return $string;
30
+ }
31
+ $unit: $unit + $c;
32
+ }
33
+ }
34
+
35
+ $number: nth(_json-decode--number($number, 2), 2);
36
+ $index: index($strings, to-lower-case($unit));
37
+
38
+ @if $index and $index > 0 {
39
+ @return $number * nth($units, $index);
40
+ }
41
+
42
+ @return $string;
43
43
  }
@@ -1,16 +1,16 @@
1
- // Strip special carriage return characters
2
- // --------------------------------------------------------------------------------
3
- // @param [string] $string: string to parse
4
- // @param [string] $char: char to strip
5
- // --------------------------------------------------------------------------------
6
- // @return [string] new string
7
-
8
- @function _strip-token($string, $token, $replace) {
9
-
10
- @while str-index($string, $token) and str-index($string, $token) > 0 {
11
- $index: str-index($string, $token);
12
- $string: str-slice($string, 1, $index - 1) + $replace + str-slice($string, $index + str-length($token));
13
- }
14
-
15
- @return $string;
16
- }
1
+ // Strip special carriage return characters
2
+ // --------------------------------------------------------------------------------
3
+ // @param [string] $string: string to parse
4
+ // @param [string] $char: char to strip
5
+ // --------------------------------------------------------------------------------
6
+ // @return [string] new string
7
+
8
+ @function _strip-token($string, $token, $replace) {
9
+
10
+ @while str-index($string, $token) and str-index($string, $token) > 0 {
11
+ $index: str-index($string, $token);
12
+ $string: str-slice($string, 1, $index - 1) + $replace + str-slice($string, $index + str-length($token));
13
+ }
14
+
15
+ @return $string;
16
+ }
@@ -1,40 +1,40 @@
1
- // Parses a JSON encoded `true`
2
- // --------------------------------------------------------------------------------
3
- // @param [string] $source: JSON complete source
4
- // @param [number] $pointer: current pointer
5
- // --------------------------------------------------------------------------------
6
- // @throw "Unexpected token `t`."
7
- // --------------------------------------------------------------------------------
8
- // @return [list|false] (new pointer, true)
9
-
10
- @function _json-decode--true($source, $pointer) {
11
- $length: str-length($source);
12
- @if $length - $pointer < 2
13
- or str-slice($source, $pointer, $pointer) != 'r'
14
- or str-slice($source, $pointer + 1, $pointer + 1) != 'u'
15
- or str-slice($source, $pointer + 2, $pointer + 2) != 'e' {
16
- @return _throw("Unexpected token: `t`.", $pointer);
17
- }
18
- @return ($pointer + 3, true);
19
- }
20
-
21
- // Parses a JSON encoded `false`
22
- // --------------------------------------------------------------------------------
23
- // @param $source: JSON complete source
24
- // @param $pointer: current pointer
25
- // --------------------------------------------------------------------------------
26
- // @throw "Unexpected token `f`."
27
- // --------------------------------------------------------------------------------
28
- // @return [list|false] (new pointer, false)
29
-
30
- @function _json-decode--false($source, $pointer) {
31
- $length: str-length($source);
32
- @if $length - $pointer < 3
33
- or str-slice($source, $pointer, $pointer) != 'a'
34
- or str-slice($source, $pointer + 1, $pointer + 1) != 'l'
35
- or str-slice($source, $pointer + 2, $pointer + 2) != 's'
36
- or str-slice($source, $pointer + 3, $pointer + 3) != 'e' {
37
- @return _throw("Unexpected token: `f`.", $pointer);
38
- }
39
- @return ($pointer + 4, false);
40
- }
1
+ // Parses a JSON encoded `true`
2
+ // --------------------------------------------------------------------------------
3
+ // @param [string] $source: JSON complete source
4
+ // @param [number] $pointer: current pointer
5
+ // --------------------------------------------------------------------------------
6
+ // @throw "Unexpected token `t`."
7
+ // --------------------------------------------------------------------------------
8
+ // @return [list|false] (new pointer, true)
9
+
10
+ @function _json-decode--true($source, $pointer) {
11
+ $length: str-length($source);
12
+ @if $length - $pointer < 2
13
+ or str-slice($source, $pointer, $pointer) != 'r'
14
+ or str-slice($source, $pointer + 1, $pointer + 1) != 'u'
15
+ or str-slice($source, $pointer + 2, $pointer + 2) != 'e' {
16
+ @return _throw("Unexpected token: `t`.", $pointer);
17
+ }
18
+ @return ($pointer + 3, true);
19
+ }
20
+
21
+ // Parses a JSON encoded `false`
22
+ // --------------------------------------------------------------------------------
23
+ // @param $source: JSON complete source
24
+ // @param $pointer: current pointer
25
+ // --------------------------------------------------------------------------------
26
+ // @throw "Unexpected token `f`."
27
+ // --------------------------------------------------------------------------------
28
+ // @return [list|false] (new pointer, false)
29
+
30
+ @function _json-decode--false($source, $pointer) {
31
+ $length: str-length($source);
32
+ @if $length - $pointer < 3
33
+ or str-slice($source, $pointer, $pointer) != 'a'
34
+ or str-slice($source, $pointer + 1, $pointer + 1) != 'l'
35
+ or str-slice($source, $pointer + 2, $pointer + 2) != 's'
36
+ or str-slice($source, $pointer + 3, $pointer + 3) != 'e' {
37
+ @return _throw("Unexpected token: `f`.", $pointer);
38
+ }
39
+ @return ($pointer + 4, false);
40
+ }
@@ -1,54 +1,54 @@
1
- // Parses a JSON encoded array
2
- // --------------------------------------------------------------------------------
3
- // @param [string] $source: JSON complete source
4
- // @param [number] $pointer: current pointer
5
- // --------------------------------------------------------------------------------
6
- // @throw "Unexpected comma in array literal."
7
- // @throw "Missing comma in array literal."
8
- // @throw "Unterminated array literal."
9
- // --------------------------------------------------------------------------------
10
- // @return [list|false] (new pointer, parsed list)
11
-
12
- @function _json-decode--list($source, $pointer) {
13
- $length: str-length($source);
14
- $list: ();
15
- $needs-comma: false;
16
-
17
- @if $pointer <= $length and str-slice($source, $pointer, $pointer) == "]" {
18
- @return ($pointer + 1, $list);
19
- }
20
-
21
- @while $pointer <= $length {
22
- $token: str-slice($source, $pointer, $pointer);
23
-
24
- @if $token == "]" {
25
- @if not $needs-comma and length($list) != 0 {
26
- @return _throw("Unexpected comma in array literal.", $pointer);
27
- }
28
-
29
- // Do it the Sass way and destruct a single item array to an element.
30
- @return ($pointer + 1, if(length($list) == 1, nth($list, 1), $list));
31
- }
32
- @else if $token == " " {
33
- $pointer: $pointer + 1;
34
- }
35
- @else if $token == "," {
36
- @if not $needs-comma {
37
- @return _throw("Unexpected comma in array literal.", $pointer);
38
- }
39
- $needs-comma: false;
40
- $pointer: $pointer + 1;
41
- }
42
- @else {
43
- @if $needs-comma {
44
- @return _throw("Missing comma in array literal.", $pointer);
45
- }
46
- $read: _json-decode--value($source, $pointer);
47
- $pointer: nth($read, 1);
48
- $list: append($list, nth($read, 2));
49
- $needs-comma: true;
50
- }
51
- }
52
-
53
- @return _throw("Unterminated array literal.", $pointer);
54
- }
1
+ // Parses a JSON encoded array
2
+ // --------------------------------------------------------------------------------
3
+ // @param [string] $source: JSON complete source
4
+ // @param [number] $pointer: current pointer
5
+ // --------------------------------------------------------------------------------
6
+ // @throw "Unexpected comma in array literal."
7
+ // @throw "Missing comma in array literal."
8
+ // @throw "Unterminated array literal."
9
+ // --------------------------------------------------------------------------------
10
+ // @return [list|false] (new pointer, parsed list)
11
+
12
+ @function _json-decode--list($source, $pointer) {
13
+ $length: str-length($source);
14
+ $list: ();
15
+ $needs-comma: false;
16
+
17
+ @if $pointer <= $length and str-slice($source, $pointer, $pointer) == "]" {
18
+ @return ($pointer + 1, $list);
19
+ }
20
+
21
+ @while $pointer <= $length {
22
+ $token: str-slice($source, $pointer, $pointer);
23
+
24
+ @if $token == "]" {
25
+ @if not $needs-comma and length($list) != 0 {
26
+ @return _throw("Unexpected comma in array literal.", $pointer);
27
+ }
28
+
29
+ // Do it the Sass way and destruct a single item array to an element.
30
+ @return ($pointer + 1, if(length($list) == 1, nth($list, 1), $list));
31
+ }
32
+ @else if $token == " " {
33
+ $pointer: $pointer + 1;
34
+ }
35
+ @else if $token == "," {
36
+ @if not $needs-comma {
37
+ @return _throw("Unexpected comma in array literal.", $pointer);
38
+ }
39
+ $needs-comma: false;
40
+ $pointer: $pointer + 1;
41
+ }
42
+ @else {
43
+ @if $needs-comma {
44
+ @return _throw("Missing comma in array literal.", $pointer);
45
+ }
46
+ $read: _json-decode--value($source, $pointer);
47
+ $pointer: nth($read, 1);
48
+ $list: append($list, nth($read, 2));
49
+ $needs-comma: true;
50
+ }
51
+ }
52
+
53
+ @return _throw("Unterminated array literal.", $pointer);
54
+ }