ember-handlebars-template 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d6ad7a61304a8e82244e186036b673eda9bc5dc2
4
+ data.tar.gz: 3b9c2a0bec6ac9b4a9d7e404d51499e213b93798
5
+ SHA512:
6
+ metadata.gz: 4ef634b2a82254509ba8102f76ee0c08256bdc20333a314e0efba7bc45d9b45751e94cdde4af25567c7b256b37e25d8183185e4001aa21e4320ed1704500c700
7
+ data.tar.gz: d388ff94f0f10bc60f0db857c5c83d2da80cf8ac156634664f159c0cda1c660120c3bc7ded3ea976ca65408acfd26bfcba1b256bd1cc271a63dffb50571bd2be
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ sudo: false
3
+ cache: bundler
4
+ rvm:
5
+ - 2.0.0
6
+ - 2.1
7
+ - 2.2
8
+ env:
9
+ matrix:
10
+ - SPROCKETS_VERSION="~> 2.1.0"
11
+ - SPROCKETS_VERSION="~> 2.12.0"
12
+ - SPROCKETS_VERSION="~> 3.0.0.beta.10"
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ember-handlebars-template.gemspec
4
+ gemspec
5
+
6
+ gem 'sprockets', ENV['SPROCKETS_VERSION'] if ENV['SPROCKETS_VERSION']
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Ryunosuke SATO
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,91 @@
1
+ # Ember::Handlebars::Template
2
+
3
+ The sprockets template for Ember Handlebars.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ember-handlebars-template'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```
22
+ $ gem install ember-handlebars-template
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ``` ruby
28
+ Sprockets.register_engine '.hbs', Ember::Handlebars::Template # or other extension which you like.
29
+ ```
30
+
31
+ ## Options
32
+
33
+ You can overwrite config as the followings:
34
+
35
+ ``` ruby
36
+ Ember::Handlebars::Template.configure do |config|
37
+ config.precompile = true
38
+
39
+ # You can overwrite other config
40
+ end
41
+ ```
42
+
43
+ ### precompile
44
+
45
+ Type: `Boolean`
46
+
47
+ Enables or disables precompilation.(default: `true`)
48
+
49
+ ### ember_template
50
+
51
+ Type: `String`
52
+
53
+ Default which Ember template type to compile. `HTMLBars` / `Handlebars`. (default: `HTMLBars`)
54
+
55
+ ### output_type
56
+
57
+ Type: `Symbol`
58
+
59
+ Configures the style of output. `:global` / `:amd`. (default `:global`)
60
+
61
+ ### amd_namespace
62
+
63
+ Type: `String`
64
+
65
+ Configures the module prefix for AMD formatted output. (default: `nil`)
66
+
67
+ ### templates_root
68
+
69
+ Type: `String`
70
+
71
+ Sets the root path for templates to be looked up in. (default: `templates`)
72
+
73
+ ### templates_path_separator
74
+
75
+ Type: `String`
76
+
77
+ The path separator to use for templates. (default: `/`)
78
+
79
+ ## Development
80
+
81
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
82
+
83
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
84
+
85
+ ## Contributing
86
+
87
+ 1. Fork it
88
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
89
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
90
+ 4. Push to the branch (`git push origin my-new-feature`)
91
+ 5. Create a new Pull Request
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ task :default => :test
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ t.warning = true
9
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'ember/handlebars/template'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'ember/handlebars/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'ember-handlebars-template'
7
+ spec.version = Ember::Handlebars::VERSION
8
+ spec.authors = ['Ryunosuke SATO']
9
+ spec.email = ['tricknotes.rs@gmail.com']
10
+
11
+ spec.summary = %q{The sprockets template for Ember Handlebars.}
12
+ spec.description = %q{The sprockets template for Ember Handlebars.}
13
+ spec.homepage = 'https://github.com/tricknotes/ember-handlebars-template'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'sprockets', '>= 2.1', '< 3.1'
22
+ spec.add_dependency 'barber', '>= 0.8.0'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.7'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'minitest'
27
+ end
@@ -0,0 +1,21 @@
1
+ module Ember
2
+ module Handlebars
3
+ class Config
4
+ attr_accessor :precompile,
5
+ :ember_template,
6
+ :output_type,
7
+ :amd_namespace,
8
+ :templates_root,
9
+ :templates_path_separator
10
+
11
+ def initialize
12
+ self.precompile = true
13
+ self.ember_template = 'HTMLBars'
14
+ self.output_type = :global
15
+ self.amd_namespace = nil
16
+ self.templates_root = 'templates'
17
+ self.templates_path_separator = '/'
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,65 @@
1
+ module Ember
2
+ module Handlebars
3
+ module Helper
4
+ private
5
+
6
+ def handlebars?(filename)
7
+ filename.to_s =~ /\.raw\.(handlebars|hjs|hbs)/
8
+ end
9
+
10
+ def mustache_to_handlebars(filename, template)
11
+ if filename =~ /\.mustache\.(handlebars|hjs|hbs)/
12
+ template.gsub(/\{\{(\w[^\}]+)\}\}/){ |x| "{{unbound #{$1}}}" }
13
+ else
14
+ template
15
+ end
16
+ end
17
+
18
+ def amd_template_target(namespace, module_name)
19
+ [namespace, module_name].compact.join('/')
20
+ end
21
+
22
+ def global_template_target(module_name, config)
23
+ "Ember.TEMPLATES[#{template_path(module_name, config).inspect}]"
24
+ end
25
+
26
+ def template_path(path, config)
27
+ root = config.templates_root
28
+
29
+ if root.kind_of? Array
30
+ root.each do |root|
31
+ path.sub!(/#{Regexp.quote(root)}\//, '')
32
+ end
33
+ else
34
+ unless root.empty?
35
+ path.sub!(/#{Regexp.quote(root)}\/?/, '')
36
+ end
37
+ end
38
+
39
+ path = path.split('/')
40
+
41
+ path.join(config.templates_path_separator)
42
+ end
43
+
44
+ def compile_handlebars(string)
45
+ "Handlebars.compile(#{indent(string).inspect});"
46
+ end
47
+
48
+ def precompile_handlebars(string)
49
+ "Handlebars.template(#{Barber::Precompiler.compile(string)});"
50
+ end
51
+
52
+ def compile_ember_handlebars(string, ember_template = 'Handlebars')
53
+ "Ember.#{ember_template}.compile(#{indent(string).inspect});"
54
+ end
55
+
56
+ def precompile_ember_handlebars(string, ember_template = 'Handlebars')
57
+ "Ember.#{ember_template}.template(#{Barber::Ember::Precompiler.compile(string)});"
58
+ end
59
+
60
+ def indent(string)
61
+ string.gsub(/$(.)/m, "\\1 ").strip
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,19 @@
1
+ require 'sprockets'
2
+ require 'barber'
3
+
4
+ module Ember
5
+ module Handlebars
6
+ autoload :VERSION, 'ember/handlebars/version'
7
+ autoload :Config, 'ember/handlebars/config'
8
+ autoload :Helper, 'ember/handlebars/helper'
9
+
10
+ case Sprockets::VERSION
11
+ when /\A2\./
12
+ autoload :Template, 'ember/handlebars/templates/sprockets2'
13
+ when /\A3\./
14
+ autoload :Template, 'ember/handlebars/templates/sprockets3'
15
+ else
16
+ raise "Unsupported sprockets version: #{Sprockets::VERSION}"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,70 @@
1
+ module Ember
2
+ module Handlebars
3
+ class Template < Tilt::Template
4
+ include Helper
5
+
6
+ class << self
7
+ def configure
8
+ yield config
9
+ end
10
+
11
+ def default_mime_type
12
+ 'application/javascript'
13
+ end
14
+
15
+ def setup(env)
16
+ env.register_engine '.hbs', self
17
+ end
18
+
19
+ def config
20
+ @config ||= Config.new
21
+ end
22
+ end
23
+
24
+ def prepare; end
25
+
26
+ def evaluate(scope, locals, &block)
27
+ filename = scope.pathname.to_s
28
+
29
+ raw = handlebars?(filename)
30
+
31
+ if raw
32
+ template = data
33
+ else
34
+ template = mustache_to_handlebars(filename, data)
35
+ end
36
+
37
+ if config.precompile
38
+ if raw
39
+ template = precompile_handlebars(template)
40
+ else
41
+ template = precompile_ember_handlebars(template, config.ember_template)
42
+ end
43
+ else
44
+ if raw
45
+ template = compile_handlebars(data)
46
+ else
47
+ template = compile_ember_handlebars(template, config.ember_template)
48
+ end
49
+ end
50
+
51
+ case config.output_type
52
+ when :amd
53
+ target = amd_template_target(config.amd_namespace, scope.logical_path.split(".").first)
54
+
55
+ "define('#{target}', ['exports'], function(__exports__){ __exports__['default'] = #{template} });"
56
+ when :global
57
+ target = global_template_target(scope.logical_path, config)
58
+
59
+ "#{target} = #{template}\n"
60
+ else
61
+ raise "Unsupported `output_type`: #{config.output_type}"
62
+ end
63
+ end
64
+
65
+ def config
66
+ @config ||= self.class.config.dup
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,108 @@
1
+ module Ember
2
+ module Handlebars
3
+ class Template
4
+ include Helper
5
+
6
+ class << self
7
+ def configure
8
+ yield config
9
+ end
10
+
11
+ def config
12
+ @config ||= Config.new
13
+ end
14
+
15
+ def setup(env)
16
+ env.register_engine '.hbs', self, mime_type: 'application/javascript'
17
+ end
18
+
19
+ def instance
20
+ @instance ||= new(config)
21
+ end
22
+
23
+ def call(input)
24
+ instance.call(input)
25
+ end
26
+ end
27
+
28
+ attr_reader :config
29
+
30
+ def initialize(config = self.class.config.dup)
31
+ @config = config
32
+ end
33
+
34
+ def call(input)
35
+ data = input[:data]
36
+ filename = input[:filename]
37
+
38
+ raw = handlebars?(filename)
39
+
40
+ if raw
41
+ template = data
42
+ else
43
+ template = mustache_to_handlebars(filename, data)
44
+ end
45
+
46
+ if config.precompile
47
+ if raw
48
+ template = precompile_handlebars(template)
49
+ else
50
+ template = precompile_ember_handlebars(template, config.ember_template, input)
51
+ end
52
+ else
53
+ if raw
54
+ template = compile_handlebars(data)
55
+ else
56
+ template = compile_ember_handlebars(template, config.ember_template)
57
+ end
58
+ end
59
+
60
+ case config.output_type
61
+ when :amd
62
+ target = amd_template_target(config.amd_namespace, input[:name])
63
+
64
+ "define('#{target}', ['exports'], function(__exports__){ __exports__['default'] = #{template} });"
65
+ when :global
66
+ target = global_template_target(input[:name], config)
67
+
68
+ "#{target} = #{template}\n"
69
+ else
70
+ raise "Unsupported `output_type`: #{config.output_type}"
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ def precompile_handlebars(template, input)
77
+ dependencies = [
78
+ Barber::Precompiler.compiler_version,
79
+ template,
80
+ ]
81
+
82
+ input[:cache].fetch(cache_key + dependencies) do
83
+ super(template)
84
+ end
85
+ end
86
+
87
+ def precompile_ember_handlebars(template, ember_template, input)
88
+ dependencies = [
89
+ Barber::Ember::Precompiler.compiler_version,
90
+ ember_template,
91
+ template
92
+ ]
93
+
94
+ input[:cache].fetch(cache_key + dependencies) do
95
+ super(template, ember_template)
96
+ end
97
+ end
98
+
99
+ def cache_key
100
+ [
101
+ self.class.name,
102
+ VERSION,
103
+ Barber::VERSION
104
+ ]
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,5 @@
1
+ module Ember
2
+ module Handlebars
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ember-handlebars-template
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryunosuke SATO
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sprockets
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3.1'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '2.1'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.1'
33
+ - !ruby/object:Gem::Dependency
34
+ name: barber
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 0.8.0
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 0.8.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.7'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.7'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: minitest
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ description: The sprockets template for Ember Handlebars.
90
+ email:
91
+ - tricknotes.rs@gmail.com
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitignore"
97
+ - ".travis.yml"
98
+ - Gemfile
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - bin/console
103
+ - bin/setup
104
+ - ember-handlebars-template.gemspec
105
+ - lib/ember/handlebars/config.rb
106
+ - lib/ember/handlebars/helper.rb
107
+ - lib/ember/handlebars/template.rb
108
+ - lib/ember/handlebars/templates/sprockets2.rb
109
+ - lib/ember/handlebars/templates/sprockets3.rb
110
+ - lib/ember/handlebars/version.rb
111
+ homepage: https://github.com/tricknotes/ember-handlebars-template
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.4.5
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: The sprockets template for Ember Handlebars.
135
+ test_files: []