js2 0.3.12 → 0.3.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/CHANGELOG +7 -2
  2. data/lib/js2/browser.js +1 -1
  3. data/lib/js2/js2.js +146 -33
  4. metadata +6 -6
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ 0.3.13
2
+ * fixed problem that doesn't allow toString methods
3
+ * fixed string sanitizer problem with double quotes
4
+ * JSML POC
5
+
1
6
  0.3.12
2
7
  * fixed parse error in JSMLElement
3
8
  * Yanking 0.3.11
@@ -19,7 +24,7 @@
19
24
  0.3.8
20
25
  * fied "render"
21
26
  * fixed comment block bug
22
- * using target-dir instead of out-dir
27
+ * using target-dir instead of out-dir
23
28
 
24
29
  0.3.7
25
30
  * fixed foreach counter
@@ -31,7 +36,7 @@
31
36
 
32
37
  0.3.5
33
38
  * Added scope and binding support to shorthand functions
34
- * Added support for modules
39
+ * Added support for modules
35
40
 
36
41
  0.3.4
37
42
  * coded reduce to clojure spec
data/lib/js2/browser.js CHANGED
@@ -19,7 +19,7 @@ function mainFunction (arg) {
19
19
 
20
20
  var JS2 = root.JS2 = mainFunction;
21
21
  var js2 = root.js2 = JS2;
22
- js2.VERSION = "0.3.12";
22
+ js2.VERSION = "0.3.13";
23
23
 
24
24
  JS2.ROOT = JS2;
25
25
 
data/lib/js2/js2.js CHANGED
@@ -14,7 +14,7 @@ function mainFunction (arg) {
14
14
 
15
15
  var JS2 = root.JS2 = mainFunction;
16
16
  var js2 = root.js2 = JS2;
17
- js2.VERSION = "0.3.12";
17
+ js2.VERSION = "0.3.13";
18
18
 
19
19
  JS2.ROOT = JS2;
20
20
 
@@ -308,7 +308,7 @@ function mainFunction (arg) {
308
308
  REGEX: /^%\{/,
309
309
  ID: IDS.ISTRING,
310
310
  sanitize: function(str) {
311
- return str.replace('"', '\\"');
311
+ return JSON.stringify(str);
312
312
  },
313
313
  consume: function() {
314
314
  var m = this.tokens.match(this.REGEX);
@@ -322,14 +322,14 @@ function mainFunction (arg) {
322
322
  if (m) {
323
323
  var matched = m[1];
324
324
  if (m[3] == '#{') {
325
- this.tokens.push([ '"' + this.sanitize(matched) + '"+(', this.ID ]);
325
+ this.tokens.push([ this.sanitize(matched) + '+(', this.ID ]);
326
326
  this.tokens.chomp(m[0].length-1);
327
327
  var block = new JS2.Lexer.Block(this.tokens);
328
328
  block.tokenize();
329
329
  this.tokens.push([ ')+', this.ID ]);
330
330
  toEnd = true;
331
331
  } else if (m[3] == '}' || m[0] == '}') {
332
- this.tokens.push([ '"' + this.sanitize(matched) + '"', this.ID ]);
332
+ this.tokens.push([ this.sanitize(matched), this.ID ]);
333
333
  this.tokens.chomp(m[0].length);
334
334
  break;
335
335
  }
@@ -349,6 +349,7 @@ function mainFunction (arg) {
349
349
  var m = this.tokens.match(this.REGEX);
350
350
  if (!m) return false;
351
351
 
352
+ var templateEngine = m[2];
352
353
  this.tokens.chomp(m[0].length);
353
354
  this.tokens.push([ "\n", IDS.SPACE ]);
354
355
 
@@ -359,13 +360,15 @@ function mainFunction (arg) {
359
360
 
360
361
  var first = true;
361
362
  var noChomp = false;
363
+ if (templateEngine) {
364
+ this.tokens.push([ 'JS2.TEMPLATES["' + templateEngine + '"].process(', IDS.IDENT ]);
365
+ }
362
366
 
363
367
  while (1) {
364
368
  var e = this.tokens.match(ender);
365
369
  if (e) {
366
370
  this.tokens.chomp(e[0].length);
367
- this.tokens.push([ ';', IDS.DSTRING ]);
368
- return true;
371
+ break;
369
372
  }
370
373
 
371
374
  if (noChomp) {
@@ -381,7 +384,7 @@ function mainFunction (arg) {
381
384
 
382
385
  if (next[1]) {
383
386
  this.tokens.chomp(next[1].length);
384
- this.tokens.push([ (first ? '' : '+') + '"' + this.sanitize(next[1]) + '\\\\n"', IDS.DSTRING ]);
387
+ this.tokens.push([ (first ? '' : '+') + this.sanitize(next[1]).replace(/"$/, '\\n"') , IDS.DSTRING ]);
385
388
  }
386
389
 
387
390
  if (next[3] == '#{') {
@@ -397,6 +400,12 @@ function mainFunction (arg) {
397
400
  }
398
401
  first = false;
399
402
  }
403
+
404
+ if (templateEngine) {
405
+ this.tokens.push([ ')', IDS.IDENT ]);
406
+ }
407
+
408
+ this.tokens.push([ ';', IDS.OPERATOR ]);
400
409
  return true;
401
410
  }
402
411
  });
@@ -646,7 +655,7 @@ function mainFunction (arg) {
646
655
  getTokenString: function(token) {
647
656
  if (token[1] == IDS.COMMENT) {
648
657
  return null;
649
- } else if (token[0] in KEYWORDS) {
658
+ } else if (KEYWORDS.hasOwnProperty(token[0])) {
650
659
  return token[0];
651
660
  } else if (token[1] == IDS.SPACE) {
652
661
  return token[0];
@@ -1483,42 +1492,50 @@ JS2.Class.extend('JSML', function(KLASS, OO){
1483
1492
 
1484
1493
  OO.addMember("initialize",function (txt) {
1485
1494
  var lines = txt.split(/\n/);
1486
- this.root = new JS2.JSMLElement('');
1487
- this.current = this.root;
1495
+ this.root = new JS2.JSMLElement();
1488
1496
  this.stack = [ this.root ];
1489
1497
 
1490
1498
  for(var _i1=0,_c1=lines,_l1=_c1.length,l;(l=_c1[_i1])||(_i1<_l1);_i1++){
1491
1499
  if (l.match(/^\s*$/)) continue;
1492
1500
  this.processLine(l);
1493
1501
  }
1502
+
1503
+ var toEval = 'function process() { var out = [];\n' + this.flatten().join('') + '\n return out.join("");\n}';
1504
+ eval(toEval);
1505
+
1506
+ this.result = function(hash) {
1507
+ return process.call(hash);
1508
+ };
1509
+ });
1510
+
1511
+ OO.addMember("flatten",function () {
1512
+ return this.root.flatten();
1494
1513
  });
1495
1514
 
1496
1515
  OO.addMember("processLine",function (line) {
1497
- console.log(line);
1498
- var ele = new JS2.JSMLElement(line);
1516
+ var ele = new JS2.JSMLElement(line);
1499
1517
  var scope = this.getScope();
1500
1518
 
1501
1519
  if (ele.scope == scope) {
1502
- console.log('same');
1503
1520
  this.stack.pop();
1504
1521
  this.getLast().push(ele);
1505
- } else if (ele.scope == scope+1) {
1506
- console.log('greater');
1522
+ this.stack.push(ele);
1523
+ } else if (ele.scope > scope) {
1507
1524
  this.getLast().push(ele);
1508
1525
  this.stack.push(ele);
1509
1526
  } else if (ele.scope < scope) {
1510
- console.log('less');
1511
- var diff = ele.scope - scope;
1512
- while(diff-- != 0) {
1527
+ var diff = scope - ele.scope + 1;
1528
+ while(diff-- > 0) {
1513
1529
  this.stack.pop();
1514
1530
  }
1515
1531
  this.getLast().push(ele);
1532
+ this.stack.push(ele);
1516
1533
  }
1517
1534
  });
1518
1535
 
1519
1536
 
1520
1537
  OO.addMember("getScope",function () {
1521
- return this.stack.length;
1538
+ return this.stack.length - 1;
1522
1539
  });
1523
1540
 
1524
1541
  OO.addMember("getLast",function () {
@@ -1528,13 +1545,22 @@ JS2.Class.extend('JSML', function(KLASS, OO){
1528
1545
  });
1529
1546
 
1530
1547
  JS2.Class.extend('JSMLElement', function(KLASS, OO){
1531
- OO.addMember("SCOPE_REGEX",/(\s+)(.*)/);
1532
- OO.addMember("TOKEN_REGEX",/^(%|#|\.)([\w-]+)/);
1533
- OO.addMember("JS_REGEX",/^(-|=)(.*)$/);
1548
+ OO.addMember("SCOPE_REGEX",/^(\s*)(.*)$/);
1549
+ OO.addMember("SPLIT_REGEX",/^([^=-\s\{]*)(\{.*\})?(=|-)?(?:\s*)(.*)$/);
1550
+ OO.addMember("TOKEN_REGEX",/(\%|\#|\.)([\w-]+)/g);
1551
+ OO.addMember("JS_REGEX",/^(-|=)(.*)$/g);
1552
+ OO.addMember("SCOPE_OFFSET",1);
1534
1553
 
1535
1554
  OO.addMember("initialize",function (line) {
1555
+ this.children = [];
1556
+
1557
+ if (line == null) {
1558
+ this.scope = this.SCOPE_OFFSET;
1559
+ return;
1560
+ }
1561
+
1536
1562
  var spaceMatch = line.match(this.SCOPE_REGEX);
1537
- this.scope = spaceMatch[1].length / 2;
1563
+ this.scope = spaceMatch[1].length / 2 + this.SCOPE_OFFSET;
1538
1564
 
1539
1565
  this.classes = [];
1540
1566
  this.nodeID = null;
@@ -1542,27 +1568,114 @@ JS2.Class.extend('JSMLElement', function(KLASS, OO){
1542
1568
  this.parse(spaceMatch[2]);
1543
1569
  });
1544
1570
 
1571
+ OO.addMember("push",function (child) {
1572
+ this.children.push(child);
1573
+ });
1574
+
1545
1575
  OO.addMember("parse",function (line) {
1576
+ this.attributes = {};
1577
+ this.line = line;
1546
1578
  var self = this;
1547
- line = line.replace(this.TOKEN_REGEX, function(match, type, name){
1579
+
1580
+ var splitted = line.match(this.SPLIT_REGEX);
1581
+ var tokens = splitted[1];
1582
+ var attrs = splitted[2];
1583
+ var jsType = splitted[3];
1584
+ var content = splitted[4];
1585
+
1586
+ tokens.replace(this.TOKEN_REGEX, function(match, type, name){
1548
1587
  switch(type) {
1549
- case '%': this.nodeType = name; break;
1550
- case '#': this.classes.push(name); break;
1551
- case '.': this.nodeID = name; break;
1588
+ case '%': self.nodeType = name; break;
1589
+ case '.': self.classes.push(name); break;
1590
+ case '#': self.nodeID = name; break;
1552
1591
  }
1553
1592
  return '';
1554
1593
  });
1555
1594
 
1556
- line = line.replace(this.JS_OUT_REGEX, function(match, type, content){
1557
- switch(type) {
1558
- case '=': this.jsEQ = content; break;
1559
- case '-': this.jsExec = content; break;
1595
+ if (jsType == '=') {
1596
+ this.jsEQ = content;
1597
+ } else if (jsType == '-') {
1598
+ this.jsExec = content;
1599
+ } else {
1600
+ this.content = content;
1601
+ }
1602
+
1603
+ if (attrs) {
1604
+ eval('this.attributes = ' + attrs + ';');
1605
+ }
1606
+
1607
+ if (!this.nodeType && (this.classes.length || this.nodeID)) {
1608
+ this.nodeType = 'div';
1609
+ }
1610
+ });
1611
+
1612
+ OO.addMember("flatten",function () {
1613
+ var out = [];
1614
+
1615
+ for(var _i1=0,_c1=this.children,_l1=_c1.length,c;(c=_c1[_i1])||(_i1<_l1);_i1++){
1616
+ var arr = c.flatten();
1617
+ for(var _i2=0,_c2=arr,_l2=_c2.length,item;(item=_c2[_i2])||(_i2<_l2);_i2++){
1618
+ out.push(item);
1560
1619
  }
1561
- return '';
1562
- });
1620
+ }
1621
+
1622
+ if (this.nodeType) {
1623
+ this.handleJsEQ(out);
1624
+ this.handleContent(out);
1625
+ out.unshift('out.push(' + JSON.stringify("<"+(this.nodeType)+""+(this.getAttributes())+">") + ');\n');
1626
+ out.push('out.push(' + JSON.stringify("</"+(this.nodeType)+">") + ');\n');
1627
+ } else {
1628
+ this.handleJsExec(out);
1629
+ this.handleJsEQ(out);
1630
+ this.handleContent(out);
1631
+ }
1632
+
1633
+ return out;
1634
+ });
1635
+
1636
+ OO.addMember("handleJsEQ",function (out) {
1637
+ if (this.jsEQ) {
1638
+ this.jsEQ = this.jsEQ.replace(/;\s*$/, '');
1639
+ out.unshift('out.push(' + this.jsEQ + ');\n');
1640
+ }
1641
+ });
1642
+
1643
+ OO.addMember("handleContent",function (out) {
1644
+ if (this.content != null && this.content.length > 0) {
1645
+ out.unshift('out.push(' + JSON.stringify(this.content) + ');\n');
1646
+ }
1647
+ });
1648
+
1649
+
1650
+ OO.addMember("handleJsExec",function (out) {
1651
+ if (this.jsExec) {
1652
+ out.unshift(this.jsExec);
1653
+ if (this.jsExec.match(/\{\s*$/)) {
1654
+ out.push("}\n");
1655
+ }
1656
+ }
1657
+ });
1658
+
1659
+ OO.addMember("getAttributes",function () {
1660
+ if (!this.attributes) return '';
1661
+
1662
+ var out = [];
1663
+ var attrs = this.attributes;
1664
+
1665
+ if (attrs['class']) this.classes.push(attrs['class']);
1666
+ if (this.classes.length) attrs['class'] = this.classes.join(' ');
1667
+
1668
+ for (var k in attrs) {
1669
+ if (attrs.hasOwnProperty(k)) {
1670
+ out.push(k + '=' + JSON.stringify(attrs[k]));
1671
+ }
1672
+ }
1673
+
1674
+ return (out.length ? ' ' : '') + out.join(' ');
1563
1675
  });
1564
1676
  });
1565
1677
 
1678
+ JS2.TEMPLATES = { jsml: JS2.JSML };
1566
1679
 
1567
1680
 
1568
1681
  JS2.fs = new JS2.FileSystem(JS2_RUBY_FILE_ADAPTER);
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: js2
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.12
5
+ version: 0.3.13
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jeff Su
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-31 00:00:00 +08:00
13
+ date: 2011-04-04 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -36,11 +36,11 @@ extra_rdoc_files: []
36
36
  files:
37
37
  - bin/js2
38
38
  - bin/js2-ruby
39
- - lib/js2.rb
40
- - lib/js2/context.rb
41
- - lib/js2/rack.rb
42
39
  - lib/js2/command.rb
40
+ - lib/js2/context.rb
43
41
  - lib/js2/fs.rb
42
+ - lib/js2/rack.rb
43
+ - lib/js2.rb
44
44
  - lib/js2/browser.js
45
45
  - lib/js2/js2.js
46
46
  - CHANGELOG
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  requirements: []
69
69
 
70
70
  rubyforge_project:
71
- rubygems_version: 1.5.2
71
+ rubygems_version: 1.6.2
72
72
  signing_key:
73
73
  specification_version: 3
74
74
  summary: Javascript Syntactic Sugar