startback 0.4.0 → 0.4.1
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.
- checksums.yaml +4 -4
- data/lib/startback/version.rb +1 -1
- data/lib/startback/web/magic_assets.rb +5 -1
- data/lib/startback/web/magic_assets/ng_html_transformer.rb +80 -0
- data/spec/unit/web/fixtures/assets/app/hello.html +1 -0
- data/spec/unit/web/fixtures/assets/index.es6 +1 -1
- data/spec/unit/web/test_magic_assets.rb +22 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a06b0b31cc8840a4dd20381feb44c16a2f7a83dd
|
4
|
+
data.tar.gz: 9cc508e4ac8f662f4bb1a8f60fba6f7fe8b36c82
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4936d6d7fc528704900c7178c95c9d9b1ab20befd46080ac1545f49fb56054deb8b3befd68afb426dc751ba3d4c065adf432c94b2b6c9e12a0690da1c5ac98a1
|
7
|
+
data.tar.gz: d652e019cf8c22043bf8688252b89578f71fa73fe0265307105e78dd69473beb9ba36ae79f4e6cc626847aeab8f5616bd881630d957aa32dc0155dbe62114330
|
data/lib/startback/version.rb
CHANGED
@@ -34,7 +34,8 @@ module Startback
|
|
34
34
|
class MagicAssets
|
35
35
|
|
36
36
|
DEFAULT_OPTIONS = {
|
37
|
-
sprockets: {}
|
37
|
+
sprockets: {},
|
38
|
+
plugins: {}
|
38
39
|
}
|
39
40
|
|
40
41
|
def initialize(app, options = {})
|
@@ -85,6 +86,9 @@ module Startback
|
|
85
86
|
@options[:sprockets].each_pair do |k,v|
|
86
87
|
s.public_send(:"#{k}=", v)
|
87
88
|
end
|
89
|
+
@options[:plugins].each do |p|
|
90
|
+
p.install(s)
|
91
|
+
end
|
88
92
|
}
|
89
93
|
end
|
90
94
|
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module Startback
|
2
|
+
module Web
|
3
|
+
class MagicAssets
|
4
|
+
#
|
5
|
+
# Plugin for MagicAssets that compiles .html angular templates in the
|
6
|
+
# assets structure to javascript files filling angular's template cache.
|
7
|
+
#
|
8
|
+
# Heavily inspired, yet over-simplified version, of angular-rails-templates
|
9
|
+
# See https://github.com/pitr/angular-rails-templates, licensed under MIT
|
10
|
+
#
|
11
|
+
# Example:
|
12
|
+
#
|
13
|
+
# use Startback::Web::MagicAssets, {
|
14
|
+
# plugins: [Startback::Web::MagicAssets::NgHtmlTransfomer.new]
|
15
|
+
# }
|
16
|
+
#
|
17
|
+
class NgHtmlTransformer
|
18
|
+
|
19
|
+
DEFAULT_OPTIONS = {
|
20
|
+
:path => '/assets',
|
21
|
+
:ng_module => 'templates',
|
22
|
+
:mime_type => 'text/ng-html',
|
23
|
+
:extensions => [".html"]
|
24
|
+
}
|
25
|
+
|
26
|
+
def initialize(options = {})
|
27
|
+
@options = DEFAULT_OPTIONS.merge(options)
|
28
|
+
end
|
29
|
+
attr_reader :options
|
30
|
+
|
31
|
+
def install(sprockets)
|
32
|
+
sprockets.register_mime_type options[:mime_type], extensions: options[:extensions]
|
33
|
+
sprockets.register_transformer options[:mime_type], 'application/javascript', self
|
34
|
+
end
|
35
|
+
|
36
|
+
TPL = <<-EOF
|
37
|
+
angular.module("<%= ng_module %>").run(["$templateCache", function($templateCache) {
|
38
|
+
$templateCache.put("<%= angular_template_name %>", <%= html %>)
|
39
|
+
}]);
|
40
|
+
EOF
|
41
|
+
|
42
|
+
# inspired by Rails' action_view/helpers/javascript_helper.rb
|
43
|
+
JS_ESCAPE_MAP = {
|
44
|
+
'\\' => '\\\\',
|
45
|
+
"\r\n" => '\n',
|
46
|
+
"\n" => '\n',
|
47
|
+
"\r" => '\n',
|
48
|
+
'"' => '\\"',
|
49
|
+
"'" => "\\'"
|
50
|
+
}
|
51
|
+
|
52
|
+
# We want to deliver the shortist valid javascript escaped string
|
53
|
+
# Count the number of " vs '
|
54
|
+
# If more ', escape "
|
55
|
+
# If more ", escape '
|
56
|
+
# If equal, prefer to escape "
|
57
|
+
|
58
|
+
def escape_javascript(raw)
|
59
|
+
if raw
|
60
|
+
quote = raw.count(%{'}) >= raw.count(%{"}) ? %{"} : %{'}
|
61
|
+
escaped = raw.gsub(/(\\|\r\n|[\n\r#{quote}])/u) {|match| JS_ESCAPE_MAP[match] }
|
62
|
+
"#{quote}#{escaped}#{quote}"
|
63
|
+
else
|
64
|
+
'""'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def call(input)
|
69
|
+
file_path = input[:filename]
|
70
|
+
angular_template_name = "#{options[:path]}/#{input[:name]}.html"
|
71
|
+
source_file = file_path
|
72
|
+
ng_module = options[:ng_module]
|
73
|
+
html = escape_javascript(input[:data].chomp)
|
74
|
+
ERB.new(TPL).result(binding)
|
75
|
+
end
|
76
|
+
|
77
|
+
end # class NgHtmlTransformer
|
78
|
+
end # class MagicAssets
|
79
|
+
end # module Web
|
80
|
+
end # module Startback
|
@@ -0,0 +1 @@
|
|
1
|
+
<p>Hello {{who}}</p>
|
@@ -1 +1 @@
|
|
1
|
-
//=
|
1
|
+
//= require_tree ./app
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'startback/web/magic_assets'
|
3
|
+
require 'startback/web/magic_assets/ng_html_transformer'
|
3
4
|
|
4
5
|
module Startback
|
5
6
|
module Web
|
@@ -21,7 +22,7 @@ module Startback
|
|
21
22
|
|
22
23
|
it 'delegates a [] call to sprockets' do
|
23
24
|
result = app['index.js']
|
24
|
-
expect(result.to_s).to match(/function test/)
|
25
|
+
expect(result.to_s).to match(/function test/)
|
25
26
|
end
|
26
27
|
|
27
28
|
it 'returns a 404 on unknown' do
|
@@ -56,6 +57,26 @@ module Startback
|
|
56
57
|
end
|
57
58
|
end
|
58
59
|
|
60
|
+
context 'when registering the NgHtmlTransformer' do
|
61
|
+
let(:app){
|
62
|
+
plugin = MagicAssets::NgHtmlTransformer.new({
|
63
|
+
ng_module: "test.templates"
|
64
|
+
})
|
65
|
+
MagicAssets.new({
|
66
|
+
folder: Path.dir/"fixtures/assets",
|
67
|
+
plugins: [plugin]
|
68
|
+
})
|
69
|
+
}
|
70
|
+
|
71
|
+
it 'works as expected' do
|
72
|
+
get "/index.js"
|
73
|
+
expect(last_response.status).to eql(200)
|
74
|
+
expect(last_response.body).to match(/function test/)
|
75
|
+
expect(last_response.body).to match(/Hello \{\{who\}\}/)
|
76
|
+
expect(last_response.body).to match(/angular.module\("test.templates"\)/)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
59
80
|
end
|
60
81
|
end
|
61
82
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: startback
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bernard Lambeau
|
@@ -164,6 +164,7 @@ files:
|
|
164
164
|
- lib/startback/web/cors_headers.rb
|
165
165
|
- lib/startback/web/health_check.rb
|
166
166
|
- lib/startback/web/magic_assets.rb
|
167
|
+
- lib/startback/web/magic_assets/ng_html_transformer.rb
|
167
168
|
- lib/startback/web/magic_assets/rake_tasks.rb
|
168
169
|
- lib/startback/web/shield.rb
|
169
170
|
- spec/spec_helper.rb
|
@@ -171,6 +172,7 @@ files:
|
|
171
172
|
- spec/unit/test_operation.rb
|
172
173
|
- spec/unit/test_support.rb
|
173
174
|
- spec/unit/web/fixtures/assets/app/hello.es6
|
175
|
+
- spec/unit/web/fixtures/assets/app/hello.html
|
174
176
|
- spec/unit/web/fixtures/assets/index.es6
|
175
177
|
- spec/unit/web/test_auto_caching.rb
|
176
178
|
- spec/unit/web/test_catch_all.rb
|