Class: Template2rails::Parser
- Inherits:
-
Object
- Object
- Template2rails::Parser
- Defined in:
- lib/template2rails.rb
Instance Attribute Summary collapse
-
#_csses ⇒ Object
Returns the value of attribute _csses.
-
#_images ⇒ Object
Returns the value of attribute _images.
-
#_jses ⇒ Object
Returns the value of attribute _jses.
-
#asset_root ⇒ Object
Returns the value of attribute asset_root.
-
#source_root ⇒ Object
Returns the value of attribute source_root.
Instance Method Summary collapse
- #asset_file(file) ⇒ Object
- #asset_path(with_file = true) ⇒ Object
- #copy_assets(asset_file) ⇒ Object
-
#initialize(path) ⇒ Parser
constructor
A new instance of Parser.
- #prep_dirs ⇒ Object
- #to_rails ⇒ Object
- #update_asset(asset, relativeTo) ⇒ Object
Constructor Details
#initialize(path) ⇒ Parser
Returns a new instance of Parser.
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/template2rails.rb', line 30 def initialize(path) path = File.(path) File.exists?(path) or raise Exception.new("File #{path} not found.") @source_root = File.dirname(path) @asset_root = Rails.root.join('vendor','assets') f = File.open(path) @document = Nokogiri::HTML(f) @_csses = @document.css('link[rel=stylesheet]').dup @_jses = @document.css('script[src]').dup @_images = @document.css('img[src]').dup f.close end |
Instance Attribute Details
#_csses ⇒ Object
Returns the value of attribute _csses.
25 26 27 |
# File 'lib/template2rails.rb', line 25 def _csses @_csses end |
#_images ⇒ Object
Returns the value of attribute _images.
25 26 27 |
# File 'lib/template2rails.rb', line 25 def _images @_images end |
#_jses ⇒ Object
Returns the value of attribute _jses.
25 26 27 |
# File 'lib/template2rails.rb', line 25 def _jses @_jses end |
#asset_root ⇒ Object
Returns the value of attribute asset_root.
23 24 25 |
# File 'lib/template2rails.rb', line 23 def asset_root @asset_root end |
#source_root ⇒ Object
Returns the value of attribute source_root.
24 25 26 |
# File 'lib/template2rails.rb', line 24 def source_root @source_root end |
Instance Method Details
#asset_file(file) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/template2rails.rb', line 102 def asset_file(file) case File.extname(file) when '.css'; prefix = 'stylesheets' when '.js'; prefix = 'javascripts' when '.ttf', '.eot', '.woff', '.otf'; prefix = 'fonts' when '.jpg', '.jpeg', '.png', '.gif', '.bmp'; prefix = 'images' else; prefix = 'miscellaneous' end file = file.gsub(/(\.{0,2}\/)*((assets|resources|fonts)\/)*([^\n]+)/, '\4') end |
#asset_path(with_file = true) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/template2rails.rb', line 140 def asset_path(with_file = true) paths = [] @_csses.each do |css| dir = with_file ? css['href'] : File.dirname(css['href']) paths.push(dir) unless paths.include?(dir) end @_jses.each do |js| dir = with_file ? js['src'] : File.dirname(js['src']) paths.push(dir) unless paths.include?(dir) end @_images.each do |img| dir = with_file ? img['src'] : File.dirname(img['src']) paths.push(dir) unless paths.include?(dir) end paths end |
#copy_assets(asset_file) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/template2rails.rb', line 119 def copy_assets(asset_file) asset_file = asset_file.split(/[\?\#]/)[0] # skip net assets if asset_file =~ /(https?\:)?\/\/[^\n]+/ return end realpath = File.("#{@source_root}/#{asset_file}") raise Exception.new "Error while parsing #{asset_file}:\n\tFile #{realpath} not found."\ unless File.exists?(realpath) realdir = File.dirname(realpath) require 'fileutils' tgt = "#{@asset_root}/#{asset_file(asset_file)}" FileUtils.cp(realpath, tgt) if File.exists?(realpath) update_asset(tgt, File.dirname(asset_file)) end |
#prep_dirs ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/template2rails.rb', line 43 def prep_dirs require 'fileutils' FileUtils::rm_rf(@asset_root) asset_path(false).each do |path| # skip net assets if path =~ /(https?\:)?\/\/[^\n]+/ next end FileUtils::mkdir_p "#{@asset_root}/#{asset_file(path)}" end end |
#to_rails ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/template2rails.rb', line 55 def to_rails begin # check for head and body tag @document.at_xpath("/html/head") or raise Exception.new("Will not convert HTML without HEAD") @document.at_xpath("/html/body") or raise Exception.new("Will not convert HTML without BODY") # prepare required directories prep_dirs # start copying assets asset_path(true).each do |path| copy_assets(path) end # convert html to rails transform('css') transform('js') transform('images') transform('content', {:menu => ENV['MENU'], :content => ENV['CONTENT']}) apply rescue Exception => error puts "#{error.backtrace[0]}:\n\t#{error.}" end end |
#update_asset(asset, relativeTo) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/template2rails.rb', line 80 def update_asset(asset, relativeTo) asset = asset.split(/[\?\#]/)[0] ext = File.extname(asset) return unless ext =~ /\.(css)/ require 'find' text = File.read(asset) unless text.match(REGEXES[ext][:rgx]).nil? tgt = nil new_contents = text.gsub(REGEXES[ext][:rgx]) do |link| in_url = $1 file = in_url.split(/[\?\#]/)[0] source_file = File.("#{@source_root}/#{relativeTo}/#{file}") dest_file = File.("#{@asset_root}/#{asset_file(relativeTo)}/#{file}") dest_dir = File.dirname(dest_file) File.directory?(dest_dir) or FileUtils.mkdir_p(dest_dir) FileUtils.cp(source_file, dest_file) if File.exists?(source_file) "url(#{dest_file.sub(/.+assets\/[^\/]+\/([^\n]+)/, '/assets/\1')})" end File.open(asset, 'wb') do |file| file.puts new_contents end end end |