Class: Html2erb
- Inherits:
-
Object
- Object
- Html2erb
- Defined in:
- lib/html2erb.rb,
lib/html2erb/version.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
-
#file_suffix ⇒ Object
Returns the value of attribute file_suffix.
-
#replace_map ⇒ Object
Returns the value of attribute replace_map.
-
#url_path_map ⇒ Object
Returns the value of attribute url_path_map.
Instance Method Summary collapse
- #a_tag(html) ⇒ Object
- #dom_variables(key, dom) ⇒ Object
- #erb_file_path(org_path) ⇒ Object
- #img_tag(html) ⇒ Object
-
#initialize(opt = {}) ⇒ Html2erb
constructor
A new instance of Html2erb.
- #inner_html(dom) ⇒ Object
- #replace_tags(html) ⇒ Object
- #run(file_name) ⇒ Object
- #to_content_tag(dom) ⇒ Object
Constructor Details
#initialize(opt = {}) ⇒ Html2erb
Returns a new instance of Html2erb.
9 10 11 12 13 |
# File 'lib/html2erb.rb', line 9 def initialize(opt = {}) opt.each do|k, v| self.send("#{k}=", v) end end |
Instance Attribute Details
#file_suffix ⇒ Object
Returns the value of attribute file_suffix.
7 8 9 |
# File 'lib/html2erb.rb', line 7 def file_suffix @file_suffix end |
#replace_map ⇒ Object
Returns the value of attribute replace_map.
7 8 9 |
# File 'lib/html2erb.rb', line 7 def replace_map @replace_map end |
#url_path_map ⇒ Object
Returns the value of attribute url_path_map.
7 8 9 |
# File 'lib/html2erb.rb', line 7 def url_path_map @url_path_map end |
Instance Method Details
#a_tag(html) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/html2erb.rb', line 62 def a_tag(html) html.gsub!(/<a .*?<\/a>/m) do |link| tag = Nokogiri::HTML.parse(link, nil, 'utf-8').xpath("//a")[0] href, opt = dom_variables('a', tag) # <a> タグの中身 label = inner_html tag href = '""' if href == '' case href when '"how-to.html"' then href = 'howto_path' when '"psychic.html"' then href = 'fortuneteller_index_path' when '"about.html"' then href = 'about_path' when '"index.html"' then href = 'root_path' end variables = [label.join(" + "), href, opt] variables.delete '' "<%= link_to(#{variables.join(", ").strip}) %>" end html end |
#dom_variables(key, dom) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/html2erb.rb', line 83 def dom_variables(key, dom) case key when 'img' then key = 'src' when 'a' then key = 'href' end opt = {} target = '' dom.each do|k,v| if k == key # ここは、html の <img> のパスにあわせて変更しなければいけない if key == 'src' v = v.sub('/m/img/', 'mobile/') .sub(/\Aimg\//, 'mobile/') .sub("images/site/", "pc/") end target = '"' + v + '"' else opt[k.to_sym] = v if v != '' end end [target, opt.to_pretty_s] end |
#erb_file_path(org_path) ⇒ Object
30 31 32 33 34 |
# File 'lib/html2erb.rb', line 30 def erb_file_path(org_path) file_name = File.basename(org_path) org_file_name = file_name.sub('.html', "#{@file_suffix}.html.erb") org_path.sub(file_name, org_file_name) end |
#img_tag(html) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/html2erb.rb', line 53 def img_tag(html) html.gsub!(/<img .*?\/>/m) do |link| tag = Nokogiri::HTML.parse(link, nil, 'utf-8').xpath("//img")[0] variables = dom_variables('img', tag).join(", ") "<%= image_tag(#{variables}) %>" end html end |
#inner_html(dom) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/html2erb.rb', line 108 def inner_html(dom) label = [] dom.children.each do|child| case child.name when 'img', 'span', 'text' then label << to_content_tag(child) when 'br' then label << 'raw("<br>")' else raise "undefined tag: #{child.name}" end end label.delete "" label.length == 0 ? ['""'] : label end |
#replace_tags(html) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/html2erb.rb', line 36 def (html) src_encoding = NKF.guess(html).to_s unless src_encoding == 'UTF-8' html.encode!('UTF-8', src_encoding, invalid: :replace, undef: :replace, replace: '?') html.gsub! "\r\n", "\n" end @replace_map.each do |regex, replacement| html.sub! regex, replacement end a_tag html img_tag html html end |
#run(file_name) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/html2erb.rb', line 15 def run(file_name) Dir.glob(file_name).each do |path| f = open(path, "r+") html = f.read f.close erb_file_path = erb_file_path(path) f = open(erb_file_path, "w") f.write (html) f.close (erb_file_path) end end |
#to_content_tag(dom) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/html2erb.rb', line 123 def to_content_tag(dom) if dom.name == 'text' if(dom.to_s =~ /\A\n +\z/) return '' else return '"' + dom.to_s + '"' end end if(dom.name == 'img') src, opt = dom_variables('img', dom) return "image_tag(#{src}, #{opt})" end opt = {} value = '' dom.each do|k, v| opt[k.to_sym] = v end if dom.children dom.children.each do |child| value << to_content_tag(child) end end variables = [":#{dom.name}", value, opt.to_pretty_s] "content_tag(#{variables.join(", ").strip})" end |