minified_erb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a83ad067f3ceac55d0704f95f5f25084843658af
4
+ data.tar.gz: d0e4e9ae7cf112bcb71e418ac73ea9ef1b0f4377
5
+ SHA512:
6
+ metadata.gz: 818208e9ff4eef919695e48bf6c05121476e7325e8afe5e98ea1de1cbc9ab7bfac64543a60deaac30b08989aa6e2469a48f950e93b17222f7c8f9bd8a407d3e3
7
+ data.tar.gz: b3a9e11cad1ab39e48cea8aca6e6cdc14c03515e2924519105b1fb4e08b2142e6fd6b7bfdcbfdd268d6d94d3b158351dcc74e474e0d50df0418a2a3f80f7f280
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.swp
11
+ /gemfiles/*.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format Fuubar
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ AllCops:
2
+ DefaultFormatter: Fuubar
3
+ DisplayCopNames: true
4
+ TargetRubyVersion: 2.3
5
+
6
+ Style/Documentation:
7
+ Enabled: false
8
+
9
+ Style/FrozenStringLiteralComment:
10
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,28 @@
1
+ language: ruby
2
+ cache: bundler
3
+ before_install:
4
+ - gem update --system
5
+ - gem --version
6
+ - gem install bundler
7
+ - gem update bundler
8
+ bundler_args: --without tools
9
+ rvm:
10
+ - 2.3.1
11
+ - 2.2.5
12
+ - 2.0.0
13
+ - ruby-head
14
+ - jruby-head
15
+ - rbx-2
16
+ gemfile:
17
+ - gemfiles/rails_4.gemfile
18
+ - gemfiles/rails_5.gemfile
19
+ matrix:
20
+ exclude:
21
+ - rvm: 2.0.0
22
+ gemfile: gemfiles/rails_5.gemfile
23
+ - rvm: rbx-2
24
+ gemfile: gemfiles/rails_5.gemfile
25
+ allow_failures:
26
+ - rvm: ruby-head
27
+ - rvm: jruby-head
28
+ - rvm: rbx-2
data/Appraisals ADDED
@@ -0,0 +1,7 @@
1
+ appraise 'rails-4' do
2
+ gem 'actionview', '~> 4.0'
3
+ end
4
+
5
+ appraise 'rails-5' do
6
+ gem 'actionview', '>= 5.0.0.rc1'
7
+ end
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in minified_erb.gemspec
4
+ gemspec
5
+
6
+ gem 'appraisal'
7
+
8
+ group :tools do
9
+ gem 'guard'
10
+ gem 'guard-rspec'
11
+ gem 'guard-rubocop'
12
+ end
data/Guardfile ADDED
@@ -0,0 +1,12 @@
1
+ group :red_green_refactor, halt_on_fail: true do
2
+ guard :rspec, cmd: 'bundle exec rspec' do
3
+ watch('spec/spec_helper.rb') { 'spec' }
4
+ watch(%r{^spec/.+_spec\.rb$})
5
+ watch(%r{^lib/.+\.rb$}) { 'spec' }
6
+ end
7
+
8
+ guard :rubocop, all_on_start: false, notification: false do
9
+ watch(/.+\.rb$/)
10
+ watch(%r{(?:.+/)?\.rubocop.*?\.yml$}) { |m| File.dirname(m[0]) }
11
+ end
12
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Roman Usherenko
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.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # MinifiedErb
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/minified_erb`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'minified_erb'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install minified_erb
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/minified_erb.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'erubis'
5
+ require 'erbse'
6
+ require 'minified_erb'
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ # (If you use this, don't forget to add pry to your Gemfile!)
12
+ require "pry"
13
+ Pry.start
14
+
15
+ # require 'irb'
16
+ # IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,14 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "actionview", "~> 4.0"
7
+
8
+ group :tools do
9
+ gem "guard"
10
+ gem "guard-rspec"
11
+ gem "guard-rubocop"
12
+ end
13
+
14
+ gemspec :path => "../"
@@ -0,0 +1,14 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "actionview", ">= 5.0.0.rc1"
7
+
8
+ group :tools do
9
+ gem "guard"
10
+ gem "guard-rspec"
11
+ gem "guard-rubocop"
12
+ end
13
+
14
+ gemspec :path => "../"
@@ -0,0 +1,9 @@
1
+ require 'minified_erb/version'
2
+
3
+ module MinifiedErb
4
+ end
5
+
6
+ require 'minified_erb/erubis' if defined? Erubis
7
+ require 'minified_erb/action_view' if defined? ActionView
8
+ require 'minified_erb/erbse' if defined? Erbse
9
+ require 'minified_erb/cell' if defined? Cell
@@ -0,0 +1,14 @@
1
+ module MinifiedErb
2
+ module ActionView
3
+ def call(template)
4
+ self.class.erb_implementation.new(
5
+ template.source,
6
+ escape: (self.class.escape_whitelist.include? template.type),
7
+ trim: (self.class.erb_trim_mode == '-'),
8
+ minify: (template.type == 'text/html')
9
+ ).src
10
+ end
11
+ end
12
+
13
+ ::ActionView::Template::Handlers::ERB.send(:prepend, ActionView)
14
+ end
@@ -0,0 +1,9 @@
1
+ module MinifiedErb
2
+ module Cell
3
+ def template_options_for(*)
4
+ super.merge!(minify: true)
5
+ end
6
+ end
7
+
8
+ ::Cell::ViewModel.send(:prepend, Cell)
9
+ end
@@ -0,0 +1,101 @@
1
+ module MinifiedErb
2
+ module Converter
3
+ def init_minifier(properties = {})
4
+ @minify = properties[:minify]
5
+ end
6
+
7
+ def convert_input(src, input)
8
+ pat = @pattern
9
+ regexp = pat.nil? || pat == '<% %>' ? self.class.const_get('DEFAULT_REGEXP') : pattern_regexp(pat)
10
+ pos = 0
11
+ is_bol = true # is beginning of line
12
+ input.scan(regexp) do |indicator, code, tailch, rspace|
13
+ match = Regexp.last_match
14
+ len = match.begin(0) - pos
15
+ text = input[pos, len]
16
+ pos = match.end(0)
17
+ ch = indicator ? indicator[0] : nil
18
+ lspace = ch == '=' ? nil : detect_spaces_at_bol(text, is_bol)
19
+
20
+ if @minify
21
+ text.gsub!(/>\s*[\n\t]+\s*</mi, '><')
22
+ text.gsub!(/>\s*$/mi, '>')
23
+ text.gsub!(/^\s*/mi, '') if is_bol
24
+ end
25
+
26
+ is_bol = rspace ? true : false
27
+
28
+ rspace = nil if @minify
29
+
30
+ add_text(src, text) if text && !text.empty?
31
+ ## * when '<%= %>', do nothing
32
+ ## * when '<% %>' or '<%# %>', delete spaces iff only spaces are around '<% %>'
33
+ if ch == '=' # <%= %>
34
+ rspace = nil if tailch && !tailch.empty?
35
+ add_text(src, lspace) if lspace
36
+ add_expr(src, code, indicator)
37
+ add_text(src, rspace) if rspace
38
+ elsif ch == '#' # <%# %>
39
+ unless @minify
40
+ n = code.count("\n") + (rspace ? 1 : 0)
41
+ if @trim && lspace && rspace
42
+ add_stmt(src, "\n" * n)
43
+ else
44
+ add_text(src, lspace) if lspace
45
+ add_stmt(src, "\n" * n)
46
+ add_text(src, rspace) if rspace
47
+ end
48
+ end
49
+ elsif ch == '%' # <%% %>
50
+ s = "#{lspace}#{@prefix ||= '<%'}#{code}#{tailch}#{@postfix ||= '%>'}#{rspace}"
51
+ add_text(src, s)
52
+ else # <% %>
53
+ if @trim && lspace && rspace
54
+ add_stmt(src, "#{lspace}#{code}#{rspace}")
55
+ else
56
+ add_text(src, lspace) if lspace
57
+ add_stmt(src, code)
58
+ add_text(src, rspace) if rspace
59
+ end
60
+ end
61
+ end
62
+
63
+ # rest = $' || input # ruby1.8
64
+ rest = pos == 0 ? input : input[pos..-1] # ruby1.9
65
+ if @minify
66
+ rest.strip!
67
+ rest.gsub!(/>\s*[\n\t]+\s*</mi, '><')
68
+ end
69
+ add_text(src, rest)
70
+ end
71
+
72
+ protected
73
+
74
+ def detect_spaces_at_bol(text, is_bol)
75
+ lspace = nil
76
+ if text.empty?
77
+ lspace = '' if is_bol
78
+ elsif text[-1] == "\n"
79
+ lspace = ''
80
+ else
81
+ rindex = text.rindex("\n")
82
+ if rindex
83
+ s = text[rindex + 1..-1]
84
+ if s =~ /\A[ \t]*\z/
85
+ lspace = s unless @minify
86
+ # text = text[0..rindex]
87
+ text[rindex + 1..-1] = ''
88
+ end
89
+ else
90
+ if is_bol && text =~ /\A[ \t]*\z/
91
+ # lspace = text
92
+ # text = nil
93
+ lspace = text.dup unless @minify
94
+ text[0..-1] = ''
95
+ end
96
+ end
97
+ end
98
+ lspace
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,23 @@
1
+ require 'minified_erb/converter'
2
+
3
+ module MinifiedErb
4
+ module Erbse
5
+ include Converter
6
+
7
+ def init_converter!(properties = {})
8
+ super
9
+ init_minifier(properties)
10
+ end
11
+
12
+ # in Erbse add_* methods are defined on +generator+
13
+ def method_missing(name, *args, &block)
14
+ generator.respond_to?(name) ? generator.send(name, *args, &block) : super
15
+ end
16
+
17
+ def respond_to_missing?(name, include_private = false)
18
+ generator.respond_to?(name) || super
19
+ end
20
+ end
21
+
22
+ ::Erbse::Basic::Converter.send(:prepend, Erbse)
23
+ end
@@ -0,0 +1,14 @@
1
+ require 'minified_erb/converter'
2
+
3
+ module MinifiedErb
4
+ module Erubis
5
+ include Converter
6
+
7
+ def init_converter(properties = {})
8
+ super
9
+ init_minifier(properties)
10
+ end
11
+ end
12
+
13
+ ::Erubis::Eruby.send(:include, Erubis)
14
+ end
@@ -0,0 +1,3 @@
1
+ module MinifiedErb
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'minified_erb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'minified_erb'
8
+ spec.version = MinifiedErb::VERSION
9
+ spec.authors = ['Roman Usherenko']
10
+ spec.email = ['roman.usherenko@gmail.com']
11
+
12
+ spec.summary = 'Minified HTML generator for popular ERB implementations'
13
+ spec.homepage = 'https://github.com/dreyks/minified_erb'
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_development_dependency 'bundler', '~> 1.12'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
+ spec.add_development_dependency 'rubocop'
25
+ spec.add_development_dependency 'fuubar'
26
+ spec.add_development_dependency 'cells-erb'
27
+ spec.add_development_dependency 'actionview', '~> 5.0.0.rc1'
28
+ end
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minified_erb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Roman Usherenko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: fuubar
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: cells-erb
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: actionview
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 5.0.0.rc1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 5.0.0.rc1
111
+ description:
112
+ email:
113
+ - roman.usherenko@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".rubocop.yml"
121
+ - ".travis.yml"
122
+ - Appraisals
123
+ - Gemfile
124
+ - Guardfile
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - bin/console
129
+ - bin/setup
130
+ - gemfiles/rails_4.gemfile
131
+ - gemfiles/rails_5.gemfile
132
+ - lib/minified_erb.rb
133
+ - lib/minified_erb/action_view.rb
134
+ - lib/minified_erb/cell.rb
135
+ - lib/minified_erb/converter.rb
136
+ - lib/minified_erb/erbse.rb
137
+ - lib/minified_erb/erubis.rb
138
+ - lib/minified_erb/version.rb
139
+ - minified_erb.gemspec
140
+ homepage: https://github.com/dreyks/minified_erb
141
+ licenses:
142
+ - MIT
143
+ metadata: {}
144
+ post_install_message:
145
+ rdoc_options: []
146
+ require_paths:
147
+ - lib
148
+ required_ruby_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ requirements: []
159
+ rubyforge_project:
160
+ rubygems_version: 2.6.4
161
+ signing_key:
162
+ specification_version: 4
163
+ summary: Minified HTML generator for popular ERB implementations
164
+ test_files: []