handlebars 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +1 -2
  3. data/README.mdown +15 -7
  4. data/handlebars.gemspec +2 -5
  5. data/lib/handlebars.rb +5 -5
  6. data/lib/handlebars/context.rb +3 -12
  7. data/lib/handlebars/version.rb +1 -1
  8. data/spec/handlebars_spec.rb +1 -1
  9. metadata +47 -58
  10. data/.gitmodules +0 -3
  11. data/vendor/handlebars/.gitignore +0 -8
  12. data/vendor/handlebars/.jshintrc +0 -52
  13. data/vendor/handlebars/.npmignore +0 -10
  14. data/vendor/handlebars/.rspec +0 -1
  15. data/vendor/handlebars/Gemfile +0 -5
  16. data/vendor/handlebars/LICENSE +0 -19
  17. data/vendor/handlebars/README.markdown +0 -317
  18. data/vendor/handlebars/Rakefile +0 -109
  19. data/vendor/handlebars/bench/benchwarmer.js +0 -149
  20. data/vendor/handlebars/bench/handlebars.js +0 -172
  21. data/vendor/handlebars/bin/handlebars +0 -193
  22. data/vendor/handlebars/dist/handlebars.js +0 -2201
  23. data/vendor/handlebars/dist/handlebars.runtime.js +0 -321
  24. data/vendor/handlebars/lib/handlebars.js +0 -14
  25. data/vendor/handlebars/lib/handlebars/base.js +0 -154
  26. data/vendor/handlebars/lib/handlebars/compiler/ast.js +0 -133
  27. data/vendor/handlebars/lib/handlebars/compiler/base.js +0 -21
  28. data/vendor/handlebars/lib/handlebars/compiler/compiler.js +0 -1267
  29. data/vendor/handlebars/lib/handlebars/compiler/index.js +0 -7
  30. data/vendor/handlebars/lib/handlebars/compiler/printer.js +0 -131
  31. data/vendor/handlebars/lib/handlebars/compiler/visitor.js +0 -13
  32. data/vendor/handlebars/lib/handlebars/runtime.js +0 -88
  33. data/vendor/handlebars/lib/handlebars/utils.js +0 -67
  34. data/vendor/handlebars/package.json +0 -35
  35. data/vendor/handlebars/spec/acceptance_spec.rb +0 -101
  36. data/vendor/handlebars/spec/parser_spec.rb +0 -433
  37. data/vendor/handlebars/spec/qunit_spec.js +0 -1370
  38. data/vendor/handlebars/spec/spec_helper.rb +0 -157
  39. data/vendor/handlebars/spec/tokenizer_spec.rb +0 -301
  40. data/vendor/handlebars/src/handlebars.l +0 -53
  41. data/vendor/handlebars/src/handlebars.yy +0 -109
  42. data/vendor/handlebars/src/parser-prefix.js +0 -1
  43. data/vendor/handlebars/src/parser-suffix.js +0 -4
@@ -1,109 +0,0 @@
1
- require "rubygems"
2
- require "bundler/setup"
3
-
4
- def compile_parser
5
- system "./node_modules/.bin/jison -m js src/handlebars.yy src/handlebars.l"
6
- if $?.success?
7
- File.open("lib/handlebars/compiler/parser.js", "w") do |file|
8
- file.puts File.read("src/parser-prefix.js") + File.read("handlebars.js") + File.read("src/parser-suffix.js")
9
- end
10
-
11
- sh "rm handlebars.js"
12
- else
13
- puts "Failed to run Jison."
14
- end
15
- end
16
-
17
- file "lib/handlebars/compiler/parser.js" => ["src/handlebars.yy","src/handlebars.l"] do
18
- if File.exists?('./node_modules/jison')
19
- compile_parser
20
- else
21
- puts "Jison is not installed. Trying `npm install jison`."
22
- sh "npm install"
23
- compile_parser
24
- end
25
- end
26
-
27
- task :compile => "lib/handlebars/compiler/parser.js"
28
-
29
- desc "run the spec suite"
30
- task :spec => [:release] do
31
- rc = system "rspec -cfs spec"
32
- fail "rspec spec failed with exit code #{$?.exitstatus}" if (rc.nil? || ! rc || $?.exitstatus != 0)
33
- end
34
-
35
- desc "run the npm test suite"
36
- task :npm_test => [:release] do
37
- rc = system "npm test"
38
- fail "npm test failed with exit code #{$?.exitstatus}" if (rc.nil? || ! rc || $?.exitstatus != 0)
39
- end
40
-
41
- task :default => [:compile, :spec, :npm_test]
42
-
43
- def remove_exports(string)
44
- match = string.match(%r{^// BEGIN\(BROWSER\)\n(.*)\n^// END\(BROWSER\)}m)
45
- match ? match[1] : string
46
- end
47
-
48
- minimal_deps = %w(base compiler/parser compiler/base compiler/ast utils compiler/compiler runtime).map do |file|
49
- "lib/handlebars/#{file}.js"
50
- end
51
-
52
- runtime_deps = %w(base utils runtime).map do |file|
53
- "lib/handlebars/#{file}.js"
54
- end
55
-
56
- directory "dist"
57
-
58
- minimal_deps.unshift "dist"
59
-
60
- def build_for_task(task)
61
- FileUtils.rm_rf("dist/*") if File.directory?("dist")
62
- FileUtils.mkdir_p("dist")
63
-
64
- contents = ["/*\n\n" + File.read('LICENSE') + "\n*/\n"]
65
- task.prerequisites.each do |filename|
66
- next if filename == "dist"
67
-
68
- contents << "// #{filename}\n" + remove_exports(File.read(filename)) + ";"
69
- end
70
-
71
- File.open(task.name, "w") do |file|
72
- file.puts contents.join("\n")
73
- end
74
- end
75
-
76
- file "dist/handlebars.js" => minimal_deps do |task|
77
- build_for_task(task)
78
- end
79
-
80
- file "dist/handlebars.runtime.js" => runtime_deps do |task|
81
- build_for_task(task)
82
- end
83
-
84
- task :build => [:compile, "dist/handlebars.js"]
85
- task :runtime => [:compile, "dist/handlebars.runtime.js"]
86
-
87
- desc "build the build and runtime version of handlebars"
88
- task :release => [:build, :runtime]
89
-
90
- directory "vendor"
91
-
92
- desc "benchmark against dust.js and mustache.js"
93
- task :bench => "vendor" do
94
- require "open-uri"
95
-
96
- #if File.directory?("vendor/coffee")
97
- #system "cd vendor/coffee && git pull"
98
- #else
99
- #system "git clone git://github.com/jashkenas/coffee-script.git vendor/coffee"
100
- #end
101
-
102
- #if File.directory?("vendor/eco")
103
- #system "cd vendor/eco && git pull && npm update"
104
- #else
105
- #system "git clone git://github.com/sstephenson/eco.git vendor/eco && cd vendor/eco && npm update"
106
- #end
107
-
108
- system "node bench/handlebars.js"
109
- end
@@ -1,149 +0,0 @@
1
-
2
- var Benchmark = require("benchmark");
3
-
4
- var BenchWarmer = function(names) {
5
- this.benchmarks = [];
6
- this.currentBenches = [];
7
- this.names = [];
8
- this.errors = {};
9
- };
10
-
11
- var print = require("sys").print;
12
-
13
- BenchWarmer.prototype = {
14
- winners: function(benches) {
15
- var result = Benchmark.filter(benches, function(bench) { return bench.cycles; });
16
-
17
- if (result.length > 1) {
18
- result.sort(function(a, b) { return b.compare(a); });
19
- first = result[0];
20
- last = result[result.length - 1];
21
-
22
- var winners = [];
23
-
24
- Benchmark.each(result, function(bench) {
25
- if (bench.compare(first) === 0) {
26
- winners.push(bench);
27
- }
28
- });
29
-
30
- return winners;
31
- } else {
32
- return result;
33
- }
34
- },
35
- suite: function(suite, fn) {
36
- this.suiteName = suite;
37
- this.first = true;
38
-
39
- var self = this;
40
-
41
- fn(function(name, benchFn) {
42
- self.push(name, benchFn);
43
- });
44
- },
45
- push: function(name, fn) {
46
- if(this.names.indexOf(name) == -1) {
47
- this.names.push(name);
48
- }
49
-
50
- var first = this.first, suiteName = this.suiteName, self = this;
51
- this.first = false;
52
-
53
- var bench = new Benchmark(function() {
54
- fn();
55
- }, {
56
- name: this.suiteName + ": " + name,
57
- onComplete: function() {
58
- if(first) { self.startLine(suiteName); }
59
- self.writeBench(bench);
60
- self.currentBenches.push(bench);
61
- }, onError: function() {
62
- self.errors[this.name] = this;
63
- }
64
- });
65
-
66
- this.benchmarks.push(bench);
67
- },
68
- bench: function() {
69
- var benchSize = 0, names = this.names, self = this, i, l;
70
-
71
- for(i=0, l=names.length; i<l; i++) {
72
- var name = names[i];
73
-
74
- if(benchSize < name.length) { benchSize = name.length; }
75
- }
76
-
77
- this.nameSize = benchSize + 2;
78
- this.benchSize = 20;
79
- var horSize = 0;
80
-
81
- this.startLine("ops/msec");
82
- horSize = horSize + "ops/msec ".length;
83
- for(i=0, l=names.length; i<l; i++) {
84
- print(names[i] + new Array(this.benchSize - names[i].length + 1).join(" "));
85
- horSize = horSize + this.benchSize;
86
- }
87
-
88
- print("WINNER(S)");
89
- horSize = horSize + "WINNER(S)".length;
90
-
91
- print("\n" + new Array(horSize + 1).join("-"));
92
-
93
- Benchmark.invoke(this.benchmarks, {
94
- name: "run",
95
- onComplete: function() {
96
- var errors = false, prop, bench;
97
- for(prop in self.errors) { if(self.errors.hasOwnProperty(prop)) { errors = true; break; } }
98
-
99
- if(errors) {
100
- print("\n\nErrors:\n");
101
- for(prop in self.errors) {
102
- if(self.errors.hasOwnProperty(prop)) {
103
- bench = self.errors[prop];
104
- print("\n" + bench.name + ":\n");
105
- print(bench.error.message);
106
- if(bench.error.stack) {
107
- print(bench.error.stack.join("\n"));
108
- }
109
- print("\n");
110
- }
111
- }
112
- }
113
- }
114
- });
115
-
116
- print("\n");
117
- },
118
- startLine: function(name) {
119
- var winners = Benchmark.map(this.winners(this.currentBenches), function(bench) {
120
- return bench.name.split(": ")[1];
121
- });
122
-
123
- this.currentBenches = [];
124
-
125
- print(winners.join(", "));
126
- print("\n");
127
- var padding = this.nameSize - name.length + 1;
128
- name = name + new Array(padding).join(" ");
129
- print(name);
130
- },
131
- writeBench: function(bench) {
132
- var out;
133
-
134
- if(!bench.error) {
135
- var count = bench.hz,
136
- moe = count * bench.stats.rme / 100;
137
-
138
- out = Math.round(count / 1000) + " ±" + Math.round(moe / 1000) + " (" + bench.cycles + ")";
139
- } else {
140
- out = "E";
141
- }
142
-
143
- var padding = this.benchSize - out.length + 1;
144
- out = out + new Array(padding).join(" ");
145
- print(out);
146
- }
147
- };
148
-
149
- module.exports = BenchWarmer;
@@ -1,172 +0,0 @@
1
- var BenchWarmer = require("./benchwarmer");
2
- Handlebars = require("../lib/handlebars");
3
-
4
- var dust, Mustache, eco;
5
-
6
- try {
7
- dust = require("dust");
8
- } catch (err) { /* NOP */ }
9
-
10
- try {
11
- Mustache = require("mustache");
12
- } catch (err) { /* NOP */ }
13
-
14
- try {
15
- var ecoExports = require("eco");
16
- eco = function(str) {
17
- return ecoExports(str);
18
- }
19
- } catch (err) { /* NOP */ }
20
-
21
- var benchDetails = {
22
- string: {
23
- context: {},
24
- handlebars: "Hello world",
25
- dust: "Hello world",
26
- mustache: "Hello world",
27
- eco: "Hello world"
28
- },
29
- variables: {
30
- context: {name: "Mick", count: 30},
31
- handlebars: "Hello {{name}}! You have {{count}} new messages.",
32
- dust: "Hello {name}! You have {count} new messages.",
33
- mustache: "Hello {{name}}! You have {{count}} new messages.",
34
- eco: "Hello <%= @name %>! You have <%= @count %> new messages."
35
- },
36
- object: {
37
- context: { person: { name: "Larry", age: 45 } },
38
- handlebars: "{{#with person}}{{name}}{{age}}{{/with}}",
39
- dust: "{#person}{name}{age}{/person}",
40
- mustache: "{{#person}}{{name}}{{age}}{{/person}}"
41
- },
42
- array: {
43
- context: { names: [{name: "Moe"}, {name: "Larry"}, {name: "Curly"}, {name: "Shemp"}] },
44
- handlebars: "{{#each names}}{{name}}{{/each}}",
45
- dust: "{#names}{name}{/names}",
46
- mustache: "{{#names}}{{name}}{{/names}}",
47
- eco: "<% for item in @names: %><%= item.name %><% end %>"
48
- },
49
- partial: {
50
- context: { peeps: [{name: "Moe", count: 15}, {name: "Larry", count: 5}, {name: "Curly", count: 1}] },
51
- partials: {
52
- mustache: { variables: "Hello {{name}}! You have {{count}} new messages." },
53
- handlebars: { variables: "Hello {{name}}! You have {{count}} new messages." }
54
- },
55
- handlebars: "{{#each peeps}}{{>variables}}{{/each}}",
56
- dust: "{#peeps}{>variables/}{/peeps}",
57
- mustache: "{{#peeps}}{{>variables}}{{/peeps}}"
58
- },
59
- recursion: {
60
- context: { name: '1', kids: [{ name: '1.1', kids: [{name: '1.1.1', kids: []}] }] },
61
- partials: {
62
- mustache: { recursion: "{{name}}{{#kids}}{{>recursion}}{{/kids}}" },
63
- handlebars: { recursion: "{{name}}{{#each kids}}{{>recursion}}{{/each}}" }
64
- },
65
- handlebars: "{{name}}{{#each kids}}{{>recursion}}{{/each}}",
66
- dust: "{name}{#kids}{>recursion:./}{/kids}",
67
- mustache: "{{name}}{{#kids}}{{>recursion}}{{/kids}}"
68
- },
69
- complex: {
70
- handlebars: "<h1>{{header}}</h1>{{#if items}}<ul>{{#each items}}{{#if current}}" +
71
- "<li><strong>{{name}}</strong></li>{{^}}" +
72
- "<li><a href=\"{{url}}\">{{name}}</a></li>{{/if}}" +
73
- "{{/each}}</ul>{{^}}<p>The list is empty.</p>{{/if}}",
74
-
75
- dust: "<h1>{header}</h1>\n" +
76
- "{?items}\n" +
77
- " <ul>\n" +
78
- " {#items}\n" +
79
- " {#current}\n" +
80
- " <li><strong>{name}</strong></li>\n" +
81
- " {:else}\n" +
82
- " <li><a href=\"{url}\">{name}</a></li>\n" +
83
- " {/current}\n" +
84
- " {/items}\n" +
85
- " </ul>\n" +
86
- "{:else}\n" +
87
- " <p>The list is empty.</p>\n" +
88
- "{/items}",
89
- context: {
90
- header: function() {
91
- return "Colors";
92
- },
93
- items: [
94
- {name: "red", current: true, url: "#Red"},
95
- {name: "green", current: false, url: "#Green"},
96
- {name: "blue", current: false, url: "#Blue"}
97
- ]
98
- }
99
- }
100
-
101
- };
102
-
103
- handlebarsTemplates = {};
104
- ecoTemplates = {};
105
-
106
- var warmer = new BenchWarmer();
107
-
108
- var makeSuite = function(name) {
109
- warmer.suite(name, function(bench) {
110
- var templateName = name;
111
- var details = benchDetails[templateName];
112
- var mustachePartials = details.partials && details.partials.mustache;
113
- var mustacheSource = details.mustache;
114
- var context = details.context;
115
-
116
- var error = function() { throw new Error("EWOT"); };
117
-
118
- if (dust) {
119
- bench("dust", function() {
120
- dust.render(templateName, context, function(err, out) { });
121
- });
122
- }
123
-
124
- bench("handlebars", function() {
125
- handlebarsTemplates[templateName](context);
126
- });
127
-
128
- if (eco) {
129
- if(ecoTemplates[templateName]) {
130
- bench("eco", function() {
131
- ecoTemplates[templateName](context);
132
- });
133
- } else {
134
- bench("eco", error);
135
- }
136
- }
137
-
138
- if (Mustache && mustacheSource) {
139
- bench("mustache", function() {
140
- Mustache.to_html(mustacheSource, context, mustachePartials);
141
- });
142
- } else {
143
- bench("mustache", error);
144
- }
145
- });
146
- }
147
-
148
- for(var name in benchDetails) {
149
- if(benchDetails.hasOwnProperty(name)) {
150
- if (dust) {
151
- dust.loadSource(dust.compile(benchDetails[name].dust, name));
152
- }
153
- handlebarsTemplates[name] = Handlebars.compile(benchDetails[name].handlebars);
154
-
155
- if (eco && benchDetails[name].eco) {
156
- ecoTemplates[name] = eco(benchDetails[name].eco);
157
- }
158
-
159
- var partials = benchDetails[name].partials;
160
- if(partials) {
161
- for(var partialName in partials.handlebars) {
162
- if(partials.handlebars.hasOwnProperty(partialName)) {
163
- Handlebars.registerPartial(partialName, partials.handlebars[partialName]);
164
- }
165
- }
166
- }
167
-
168
- makeSuite(name);
169
- }
170
- }
171
-
172
- warmer.bench();
@@ -1,193 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- var optimist = require('optimist')
4
- .usage('Precompile handlebar templates.\nUsage: $0 template...', {
5
- 'f': {
6
- 'type': 'string',
7
- 'description': 'Output File',
8
- 'alias': 'output'
9
- },
10
- 'a': {
11
- 'type': 'boolean',
12
- 'description': 'Exports amd style (require.js)',
13
- 'alias': 'amd'
14
- },
15
- 'c': {
16
- 'type': 'string',
17
- 'description': 'Exports CommonJS style, path to Handlebars module',
18
- 'alias': 'commonjs',
19
- 'default': null
20
- },
21
- 'h': {
22
- 'type': 'string',
23
- 'description': 'Path to handlebar.js (only valid for amd-style)',
24
- 'alias': 'handlebarPath',
25
- 'default': ''
26
- },
27
- 'k': {
28
- 'type': 'string',
29
- 'description': 'Known helpers',
30
- 'alias': 'known'
31
- },
32
- 'o': {
33
- 'type': 'boolean',
34
- 'description': 'Known helpers only',
35
- 'alias': 'knownOnly'
36
- },
37
- 'm': {
38
- 'type': 'boolean',
39
- 'description': 'Minimize output',
40
- 'alias': 'min'
41
- },
42
- 'n': {
43
- 'type': 'string',
44
- 'description': 'Template namespace',
45
- 'alias': 'namespace',
46
- 'default': 'Handlebars.templates'
47
- },
48
- 's': {
49
- 'type': 'boolean',
50
- 'description': 'Output template function only.',
51
- 'alias': 'simple'
52
- },
53
- 'r': {
54
- 'type': 'string',
55
- 'description': 'Template root. Base value that will be stripped from template names.',
56
- 'alias': 'root'
57
- },
58
- 'p' : {
59
- 'type': 'boolean',
60
- 'description': 'Compiling a partial template',
61
- 'alias': 'partial'
62
- },
63
- 'd' : {
64
- 'type': 'boolean',
65
- 'description': 'Include data when compiling',
66
- 'alias': 'data'
67
- }
68
- })
69
-
70
- .check(function(argv) {
71
- var template = [0];
72
- if (!argv._.length) {
73
- throw 'Must define at least one template or directory.';
74
- }
75
-
76
- argv._.forEach(function(template) {
77
- try {
78
- fs.statSync(template);
79
- } catch (err) {
80
- throw 'Unable to open template file "' + template + '"';
81
- }
82
- });
83
- })
84
- .check(function(argv) {
85
- if (argv.simple && argv.min) {
86
- throw 'Unable to minimze simple output';
87
- }
88
- if (argv.simple && (argv._.length !== 1 || fs.statSync(argv._[0]).isDirectory())) {
89
- throw 'Unable to output multiple templates in simple mode';
90
- }
91
- });
92
-
93
- var fs = require('fs'),
94
- handlebars = require('../lib/handlebars'),
95
- basename = require('path').basename,
96
- uglify = require('uglify-js');
97
-
98
- var argv = optimist.argv,
99
- template = argv._[0];
100
-
101
- // Convert the known list into a hash
102
- var known = {};
103
- if (argv.known && !Array.isArray(argv.known)) {
104
- argv.known = [argv.known];
105
- }
106
- if (argv.known) {
107
- for (var i = 0, len = argv.known.length; i < len; i++) {
108
- known[argv.known[i]] = true;
109
- }
110
- }
111
-
112
- var output = [];
113
- if (!argv.simple) {
114
- if (argv.amd) {
115
- output.push('define([\'' + argv.handlebarPath + 'handlebars\'], function(Handlebars) {\n');
116
- } else if (argv.commonjs) {
117
- output.push('var Handlebars = require("' + argv.commonjs + '");');
118
- } else {
119
- output.push('(function() {\n');
120
- }
121
- output.push(' var template = Handlebars.template, templates = ');
122
- output.push(argv.namespace);
123
- output.push(' = ');
124
- output.push(argv.namespace);
125
- output.push(' || {};\n');
126
- }
127
- function processTemplate(template, root) {
128
- var path = template,
129
- stat = fs.statSync(path);
130
- if (stat.isDirectory()) {
131
- fs.readdirSync(template).map(function(file) {
132
- var path = template + '/' + file;
133
-
134
- if (/\.handlebars$/.test(path) || fs.statSync(path).isDirectory()) {
135
- processTemplate(path, root || template);
136
- }
137
- });
138
- } else {
139
- var data = fs.readFileSync(path, 'utf8');
140
-
141
- var options = {
142
- knownHelpers: known,
143
- knownHelpersOnly: argv.o
144
- };
145
-
146
- if (argv.data) {
147
- options.data = true;
148
- }
149
-
150
- // Clean the template name
151
- if (!root) {
152
- template = basename(template);
153
- } else if (template.indexOf(root) === 0) {
154
- template = template.substring(root.length+1);
155
- }
156
- template = template.replace(/\.handlebars$/, '');
157
-
158
- if (argv.simple) {
159
- output.push(handlebars.precompile(data, options) + '\n');
160
- } else if (argv.partial) {
161
- output.push('Handlebars.partials[\'' + template + '\'] = template(' + handlebars.precompile(data, options) + ');\n');
162
- } else {
163
- output.push('templates[\'' + template + '\'] = template(' + handlebars.precompile(data, options) + ');\n');
164
- }
165
- }
166
- }
167
-
168
- argv._.forEach(function(template) {
169
- processTemplate(template, argv.root);
170
- });
171
-
172
- // Output the content
173
- if (!argv.simple) {
174
- if (argv.amd) {
175
- output.push('});');
176
- } else if (!argv.commonjs) {
177
- output.push('})();');
178
- }
179
- }
180
- output = output.join('');
181
-
182
- if (argv.min) {
183
- var ast = uglify.parser.parse(output);
184
- ast = uglify.uglify.ast_mangle(ast);
185
- ast = uglify.uglify.ast_squeeze(ast);
186
- output = uglify.uglify.gen_code(ast);
187
- }
188
-
189
- if (argv.output) {
190
- fs.writeFileSync(argv.output, output, 'utf8');
191
- } else {
192
- console.log(output);
193
- }