opal-erubi 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 28b2ec7bf06d7c568035145132abedf54f6f76544fbeb73f9b7316bf5f158fba
4
+ data.tar.gz: db14ec171666f0d1c7b27b5c078b29f385b862dbc1f1bb81cdb88d113d9f64d1
5
+ SHA512:
6
+ metadata.gz: a5d0e3dc8d048df7554e58fb248b1e384843011d7bc63ee9514fa96b148c6dca1152f6d2bafbd4ba426cb22436d02abe6cac3ce4679a9dcd95577c0e20b68f96
7
+ data.tar.gz: bedbde578f02ede63013a17b0357870b11db4da00596807d7108d89febe87701813de7f53e1940010f3001800ec9b4397054a23783c086d1fbc189405adc12d1
@@ -0,0 +1,3 @@
1
+ Gemfile.lock
2
+ *.gem
3
+ *~
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+
2
+ source 'https://rubygems.org'
3
+ gemspec
4
+
5
+ gem "opal-rspec", "> 0.6.2"
6
+ gem "opal"
7
+ gem "erubi"
8
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (C) 2019-2020 by hmdne
4
+ opal-haml: Copyright (C) 2013 by Adam Beynon, 2015 Elia Schito
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
@@ -0,0 +1,116 @@
1
+ # Opal Erubi
2
+
3
+ *Erubi templates for Opal*
4
+
5
+ This gem adds `.erubi` files to precompile templates for opal. This gem does
6
+ not make the Erubi compiler available under opal, just the result of
7
+ pre-compiling an Erubi template.
8
+
9
+ A micro runtime is included to handle escaping.
10
+
11
+ All Erubi templates become available under the `Template` constant in Opal,
12
+ which allows a template to be easily rendered in a context:
13
+
14
+ ```ruby
15
+ Template['user'].render(my_user)
16
+ # => <div>..</div>
17
+ ```
18
+
19
+ This code is related to an opal-haml gem. It's mostly compatible, but it doesn't
20
+ include sprockets support by default. Since Erubi is most likely to be used in a
21
+ Roda stack which uses just Tilt to render assets this shouldn't be of any issue.
22
+ Including sprockets support is as simple as doing `include "opal/erubi/processor"`.
23
+
24
+ This code is opinionated - it assumes that you want to run it in an "escape: true"
25
+ mode.
26
+
27
+ ## Usage
28
+
29
+ Add opal-erubi to your `Gemfile`:
30
+
31
+ ```ruby
32
+ gem 'opal-erubi'
33
+ ```
34
+
35
+ Create an erubi file in the opal load path (e.g. `app/user_template.erubi`):
36
+
37
+ ```erb
38
+ <div class="row">
39
+ <div class="col-md-6">
40
+ <%= self.name %>
41
+ </div>
42
+ <div class="col-md-6">
43
+ <%= self.age %>
44
+ </div>
45
+ </div>
46
+ ```
47
+
48
+ Render the erubi template:
49
+
50
+ ```ruby
51
+ # app/application.rb
52
+ require 'user_template'
53
+
54
+ class User
55
+ attr_accessor :name, :age
56
+
57
+ def initialize(name, age)
58
+ @name, @age = name, age
59
+ end
60
+ end
61
+
62
+ user = User.new('Ford Prefect', 42)
63
+ html = Template['user_template'].render(user)
64
+ puts html
65
+ # => <div class="row">...</div>
66
+ ```
67
+
68
+ ## Running tests
69
+
70
+ Get dependencies:
71
+
72
+ ```
73
+ $ bundle install
74
+ ```
75
+
76
+ Run tests using opal-rspec:
77
+
78
+ ```
79
+ $ bundle exec rake
80
+ ```
81
+
82
+ Run specs in the browser:
83
+
84
+ ```
85
+ $ bundle exec rackup
86
+ ```
87
+
88
+ ## License
89
+
90
+ This gem contains adapted parts from opal-haml.
91
+
92
+ (The MIT License)
93
+
94
+ Copyright (C) 2019 by ahmadine
95
+
96
+ opal-haml: Copyright (C) 2013 by Adam Beynon, 2015 Elia Schito
97
+
98
+ Permission is hereby granted, free of charge, to any person obtaining a copy
99
+ of this software and associated documentation files (the "Software"), to deal
100
+ in the Software without restriction, including without limitation the rights
101
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
102
+ copies of the Software, and to permit persons to whom the Software is
103
+ furnished to do so, subject to the following conditions:
104
+
105
+ The above copyright notice and this permission notice shall be included in
106
+ all copies or substantial portions of the Software.
107
+
108
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
109
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
110
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
111
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
112
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
113
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
114
+ THE SOFTWARE.
115
+
116
+
@@ -0,0 +1,10 @@
1
+ require 'bundler'
2
+ Bundler.require
3
+ Bundler::GemHelper.install_tasks
4
+ require 'opal/erubi/processor' # Sprockets support
5
+
6
+ require 'opal/rspec/rake_task'
7
+ Opal::RSpec::RakeTask.new(:default) do |_, task|
8
+ task.default_path = 'spec-opal'
9
+ task.pattern = 'spec-opal/**/*_spec.{rb,opal}'
10
+ end
@@ -0,0 +1,19 @@
1
+
2
+ require 'bundler'
3
+ Bundler.require
4
+
5
+ require 'opal-sprockets'
6
+ require 'opal-rspec'
7
+ require 'opal/erubi/processor' # Sprockets support
8
+
9
+ sprockets_env = Opal::RSpec::SprocketsEnvironment.new(
10
+ spec_pattern = 'spec-opal/**/*_spec.{rb,opal}',
11
+ spec_exclude_pattern = nil,
12
+ spec_files = nil,
13
+ default_path = 'spec-opal')
14
+
15
+ run Opal::Sprockets::Server.new(sprockets: sprockets_env) { |s|
16
+ s.main = 'opal/rspec/sprockets_runner'
17
+ s.append_path 'spec-opal'
18
+ s.debug = true
19
+ }
@@ -0,0 +1,15 @@
1
+ require 'template'
2
+
3
+ module Erubi
4
+ ESCAPE_TABLE={"&"=>"&amp;", "<"=>"&lt;", ">"=>"&gt;", "\""=>"&quot;", "'"=>"&#39;"}
5
+
6
+ def self.h value
7
+ value.to_s.gsub(/[&<>"']/, ESCAPE_TABLE)
8
+ end
9
+ end
10
+
11
+ class Template
12
+ class OutputBuffer
13
+ alias << append
14
+ end
15
+ end
@@ -0,0 +1 @@
1
+ require 'opal/erubi'
@@ -0,0 +1,21 @@
1
+ require 'erubi'
2
+
3
+ require 'opal/erubi/version'
4
+ require 'opal/erubi/builder'
5
+
6
+ #require 'opal/erubi/processor' <<< sprockets support, disabled by default
7
+
8
+ Opal.append_path File.expand_path('../../../lib-opal', __FILE__).untaint
9
+
10
+ module Opal
11
+ module Erubi
12
+ def self.compile_erubi(source, path)
13
+ ::Erubi::Engine.new(source.to_s,
14
+ bufval: "__buf",
15
+ postamble: "_buf.join }",
16
+ escape: true,
17
+ src: "require 'erubi';Template.new('#{path}') { |__buf| ",
18
+ filename: path).src.dup
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,28 @@
1
+ require 'opal'
2
+
3
+ module Opal
4
+ module BuilderProcessors
5
+ class ErubiProcessor < RubyProcessor
6
+ handles :erubi
7
+
8
+ def initialize(*args)
9
+ super
10
+ @source = prepare(@source, @filename)
11
+ end
12
+
13
+ def requires
14
+ ['erubi'] + super
15
+ end
16
+
17
+ private
18
+
19
+ def prepare(source, path)
20
+ ::Opal::Erubi::compile_erubi(source, path)
21
+ end
22
+ end
23
+ end
24
+
25
+ # You could do this if you wanted to reclaim "erb" extension but I'm afraid
26
+ # it may cause some problems. ERB is used for preprocessing (Opal) Ruby code.
27
+ #Builder.processors.delete(BuilderProcessors::ERBProcessor)
28
+ end
@@ -0,0 +1,29 @@
1
+ # This file is for sprockets support.
2
+
3
+ require 'opal-sprockets'
4
+
5
+ module Opal
6
+ module Erubi
7
+ class Processor < ::Opal::Processor
8
+ self.default_mime_type = 'application/javascript'
9
+
10
+ def evaluate(context, locals, &block)
11
+ @data = Opal::Erubi.compile_erubi @data, context.logical_path.sub(/^templates\//, '')
12
+ super
13
+ end
14
+
15
+ def self.compiler_options
16
+ # otherwise would check current class `attr_accessor`s
17
+ ::Opal::Processor.compiler_options
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ if Sprockets.respond_to? :register_transformer
24
+ extra_args = [{mime_type: 'application/javascript', silence_deprecation: true}]
25
+ else
26
+ extra_args = []
27
+ end
28
+
29
+ Sprockets.register_engine '.erubi', Opal::Erubi::Processor, *extra_args
@@ -0,0 +1,5 @@
1
+ module Opal
2
+ module Erubi
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ $LOAD_PATH << File.expand_path('../lib', __FILE__)
2
+ require 'opal/erubi/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'opal-erubi'
6
+ s.version = Opal::Erubi::VERSION
7
+ s.author = 'hmdne'
8
+ s.email = '54514036+hmdne@users.noreply.github.com'
9
+ s.homepage = 'https://github.com/hmdne/opal-erubi'
10
+ s.summary = 'Run Erubi templates client-side with Opal'
11
+ s.description = 'Run Erubi templates client-side with Opal.'
12
+ s.license = 'MIT'
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.require_paths = ['lib']
18
+
19
+ s.add_dependency 'opal', ['>= 0.11', '< 2']
20
+ s.add_dependency 'erubi'
21
+
22
+ s.add_development_dependency 'opal-rspec'
23
+ s.add_development_dependency 'opal-sprockets' #for RSpec
24
+ s.add_development_dependency 'rake'
25
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+ require 'fixtures/simple'
3
+ require 'fixtures/advanced'
4
+ require 'fixtures/html_content'
5
+ #require 'fixtures/yielder'
6
+
7
+ describe "Erubi files" do
8
+ let(:simple) { Template['fixtures/simple'] }
9
+ let(:advanced) { Template['fixtures/advanced'] }
10
+ let(:html_content) { Template['fixtures/html_content'] }
11
+ #let(:yielder) { Template['fixtures/yielder'] }
12
+
13
+ it "should be defined by filename on Template namespace" do
14
+ expect(simple).to be_kind_of(Template)
15
+ end
16
+
17
+ it "should render using #render" do
18
+ expect(simple.render(self).chomp).to eq('lol')
19
+ end
20
+
21
+ it "accepts a context to render template with" do
22
+ @erubi_message = "hello world"
23
+ expect(advanced.render(self).chomp).to include('hello world')
24
+ expect(advanced.render(self).chomp).to eq(<<~EOF.chomp)
25
+ hello world
26
+ <a foo="123" bar="baz" quz="1134" href="#">
27
+ foofoofoo
28
+ </a>
29
+ EOF
30
+ end
31
+
32
+ it "generates html with a given context" do
33
+ @h1_content = 'Ford Perfect'
34
+ expect(html_content.render(self).chomp).to eq('<h1>Ford Perfect</h1>')
35
+ end
36
+
37
+ # There are issues to be solved before such a syntax can be supported
38
+ xit "yields a block" do
39
+ out = yielder.render self do
40
+ "<strong>Hello world!</strong>"
41
+ end
42
+ expect(out.chomp).to eq("<div>\n <strong>Hello world!</strong>\n</div>")
43
+ end
44
+ end
@@ -0,0 +1,4 @@
1
+ <%= @erubi_message %>
2
+ <a foo="123" bar="baz" quz="<%= 456+678 %>" href="#">
3
+ <%= 'foo' * 3 %>
4
+ </a>
@@ -0,0 +1 @@
1
+ <h1><%= @h1_content %></h1>
@@ -0,0 +1 @@
1
+ lol
@@ -0,0 +1,3 @@
1
+ <div>
2
+ <%== yield %>
3
+ </div>
@@ -0,0 +1,6 @@
1
+ require 'opal'
2
+ require 'erubi'
3
+
4
+ RSpec.configure do |c|
5
+ c.color = true
6
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opal-erubi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - hmdne
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: opal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0.11'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '0.11'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2'
33
+ - !ruby/object:Gem::Dependency
34
+ name: erubi
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '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'
47
+ - !ruby/object:Gem::Dependency
48
+ name: opal-rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: opal-sprockets
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
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: Run Erubi templates client-side with Opal.
90
+ email: 54514036+hmdne@users.noreply.github.com
91
+ executables: []
92
+ extensions: []
93
+ extra_rdoc_files: []
94
+ files:
95
+ - ".gitignore"
96
+ - Gemfile
97
+ - LICENSE
98
+ - README.md
99
+ - Rakefile
100
+ - config.ru
101
+ - lib-opal/erubi.rb
102
+ - lib/opal-erubi.rb
103
+ - lib/opal/erubi.rb
104
+ - lib/opal/erubi/builder.rb
105
+ - lib/opal/erubi/processor.rb
106
+ - lib/opal/erubi/version.rb
107
+ - opal-erubi.gemspec
108
+ - spec-opal/erubi_spec.rb
109
+ - spec-opal/fixtures/advanced.erubi
110
+ - spec-opal/fixtures/html_content.erubi
111
+ - spec-opal/fixtures/simple.erubi
112
+ - spec-opal/fixtures/yielder.erubi
113
+ - spec-opal/spec_helper.rb
114
+ homepage: https://github.com/hmdne/opal-erubi
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubygems_version: 3.1.2
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Run Erubi templates client-side with Opal
137
+ test_files: []