steering 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,16 +1,29 @@
1
1
  # Steering
2
2
 
3
- Steering is a bridge to the [Handlebars.js][1] template precompiler. By precompiling your templates you can speed up page loading in two ways - firstly the final compilation step is much quicker as the template source code does not have to be parsed and the size of the library can be reduced by using the smaller runtime library if all your templates are precompiled.
3
+ Steering is a bridge to the [Handlebars.js][1] template precompiler. By precompiling
4
+ your templates you can speed up page loading in two ways - firstly the final compilation
5
+ step is much quicker as the template source code does not have to be parsed and the
6
+ size of the library can be reduced by using the smaller runtime library if all your
7
+ templates are precompiled.
8
+
9
+ $ irb
4
10
 
5
11
  require "steering"
6
-
7
- Steering.compile(File.read("template.hb"))
8
- # => "function(...) {...}"
9
-
12
+ # => true
13
+
14
+ Steering.compile(File.read("example/mytemplate.handlebars"))
15
+ # => "function(Handlebars,...) {...}"
16
+
17
+ # This will create a file called "mytemplate.js" in the supplied
18
+ # example folder and return the size of the resulting file in bytes.
19
+ # Make sure to load "index.html" before and after in your favourite browser!
20
+ Steering.compile_to_file("example/mytemplate.handlebars", "example/mytemplate.js")
21
+ # => 1069
22
+
10
23
  context = Steering.context_for("Hello {{ name }}")
11
24
  context.call("template", :name => "Andrew")
12
25
  # => "Hello Andrew"
13
-
26
+
14
27
  Steering.render("Hello {{ name }}", :name => "world")
15
28
  # => "Hello world"
16
29
 
@@ -20,21 +33,76 @@ Steering is a bridge to the [Handlebars.js][1] template precompiler. By precompi
20
33
 
21
34
  ## Dependencies
22
35
 
23
- This library depends on the `steering-source` gem which is updated any time a new version of Handlebars.js is released (The `steering-source` gem's version number is synced with each official Handlebars.js release). This way you can build against different versions of Handlebars.js by requiring the correct version of the `steering-source` gem.
36
+ This library depends on the `steering-source` gem which is updated any time a
37
+ new version of Handlebars.js is released (The `steering-source` gem's version
38
+ number is synced with each official Handlebars.js release). This way you can
39
+ build against different versions of Handlebars.js by requiring the correct
40
+ version of the `steering-source` gem.
24
41
 
25
- In addition, you can use this library with unreleased versions of Handlebars.js by setting the `HANDLEBARS_SOURCE_PATH` and `HANDLEBARS_RUNTIME_PATH` environment variable:
42
+ In addition, you can use this library with unreleased versions of Handlebars.js
43
+ by setting the `HANDLEBARS_SOURCE_PATH` and `HANDLEBARS_RUNTIME_PATH`
44
+ environment variable:
26
45
 
27
46
  export HANDLEBARS_SOURCE_PATH=/path/to/handlebars.js
28
47
  export HANDLEBARS_RUNTIME_PATH=/path/to/handlebars.runtime.js
29
48
 
30
49
  ### ExecJS
31
50
 
32
- The [ExecJS][2] library is used to automatically choose the best JavaScript engine for your platform. Check out its [README][3] for a complete list of supported engines.
51
+ The [ExecJS][2] library is used to automatically choose the best JavaScript engine
52
+ for your platform. Check out its [README][3] for a complete list of supported engines.
33
53
 
34
54
  ## Acknowledgements
35
55
 
36
56
  The structure and code patterns for this gem were derived from the [Ruby Eco][4] gem
37
57
 
58
+ ## Contributing
59
+
60
+ You can check out the Steering source code from GitHub:
61
+
62
+ $ git clone http://github.com/pixeltrix/steering.git
63
+
64
+ To run Steerings's test suite run `rake test`.
65
+
66
+ Report bugs on the [GitHub issue tracker](http://github.com/pixeltrix/steering/issues).
67
+
68
+ ## Special thanks
69
+
70
+ * Daniel Demmel <dain@danieldemmel.me>
71
+
72
+ ## Changelog
73
+
74
+ ### 1.1.0
75
+
76
+ * Added 'compile_to_file' method. *Daniel Demmel*
77
+ * Added precompile workflow example files. *Daniel Demmel*
78
+
79
+ ### 1.0.0
80
+
81
+ Initial version.
82
+
83
+ ## License (MIT)
84
+
85
+ Copyright (c) 2012 Andrew White <andyw@pixeltrix.co.uk>
86
+
87
+ Permission is hereby granted, free of charge, to any person obtaining
88
+ a copy of this software and associated documentation files (the
89
+ "Software"), to deal in the Software without restriction, including
90
+ without limitation the rights to use, copy, modify, merge, publish,
91
+ distribute, sublicense, and/or sell copies of the Software, and to
92
+ permit persons to whom the Software is furnished to do so, subject to
93
+ the following conditions:
94
+
95
+ The above copyright notice and this permission notice shall be
96
+ included in all copies or substantial portions of the Software.
97
+
98
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
99
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
100
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
101
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
102
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
103
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
104
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
105
+
38
106
  [1]: https://github.com/wycats/handlebars.js
39
107
  [2]: https://github.com/sstephenson/execjs
40
108
  [3]: https://github.com/sstephenson/execjs/blob/master/README.md
@@ -0,0 +1,16 @@
1
+ <html lang="en-GB">
2
+ <head>
3
+ <script type="text/javascript" src="https://raw.github.com/pixeltrix/steering-source/master/lib/steering/handlebars.runtime.js"></script>
4
+ <script type="text/javascript" src="mytemplate.js"></script>
5
+ </head>
6
+
7
+ <body>
8
+ <div id="placeholder">This example will only work if you precompile mytemplate.handlebars. See README!</div>
9
+ </body>
10
+
11
+ <script type="text/javascript">
12
+ template = Handlebars.templates['mytemplate'];
13
+ var placeholderElement = document.getElementById('placeholder');
14
+ placeholderElement.innerHTML = template({title: 'My title', body: "My body"});
15
+ </script>
16
+ </html>
@@ -0,0 +1,6 @@
1
+ <div class="entry">
2
+ <h1>{{title}}</h1>
3
+ <div class="body">
4
+ {{body}}
5
+ </div>
6
+ </div>
data/lib/steering.rb CHANGED
@@ -48,6 +48,15 @@ module Steering
48
48
  Source.context.call("Handlebars.precompile", template, { :knownHelpers => known_helpers })
49
49
  end
50
50
 
51
+ def compile_to_file(template, file, extension = ".handlebars")
52
+ File.open(file, 'w') do |f|
53
+ name = File.basename(template, extension)
54
+ template = File.read(template)
55
+ f.write("\nHandlebars.templates = Handlebars.templates || {};")
56
+ f.write("\nHandlebars.templates['#{name}'] = Handlebars.template(#{compile(template)});\n")
57
+ end
58
+ end
59
+
51
60
  def context_for(template, extra = "")
52
61
  ExecJS.compile("#{Source.runtime}; #{extra}; var template = Handlebars.template(#{compile(template)})")
53
62
  end
@@ -1,3 +1,3 @@
1
1
  module Steering
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -4,6 +4,10 @@ require "test/unit"
4
4
 
5
5
  class SteeringTest < Test::Unit::TestCase
6
6
  JS_FUNCTION_PATTERN = /^function\s*\(.*?\)\s*\{.*\}$/m
7
+ HB_PREAMBLE = /Handlebars\.templates\s*=\s*Handlebars.templates\s\|\|\s*\{\};/
8
+ HB_ASSIGNMENT = /Handlebars\.templates\['\w+'\]\s*=/
9
+ HB_TEMPLATE = /Handlebars\.template\(function\s*\(.*?\)\s*\{.*\}\);/m
10
+ HB_TEMPLATE_PATTERN = /^\n#{HB_PREAMBLE}\n#{HB_ASSIGNMENT}\s*#{HB_TEMPLATE}\s*$/m
7
11
 
8
12
  def test_version
9
13
  assert_equal Steering::Source::VERSION, Steering.version
@@ -24,6 +28,18 @@ class SteeringTest < Test::Unit::TestCase
24
28
  end
25
29
  end
26
30
 
31
+ def test_compile_file
32
+ file = "example/mytemplate.handlebars"
33
+ compiled_file = "example/mytemplate.js"
34
+
35
+ Steering.compile_to_file(file, compiled_file)
36
+ compiled_source = File.read(compiled_file)
37
+
38
+ assert_match HB_TEMPLATE_PATTERN, compiled_source
39
+ ensure
40
+ File.delete(compiled_file) if File.exists?(compiled_file)
41
+ end
42
+
27
43
  def test_context_for
28
44
  context = Steering.context_for("Hello {{ name }}")
29
45
  assert_equal "Hello Andrew", context.call("template", :name => "Andrew")
metadata CHANGED
@@ -1,106 +1,89 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: steering
3
- version: !ruby/object:Gem::Version
4
- hash: 23
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 0
9
- - 0
10
- version: 1.0.0
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Andrew White
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-03-21 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-05-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: execjs
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 27
29
- segments:
30
- - 1
31
- - 3
32
- - 0
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
33
21
  version: 1.3.0
34
22
  type: :runtime
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: steering-source
38
23
  prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.3.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: steering-source
32
+ requirement: !ruby/object:Gem::Requirement
40
33
  none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- hash: 2469281371
45
- segments:
46
- - 1
47
- - 0
48
- - beta
49
- - 6
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
50
37
  version: 1.0.beta.6
51
38
  type: :runtime
52
- version_requirements: *id002
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.0.beta.6
53
46
  description: Steering is a bridge to the official JavaScript Handlebars.js compiler.
54
- email:
47
+ email:
55
48
  - andyw@pixeltrix.co.uk
56
49
  executables: []
57
-
58
50
  extensions: []
59
-
60
51
  extra_rdoc_files: []
61
-
62
- files:
52
+ files:
63
53
  - .gitignore
64
54
  - Gemfile
65
55
  - LICENSE
66
56
  - README.md
67
57
  - Rakefile
58
+ - example/index.html
59
+ - example/mytemplate.handlebars
68
60
  - lib/steering.rb
69
61
  - lib/steering/version.rb
70
62
  - steering.gemspec
71
63
  - test/steering_test.rb
72
64
  homepage: https://github.com/pixeltrix/steering
73
65
  licenses: []
74
-
75
66
  post_install_message:
76
67
  rdoc_options: []
77
-
78
- require_paths:
68
+ require_paths:
79
69
  - lib
80
- required_ruby_version: !ruby/object:Gem::Requirement
70
+ required_ruby_version: !ruby/object:Gem::Requirement
81
71
  none: false
82
- requirements:
83
- - - ">="
84
- - !ruby/object:Gem::Version
85
- hash: 3
86
- segments:
87
- - 0
88
- version: "0"
89
- required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
77
  none: false
91
- requirements:
92
- - - ">="
93
- - !ruby/object:Gem::Version
94
- hash: 3
95
- segments:
96
- - 0
97
- version: "0"
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
98
82
  requirements: []
99
-
100
83
  rubyforge_project:
101
- rubygems_version: 1.8.14
84
+ rubygems_version: 1.8.22
102
85
  signing_key:
103
86
  specification_version: 3
104
87
  summary: Ruby Handlebars.js Compiler
105
- test_files:
88
+ test_files:
106
89
  - test/steering_test.rb