class Html2erb

Constants

VERSION

Attributes

file_suffix[RW]
replace_map[RW]
url_path_map[RW]

Public Class Methods

new(opt = {}) click to toggle source
# File lib/html2erb.rb, line 9
def initialize(opt = {})
  opt.each do|k, v|
    self.send("#{k}=", v)
  end
end

Public Instance Methods

a_tag(html) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/html2erb.rb, line 36
def replace_tags(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) click to toggle source
# 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 replace_tags(html)
    f.close

    replace_tags(erb_file_path)
  end
end
to_content_tag(dom) click to toggle source
# 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