Class: Metro::Ui::Convert

Inherits:
Object
  • Object
show all
Defined in:
lib/metro/ui/convert.rb

Instance Method Summary collapse

Instance Method Details

#replace_filesObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/metro/ui/convert.rb', line 12

def replace_files
  less_src_dir = File.expand_path("../../../../Metro-UI-CSS/less", __FILE__)
  less_dest_dir = File.expand_path("../../../../vendor/toolkit/metro-ui-css", __FILE__)
  rm_and_cp(less_src_dir, less_dest_dir)

  image_src_dir = File.expand_path("../../../../Metro-UI-CSS/public/images", __FILE__)
  image_dest_dir = File.expand_path("../../../../vendor/assets/images/metro-ui-css", __FILE__)
  rm_and_cp(image_src_dir, image_dest_dir)

  js_src_dir = File.expand_path("../../../../Metro-UI-CSS/javascript", __FILE__)
  js_dest_dir = File.expand_path("../../../../vendor/assets/javascripts/metro-ui-css", __FILE__)
  rm_and_cp(js_src_dir, js_dest_dir)

  font_src_dir = File.expand_path("../../../../Metro-UI-CSS/fonts", __FILE__)
  font_dest_dir = File.expand_path("../../../../vendor/assets/images/metro-ui-css/fonts", __FILE__)
  rm_and_cp(font_src_dir, font_dest_dir)

  Dir["#{less_dest_dir}/*.less"].each do |file|
    content = File.read(file)
    new_content = replace_image_url(content)
    new_content = replace_google_font_url(new_content)
    new_content = replace_local_font_url(new_content)
    File.write(file, new_content)
  end
end

#replace_google_font_url(content) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/metro/ui/convert.rb', line 54

def replace_google_font_url(content)
  content.gsub(/url\((.+?googleusercontent.+?)\)/mi) do |full_url|
    font_url = strip_quotes( $1.strip )
    font_path = File.expand_path("../../../../vendor/assets/images/metro-ui-css/fonts/#{File.basename(font_url)}", __FILE__)
    unless File.exist?(font_path)
      FileUtils.mkdir_p(File.expand_path("../../../../vendor/assets/images/metro-ui-css/fonts", __FILE__))
      system("wget #{font_url} -O #{font_path}")
    end
    font_url = "metro-ui-css/fonts/#{File.basename(font_url)}"
    "asset-url(\"#{font_url}\")"
  end
end

#replace_image_url(content) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/metro/ui/convert.rb', line 38

def replace_image_url(content)
  content.gsub(/url\((.+?images.+?)\)/mi) do |full_url|
    image_url = strip_quotes( $1.strip )
    image_url = "metro-ui-css/#{File.basename(image_url)}"
    "image-url(\"#{image_url}\")"
  end
end

#replace_local_font_url(content) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/metro/ui/convert.rb', line 46

def replace_local_font_url(content)
  content.gsub(/url\((.+?\.\.\/fonts\/.+?)\)/mi) do |full_url|
    font_url = strip_quotes( $1.strip )
    font_url = "metro-ui-css/fonts/#{File.basename(font_url)}"
    "asset-url(\"#{font_url}\")"
  end
end

#runObject



4
5
6
7
8
9
10
# File 'lib/metro/ui/convert.rb', line 4

def run
  replace_files

  license_src_file = File.expand_path("../../../../Metro-UI-CSS/LICENSE", __FILE__)
  license_dest_file = File.expand_path("../../../../Metro-UI-CSS-LICENSE", __FILE__)
  FileUtils.cp(license_src_file, license_dest_file)
end