html2erb 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
+ SHA1:
3
+ metadata.gz: bf8c2fdceaf089b7dcd8e63db1a08c7786e4b803
4
+ data.tar.gz: fb2b18d817899e63c1dbcded4936835f109da61d
5
+ SHA512:
6
+ metadata.gz: 1caba280ffdb70571d07b3d3433990f80f6c39280f5d4fc24cc6364b8ad013556795d0ae9904183a6c11674755050a5a2e0719612666d5edb3e0a56aa5267c47
7
+ data.tar.gz: 22a05745346b3ead45facc518670a47a649ee93a2498e8b4352c5b0afaf274ea5e13b58be733bd59c308af1603ac7416afb1cde9465ff425a4c2cb4589dff616
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in html2erb.gemspec
4
+ gemspec
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 hiroshi kobayashi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Html2erb
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'html2erb'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install html2erb
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/html2erb/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -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
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require "html2erb"
3
+
4
+
5
+ puts 'a'
@@ -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 'html2erb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "html2erb"
8
+ spec.version = Html2erb::VERSION
9
+ spec.authors = ["hiroshi kobayashi"]
10
+ spec.email = ["kobayashi@ans-web.co.jp"]
11
+ spec.summary = %q{html to eruby}
12
+ spec.description = %q{a tag to link_to, img tag to image}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "nokogiri"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.5"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "guard"
27
+ spec.add_development_dependency "guard-rspec"
28
+ end
@@ -0,0 +1,168 @@
1
+ # encoding: utf-8
2
+ require "html2erb/version"
3
+ require 'nkf'
4
+ require "nokogiri"
5
+
6
+ class Html2erb
7
+ attr_accessor :replace_map, :file_suffix, :url_path_map
8
+
9
+ def initialize(opt = {})
10
+ opt.each do|k, v|
11
+ self.send("#{k}=", v)
12
+ end
13
+ end
14
+
15
+ def run(file_name)
16
+ Dir.glob(file_name).each do |path|
17
+ f = open(path, "r+")
18
+ html = f.read
19
+ f.close
20
+
21
+ erb_file_path = erb_file_path(path)
22
+ f = open(erb_file_path, "w")
23
+ f.write replace_tags(html)
24
+ f.close
25
+
26
+ replace_tags(erb_file_path)
27
+ end
28
+ end
29
+
30
+ def erb_file_path(org_path)
31
+ file_name = File.basename(org_path)
32
+ org_file_name = file_name.sub('.html', "#{@file_suffix}.html.erb")
33
+ org_path.sub(file_name, org_file_name)
34
+ end
35
+
36
+ def replace_tags(html)
37
+ src_encoding = NKF.guess(html).to_s
38
+ unless src_encoding == 'UTF-8'
39
+ html.encode!('UTF-8', src_encoding, invalid: :replace, undef: :replace, replace: '?')
40
+ html.gsub! "\r\n", "\n"
41
+ end
42
+
43
+ @replace_map.each do |regex, replacement|
44
+ html.sub! regex, replacement
45
+ end
46
+
47
+ a_tag html
48
+ img_tag html
49
+
50
+ html
51
+ end
52
+
53
+ def img_tag(html)
54
+ html.gsub!(/<img .*?\/>/m) do |link|
55
+ tag = Nokogiri::HTML.parse(link, nil, 'utf-8').xpath("//img")[0]
56
+ variables = dom_variables('img', tag).join(", ")
57
+ "<%= image_tag(#{variables}) %>"
58
+ end
59
+ html
60
+ end
61
+
62
+ def a_tag(html)
63
+ html.gsub!(/<a .*?<\/a>/m) do |link|
64
+ tag = Nokogiri::HTML.parse(link, nil, 'utf-8').xpath("//a")[0]
65
+ href, opt = dom_variables('a', tag)
66
+
67
+ # <a> タグの中身
68
+ label = inner_html tag
69
+ href = '""' if href == ''
70
+ case href
71
+ when '"how-to.html"' then href = 'howto_path'
72
+ when '"psychic.html"' then href = 'fortuneteller_index_path'
73
+ when '"about.html"' then href = 'about_path'
74
+ when '"index.html"' then href = 'root_path'
75
+ end
76
+ variables = [label.join(" + "), href, opt]
77
+ variables.delete ''
78
+ "<%= link_to(#{variables.join(", ").strip}) %>"
79
+ end
80
+ html
81
+ end
82
+
83
+ def dom_variables(key, dom)
84
+ case key
85
+ when 'img' then key = 'src'
86
+ when 'a' then key = 'href'
87
+ end
88
+
89
+ opt = {}
90
+ target = ''
91
+ dom.each do|k,v|
92
+ if k == key
93
+ # ここは、html の <img> のパスにあわせて変更しなければいけない
94
+ if key == 'src'
95
+ v = v.sub('/m/img/', 'mobile/')
96
+ .sub(/\Aimg\//, 'mobile/')
97
+ .sub("images/site/", "pc/")
98
+ end
99
+
100
+ target = '"' + v + '"'
101
+ else
102
+ opt[k.to_sym] = v if v != ''
103
+ end
104
+ end
105
+ [target, opt.to_pretty_s]
106
+ end
107
+
108
+ def inner_html(dom)
109
+ label = []
110
+ dom.children.each do|child|
111
+ case child.name
112
+ when 'img', 'span', 'text' then label << to_content_tag(child)
113
+ when 'br' then label << 'raw("<br>")'
114
+ else
115
+ raise "undefined tag: #{child.name}"
116
+ end
117
+ end
118
+
119
+ label.delete ""
120
+ label.length == 0 ? ['""'] : label
121
+ end
122
+
123
+ def to_content_tag(dom)
124
+ if dom.name == 'text'
125
+ if(dom.to_s =~ /\A\n +\z/)
126
+ return ''
127
+ else
128
+ return '"' + dom.to_s + '"'
129
+ end
130
+ end
131
+
132
+ if(dom.name == 'img')
133
+ src, opt = dom_variables('img', dom)
134
+ return "image_tag(#{src}, #{opt})"
135
+ end
136
+
137
+ opt = {}
138
+ value = ''
139
+ dom.each do|k, v|
140
+ opt[k.to_sym] = v
141
+ end
142
+
143
+ if dom.children
144
+ dom.children.each do |child|
145
+ value << to_content_tag(child)
146
+ end
147
+ end
148
+
149
+ variables = [":#{dom.name}", value, opt.to_pretty_s]
150
+ "content_tag(#{variables.join(", ").strip})"
151
+ end
152
+ end
153
+
154
+ class Hash
155
+ # ハッシュを [ hoge: 'fuga' ] 形式で表現した文字列を返す
156
+ def to_pretty_s
157
+ buf = []
158
+ self.each{|k,v|
159
+ buf << "#{k}: '#{v}'" if v != ''
160
+ }
161
+ buf.join(", ")
162
+ # self.to_s.gsub(':', '') # : を取り除く
163
+ # .gsub('=>', ': ') # => を : に
164
+ # .gsub(/\A\{/, '') # 先頭のブラケット
165
+ # .gsub(/\}\z/, '') # 末尾のブラケット
166
+ # .strip.gsub(/,\z/, '') # 末尾のカンマ
167
+ end
168
+ end
@@ -0,0 +1,3 @@
1
+ class Html2erb
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe Html2erb do
4
+ it 'should have a version number' do
5
+ Html2erb::VERSION.should_not be_nil
6
+ end
7
+
8
+ describe 'create erb file' do
9
+ before do
10
+ @html = 'spec/test.html'
11
+ @erb = 'spec/test.html.erb'
12
+ File.delete @erb if File.exists? @erb
13
+
14
+ html_to_erb = Html2erb.new(
15
+ replace_map: [
16
+ [/<html.*id="main">/m, '<% content_for :title do %><% end %>'],
17
+ [/<\/div>\n <section class="footer">.*/m, '']
18
+ ]
19
+ )
20
+ html_to_erb.run(@html)
21
+ end
22
+
23
+ it 'erb file exists' do
24
+ File.exists?(@erb).should be_true
25
+ end
26
+
27
+ it 'remove header and footer' do
28
+ open(@erb, "r+") do |f|
29
+ html = f.read
30
+ html.include?('header').should be_false
31
+ html.include?('footer').should be_false
32
+ end
33
+ end
34
+
35
+ it 'replace a-tag to link_to' do
36
+ open(@erb, "r+") do |f|
37
+ html = f.read
38
+ html.include?('link_to').should be_true
39
+ html.include?(
40
+ %{<%= link_to(image_tag("google.jpg", title: 'foo') + content_tag(:span, "span", class: 'span-class'), "http://google.com", class: 'foo', alt: 'グーグル') %>}
41
+ ).should be_true
42
+ end
43
+ end
44
+
45
+ it 'replace image-tag to image' do
46
+ open(@erb, "r+") do |f|
47
+ html = f.read
48
+ html.include?(%{<%= image_tag("foo.jpg", title: 'foo') %>}).should be_true
49
+ end
50
+ end
51
+
52
+
53
+ after do
54
+ File.delete @erb if File.exists? @erb
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'html2erb'
@@ -0,0 +1,19 @@
1
+ <html>
2
+ <head>
3
+ <title>test</title>
4
+ </head>
5
+ <body>
6
+ <section class="header">header</section>
7
+ <div id="main">
8
+ <a href="http://google.com" class="foo" alt="グーグル">google</a>
9
+ <img src="foo.jpg" title="foo" />
10
+ <section class="link-include-image">
11
+ <a href="http://google.com" class="foo" alt="グーグル">
12
+ <img src="google.jpg" title="foo" />
13
+ <span class="span-class">span</span>
14
+ </a>
15
+ </section>
16
+ </div>
17
+ <section class="footer">footer</section>
18
+ </body>
19
+ </html>
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html2erb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - hiroshi kobayashi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
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: guard
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: guard-rspec
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
+ description: a tag to link_to, img tag to image
98
+ email:
99
+ - kobayashi@ans-web.co.jp
100
+ executables:
101
+ - html2erb
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - .gitignore
106
+ - .rspec
107
+ - .travis.yml
108
+ - Gemfile
109
+ - Guardfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/html2erb
114
+ - html2erb.gemspec
115
+ - lib/html2erb.rb
116
+ - lib/html2erb/version.rb
117
+ - spec/html2erb_spec.rb
118
+ - spec/spec_helper.rb
119
+ - spec/test.html
120
+ homepage: ''
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.1.10
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: html to eruby
144
+ test_files:
145
+ - spec/html2erb_spec.rb
146
+ - spec/spec_helper.rb
147
+ - spec/test.html