angular-rails-templates 0.0.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 89d964f875011e9189ab1360595c5761ab4ce256
4
- data.tar.gz: 73ce6e9e285e9562531a7f87e22b437c132d3cb0
3
+ metadata.gz: 1a0382ea27ff3c7b1cfb6ea0e66196406cea4f93
4
+ data.tar.gz: bd48df6bc60a92033beb772507409bf70e4f2af2
5
5
  SHA512:
6
- metadata.gz: 4da72ed50555f8993d4e34af7e767db53b4cba3f606fa2ef8be8c042e972ee35dfcecd432127e42c7e8e010ffd3ab3b6fbdb3d116ae3e4467d7cf9cd7313cced
7
- data.tar.gz: 1f938a613811dced90f9ee7c53c4f64555174c737a2995862bff80a1d3ab1d4a82c4af51b059d5e6ea6fc1b2968cca1ca45f9198581cc1f7b9514972ace41d8f
6
+ metadata.gz: 2111bf9c7c7cfa83ca6fdcdd5a7c05821509f04b85f922ba73f8fb03c9f2ba2f06cc33a580408053ae4bf800b5e6722f4ba0c16b6f45c5977d1e77258a1ffe5a
7
+ data.tar.gz: 15b917f2a93c0cef94fb7a2fb040b4026110b78168072398b814d931333e4b3542be4d8e19085dd467dae5a530afd1624f541634846a76721d36f7888aa57d61
data/README.md CHANGED
@@ -6,7 +6,7 @@ It removes the need for AJAX calls to retrieve the templates (or for you to manu
6
6
 
7
7
  ## Usage
8
8
 
9
- ### 1. Add the gem
9
+ ### 1. Add the Gem
10
10
 
11
11
  In Gemfile
12
12
 
@@ -14,39 +14,188 @@ In Gemfile
14
14
  gem 'angular-rails-templates'
15
15
  ```
16
16
 
17
- ### 2. Include templates in JS
17
+ ### 2. Include Templates in Rails Asset Pipeline
18
18
 
19
- Then, in your `application.js` file, require your templates and the internal javascript file:
19
+ Then, in your `application.js` file, require `angular-rails-templates` and your templates:
20
20
 
21
21
  ```javascript
22
22
  //= require angularjs
23
- //= require_tree ./path_to_your_templates
23
+ // ...
24
+ //= require angular-rails-templates
25
+ //
26
+ // Templates in app/assets/javascript/templates
27
+ //= require_tree ./templates
28
+ // OR
29
+ // Templates in app/assets/templates
30
+ //= require_tree ../templates
24
31
  ```
25
32
 
26
- `path_to_your_templates` is relative to `application.js`. For example, your templates are under `app/assets/javascripts/my_app/templates` and you then `require_tree ./my_app/templates`
33
+ Make sure to `require angular-rails-templates` **before** you require your templates.
27
34
 
28
- Extensions supported are **.html**, **.ajs**, **.nghaml**, **.ngslim**. Last two do additional preprocessing and require `haml` and `slim` gems in your Gemfile.
35
+ Name your templates like you would name any other Rails view. **The `.html` part is required.** If it is not present your views will not be added to angular's template cache.
29
36
 
30
- ### 3. Add a dependency in Angular module
37
+ ```
38
+ foo.html
39
+ foo.html.erb
40
+ foo.html.haml
41
+ foo.html.slim
42
+ ```
43
+
44
+ Caution: *`.ngslim` and `.nghaml` are no longer supported!*
45
+
46
+ Angular Rails Templates will try to load support for the following markups if their gems are present:
47
+
48
+ | Extension | Required gem |
49
+ |---------- |----------------------------------------------------------|
50
+ | .erb | - |
51
+ | .str | - |
52
+ | .haml | haml |
53
+ | .slim | slim |
54
+ | .md | liquid, rdiscount, redcrpet, bluecloth, kramdown, maruku |
31
55
 
32
- Your Angular module needs to have `templates` as a dependency (configurable with `config.angular_templates.module_name`)
56
+ See [Advanced](#advanced-configuration) if you would like to use other markup languages.
57
+
58
+ ### 3. Add a Dependency in your Angular Application Module
59
+
60
+ Your Angular module needs to depend on the `templates` module. (configurable, see [Advanced Configuration](#configuration-option-module_name))
33
61
 
34
62
  ```javascript
35
- var application = angular.module('myApplication', ['templates']);
63
+ angular.module('myApplication', ['templates']);
36
64
  ```
37
65
 
38
- Loading this module will automatically load all your templates into angular's `$templateCache`.
39
- Angular uses this parameter when checking for the presence of templates before making an HTTP call.
66
+ ### 4. Use your Templates
67
+
68
+ No matter what the source file extension is, your template's url will be `#{base_name}.html`
69
+
70
+ For example:
71
+ ```ruby
72
+ main.html => main.html
73
+ widget.html.haml => widget.html
74
+ modals/confirm.html.slim => modals/confirm.html
75
+ modals/dialog.html.slim.erb.str => modals/dialog.html # don't do this
76
+ ```
40
77
 
41
78
  The templates can then be accessed via `templateUrl` as expected:
42
79
 
43
80
  ``` javascript
81
+ // Template: app/assets/templates/yourTemplate.html.haml
82
+ {
83
+ templateUrl: 'yourTemplate.html'
84
+ }
85
+ ```
86
+
87
+ ## Advanced Configuration
88
+
89
+ Angular Rails Templates has some configuration options that can be set inside `config/application.rb`
90
+
91
+ Here are their default values:
92
+ ```ruby
93
+ # config/application.rb
94
+ config.angular_templates.module_name = 'templates'
95
+ config.angular_templates.ignore_prefix = 'templates/'
96
+ config.angular_templates.markups = %w(erb str haml slim md)
97
+ config.angular_templates.htmlcompressor = false
98
+ ```
99
+
100
+ ### Configuration Option: `module_name`
101
+
102
+ This configures the module name that your templates will be placed into.
103
+ It is used to generate javascript like:
104
+
105
+ ```javascipt
106
+ angular.module("<%= module_name %>")...
107
+ ```
108
+
109
+ Although it is not recommended, you can set `module_name` to the name of your main application module and remove `require angular-rails-templates` from your javascript manifest to have your templates directly injected into your app.
110
+
111
+ ### Configuration Option: `ignore_prefix`
112
+
113
+ *If you place your templates in `app/assets/templates` this option is mostly useless.*
114
+
115
+ `ignore_prefix` will be stripped from the beginning of the `templateUrl` it reports to angularjs.
116
+
117
+ Since the default ignore_prefix is `templates/`, any templates placed under `app/assets/javascripts/templates` will automatically have short names. If your templates are not in this location, you will need to use the full path to the template.
118
+
119
+ You can set `config.angular_templates.ignore_prefix` to change the default ignore prefix. Default is `templates/`.
120
+
121
+
122
+ ``` javascript
123
+ // Templates in: app/assets/javascripts/templates (default)
124
+ // ignore_prefix: templates/ (default)
125
+ {
126
+ templateUrl: 'yourTemplate.html'
127
+ }
128
+ // This won't work:
129
+ {
130
+ templateUrl: 'templates/yourTemplate.html'
131
+ }
132
+ ```
133
+
134
+ ``` javascript
135
+ // Templates in: app/assets/javascripts/my_app/templates (custom)
136
+ // ignore_prefix: templates/ (default)
44
137
  {
45
138
  templateUrl: 'my_app/templates/yourTemplate.html'
46
139
  }
140
+
141
+ // ignore_prefix: my_app/templates/ (custom)
142
+ {
143
+ templateUrl: 'yourTemplate.html'
144
+ }
145
+ ```
146
+
147
+
148
+ ### Configuration Option: `markups`
149
+
150
+ Any markup that Tilt supports can be used, but you may need to add a gem to your Gemfile. See [Tilt](https://github.com/rtomayko/tilt) for a list of the supported markups and required libraries.
151
+
152
+ ```ruby
153
+ # Gemfile
154
+ gem "asciidoctor"
155
+ gem "radius"
156
+ gem "creole"
157
+ gem "tilt-handlebars"
158
+
159
+ # config/application.rb
160
+ config.angular_templates.markups.push 'asciidoc', 'radius', 'wiki', 'hbs'
161
+ ```
162
+ If you would like to use a non-standard extension or you would like to use a custom template, you just need to tell Tilt about it.
163
+
164
+ ```ruby
165
+ # config/initializers/angular_rails_templates.rb
166
+ Tilt.register Tilt::HamlTemplate, 'nghaml'
167
+
168
+ # config/application.rb
169
+ config.angular_templates.markups.push 'nghaml'
170
+ ```
171
+ Note: You would still need to use `foo`**`.html`**`.nghaml`
172
+
173
+
174
+ ### Configuration Option: `htmlcompressor`
175
+
176
+ The [htmlcompressor gem](https://github.com/paolochiodi/htmlcompressor) is in alpha, not activly maintained, and has several known bugs. Beware if you are using windows. The `simple_boolean_attributes` option is known to mangle angular templates. It depends on a three-year-old version of yui-compressor. However, it does a good job of compressing html!
177
+
178
+ If you would like to use htmlcompressor add it to your Gemfile and Enable the configuration option.
179
+
180
+ ```ruby
181
+ # Gemfile
182
+ gem 'htmlcompressor'
183
+ ```
184
+
185
+ ```ruby
186
+ # config/application.rb
187
+ config.angular_templates.htmlcompressor = true
188
+ ```
189
+
190
+ You can also pass an options hash to `htmlcompressor` that will be directly passed to ```HtmlCompressor::Compressor.new```. See the [ruby project](https://github.com/paolochiodi/htmlcompressor#usage) or the [java project](https://code.google.com/p/htmlcompressor/#Compressing_HTML_and_XML_files_from_a_command_line) for descriptions of the options.
191
+
192
+ ```ruby
193
+ # config/application.rb
194
+ config.angular_templates.htmlcompressor = {
195
+ :remove_quotes => true
196
+ }
47
197
  ```
48
198
 
49
- You can set `config.angular_templates.ignore_prefix` to remove part of the path to your templates. For example, set it to `my_app/templates/` so you can refer to your templates by simply `yourTemplate.html`. Default is `templates/`.
50
199
 
51
200
  ## License
52
201
 
@@ -1,12 +1,6 @@
1
- module AngularRailsTemplates
2
- HAML_EXT = '.nghaml'
3
- SLIM_EXT = '.ngslim'
1
+ require 'angular-rails-templates/engine'
4
2
 
5
- autoload :Template , 'angular-rails-templates/template'
6
- autoload :BaseTemplate , 'angular-rails-templates/base_template'
7
- autoload :SlimTemplate , 'angular-rails-templates/slim_template'
8
- autoload :HamlTemplate , 'angular-rails-templates/haml_template'
9
- autoload :Version , 'angular-rails-templates/version'
3
+ module AngularRailsTemplates
4
+ autoload :Template, 'angular-rails-templates/template'
5
+ autoload :VERSION, 'angular-rails-templates/version'
10
6
  end
11
-
12
- require 'angular-rails-templates/engine'
@@ -0,0 +1,29 @@
1
+ module AngularRailsTemplates
2
+ module CompactJavaScriptEscape
3
+ # inspired by Rails' action_view/helpers/javascript_helper.rb
4
+ JS_ESCAPE_MAP = {
5
+ '\\' => '\\\\',
6
+ "\r\n" => '\n',
7
+ "\n" => '\n',
8
+ "\r" => '\n',
9
+ '"' => '\\"',
10
+ "'" => "\\'"
11
+ }
12
+
13
+ # We want to deliver the shortist valid javascript escaped string
14
+ # Count the number of " vs '
15
+ # If more ', escape "
16
+ # If more ", escape '
17
+ # If equal, prefer to escape "
18
+
19
+ def escape_javascript(raw)
20
+ if raw
21
+ quote = raw.count(%{'}) >= raw.count(%{"}) ? %{"} : %{'}
22
+ escaped = raw.gsub(/(\\|\r\n|[\n\r#{quote}])/u) {|match| JS_ESCAPE_MAP[match] }
23
+ "#{quote}#{escaped}#{quote}"
24
+ else
25
+ '""'
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,18 +1,52 @@
1
1
  module AngularRailsTemplates
2
2
  class Engine < ::Rails::Engine
3
3
  config.angular_templates = ActiveSupport::OrderedOptions.new
4
- config.angular_templates.module_name = 'templates'
5
- config.angular_templates.ignore_prefix = 'templates/'
4
+ config.angular_templates.module_name = 'templates'
5
+ config.angular_templates.ignore_prefix = 'templates/'
6
+ config.angular_templates.markups = []
7
+ config.angular_templates.htmlcompressor = false
8
+
9
+ # try loading common markups
10
+ %w(erb haml liquid md radius slim str textile wiki).
11
+ each do |ext|
12
+ begin
13
+ config.angular_templates.markups << ext if Tilt[ext]
14
+ rescue LoadError
15
+ # They don't have the required libray required. Oh well.
16
+ end
17
+ end
18
+
6
19
 
7
20
  config.before_initialize do |app|
8
21
  if app.config.assets
9
22
  require 'sprockets'
10
- Sprockets::Engines #force autoloading
23
+ require 'sprockets/engines' # load sprockets for Rails 3
24
+
25
+ if app.config.angular_templates.htmlcompressor
26
+ require 'htmlcompressor/compressor'
27
+ unless app.config.angular_templates.htmlcompressor.is_a? Hash
28
+ app.config.angular_templates.htmlcompressor = {remove_intertag_spaces: true}
29
+ end
30
+ end
31
+
32
+ # These engines render markup as HTML
33
+ app.config.angular_templates.markups.each do |ext|
34
+ # Processed haml/slim templates have a mime-type of text/html.
35
+ # If sprockets sees a `foo.html.haml` it will process the haml
36
+ # and stop, because the haml output is html. Our html engine won't get run.
37
+ mimeless_engine = Class.new(Tilt[ext]) do
38
+ def self.default_mime_type
39
+ nil
40
+ end
41
+ end
42
+
43
+ Sprockets.register_engine ".#{ext}", mimeless_engine
44
+ end
45
+
46
+ # This engine wraps the HTML into JS
47
+ Sprockets.register_engine '.html', AngularRailsTemplates::Template
11
48
 
12
- Sprockets.register_engine SLIM_EXT , AngularRailsTemplates::Template
13
- Sprockets.register_engine HAML_EXT , AngularRailsTemplates::Template
14
- Sprockets.register_engine '.ajs' , AngularRailsTemplates::Template
15
- Sprockets.register_engine '.html' , AngularRailsTemplates::Template
49
+ app.config.assets.precompile << %r(angular-rails-templates.js)
16
50
  end
17
51
  end
18
52
  end
@@ -0,0 +1,7 @@
1
+ // Angular Rails Template
2
+ // source: <%= source_file %>
3
+
4
+ angular.module("<%= angular_module %>").run(["$templateCache", function($templateCache) {
5
+ $templateCache.put("<%= angular_template_name %>", <%= html %>)
6
+ }]);
7
+
@@ -1,51 +1,48 @@
1
- require 'sprockets'
2
- require 'sprockets/engines'
1
+ require 'angular-rails-templates/compact_javascript_escape'
3
2
 
4
3
  module AngularRailsTemplates
5
- class Template < Tilt::Template
4
+ class Template < ::Tilt::Template
5
+ include CompactJavaScriptEscape
6
+ AngularJsTemplateWrapper = Tilt::ERBTemplate.new "#{File.dirname __FILE__}/javascript_template.js.erb"
7
+ @@compressor = nil
8
+
6
9
  def self.default_mime_type
7
10
  'application/javascript'
8
11
  end
9
12
 
10
- def prepare ; end
13
+ # this method is mandatory on Tilt::Template subclasses
14
+ def prepare
15
+ if configuration.htmlcompressor
16
+ @data = compress data
17
+ end
18
+ end
11
19
 
12
20
  def evaluate(scope, locals, &block)
13
- template = case File.extname(file)
14
- when HAML_EXT then HamlTemplate.new(self)
15
- when SLIM_EXT then SlimTemplate.new(self)
16
- else
17
- BaseTemplate.new(self)
18
- end
19
-
20
- render_script_template(logical_template_path(scope), template.render)
21
+ locals[:html] = escape_javascript data.chomp
22
+ locals[:angular_template_name] = logical_template_path(scope)
23
+ locals[:source_file] = "#{scope.pathname}".sub(/^#{Rails.root}\//,'')
24
+ locals[:angular_module] = configuration.module_name
25
+
26
+ AngularJsTemplateWrapper.render(scope, locals)
21
27
  end
22
28
 
23
- protected
29
+ private
24
30
 
25
31
  def logical_template_path(scope)
26
- path = scope.logical_path
27
- path.gsub!(Regexp.new("^#{configuration.ignore_prefix}"), "")
32
+ path = scope.logical_path.sub /^#{configuration.ignore_prefix}/, ''
28
33
  "#{path}.html"
29
34
  end
30
35
 
31
- def module_name
32
- configuration.module_name.inspect
33
- end
34
-
35
36
  def configuration
36
37
  ::Rails.configuration.angular_templates
37
38
  end
38
39
 
39
- def render_script_template(path, data)
40
- %Q{
41
- window.AngularRailsTemplates || (window.AngularRailsTemplates = angular.module(#{module_name}, []));
42
-
43
- window.AngularRailsTemplates.run(["$templateCache",function($templateCache) {
44
- $templateCache.put(#{path.inspect}, #{data.to_json});
45
- }]);
46
- }
40
+ def compress html
41
+ unless @@compressor
42
+ @@compressor = HtmlCompressor::Compressor.new configuration.htmlcompressor
43
+ end
44
+ @@compressor.compress(html)
47
45
  end
48
-
49
46
  end
50
47
  end
51
48
 
@@ -1,3 +1,3 @@
1
1
  module AngularRailsTemplates
2
- VERSION = '0.0.7'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: angular-rails-templates
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damien Mathieu
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-03 00:00:00.000000000 Z
12
+ date: 2014-04-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
@@ -39,6 +39,34 @@ dependencies:
39
39
  - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: minitest
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: capybara
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
42
70
  - !ruby/object:Gem::Dependency
43
71
  name: uglifier
44
72
  requirement: !ruby/object:Gem::Requirement
@@ -55,7 +83,7 @@ dependencies:
55
83
  version: '0'
56
84
  description:
57
85
  email:
58
- - 42@dmathieu.com
86
+ - pitr.vern@gmail.com
59
87
  executables: []
60
88
  extensions: []
61
89
  extra_rdoc_files: []
@@ -63,10 +91,9 @@ files:
63
91
  - LICENSE
64
92
  - README.md
65
93
  - lib/angular-rails-templates.rb
66
- - lib/angular-rails-templates/base_template.rb
94
+ - lib/angular-rails-templates/compact_javascript_escape.rb
67
95
  - lib/angular-rails-templates/engine.rb
68
- - lib/angular-rails-templates/haml_template.rb
69
- - lib/angular-rails-templates/slim_template.rb
96
+ - lib/angular-rails-templates/javascript_template.js.erb
70
97
  - lib/angular-rails-templates/template.rb
71
98
  - lib/angular-rails-templates/version.rb
72
99
  homepage: https://github.com/pitr/angular-rails-templates
@@ -1,29 +0,0 @@
1
- require 'forwardable'
2
-
3
- module AngularRailsTemplates
4
- BaseTemplate = Struct.new(:context) do
5
- extend Forwardable
6
-
7
- def_delegators :context, :file, :data, :require_template_library
8
-
9
- def initialize_engine ; end
10
-
11
- def render
12
- initialize_engine
13
- engine.render
14
- end
15
-
16
- protected
17
-
18
- NoEngine = Struct.new(:data) do
19
- def render
20
- data
21
- end
22
- end
23
-
24
- def engine
25
- @engine ||= NoEngine.new(data)
26
- end
27
- end
28
- end
29
-
@@ -1,14 +0,0 @@
1
- module AngularRailsTemplates
2
- class HamlTemplate < BaseTemplate
3
- def initialize_engine
4
- require_template_library 'haml'
5
- end
6
-
7
- protected
8
-
9
- def engine
10
- @engine ||= Haml::Engine.new(data)
11
- end
12
- end
13
- end
14
-
@@ -1,14 +0,0 @@
1
- module AngularRailsTemplates
2
- class SlimTemplate < BaseTemplate
3
- def initialize_engine
4
- require_template_library 'slim'
5
- end
6
-
7
- protected
8
-
9
- def engine
10
- @engine ||= Slim::Template.new(file)
11
- end
12
- end
13
- end
14
-