Top Level Namespace

Defined Under Namespace

Modules: Zfben_libjs Classes: Hash, String

Instance Method Summary collapse

Instance Method Details

#css2sass(css) ⇒ Object



73
74
75
# File 'lib/zfben_libjs/lib.rb', line 73

def css2sass css
  return Sass::CSS.new(css, :cache => false).render(:sass)
end

#css_import(url, dir) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/zfben_libjs/lib.rb', line 43

def css_import url, dir
  path = File.join(dir, File.basename(url))
  download url, path
  reg = /@import\s+\(?"([^"]+)"\)?;?/
  return File.read(path).partition_all(reg).map{ |f|
    if reg =~ f
      f = reg.match(f)[1]
      f = "/* @import #{f} */\n" + css_import(File.join(File.dirname(url), f), dir)
    end
    f
  }.join("\n")
end

#download(url, path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/zfben_libjs/lib.rb', line 29

def download url, path
  if url =~ /:\/\// && (@opts[:config]['download'] == true || !File.exists?(path))
    unless system 'wget ' + url + ' -O ' + path + ' -N'
      p url + ' download fail!'
      system('rm ' + path)
      exit!
    end
  else
    if File.exists?(url) && url != path
      err 'Copy Error' unless system "cp #{url} #{path}"
    end
  end
end

#download_images(lib, url, path) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/zfben_libjs/lib.rb', line 56

def download_images lib, url, path
  reg = /url\("?'?([^'")]+)'?"?\)/
  return File.read(path).partition_all(reg).map{ |f|
    if reg =~ f
      f = reg.match(f)[1]
      sub = File.join(File.dirname(path), f)
      suburl = File.dirname(url) + '/' + f
      system('mkdir ' + File.dirname(sub)) unless File.exists?(File.dirname(sub))
      download(suburl, sub)
      f = sub
    else
      f = nil
    end
    f
  }.compact
end

#err(msg) ⇒ Object



22
23
24
25
# File 'lib/zfben_libjs.rb', line 22

def err msg
  STDERR.print "#{msg}\n".color(:red)
  exit!
end

#get_filetype(path) ⇒ Object



25
26
27
# File 'lib/zfben_libjs/lib.rb', line 25

def get_filetype path
  return File.extname(path).delete('.')
end

#minify(source, type) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/zfben_libjs/lib.rb', line 77

def minify source, type
  if source.length > 10
    min = ''
    case type
      when :js
        min = Uglifier.compile(source, :copyright => false)
      when :css
        min = Sass::Engine.new(css2sass(source), { :syntax => :sass, :style => :compressed, :cache => false }).render
    end
    if min.length > 10
      return min
    end
  end
  return source
end

#tip(msg) ⇒ Object



27
28
29
# File 'lib/zfben_libjs.rb', line 27

def tip msg
  STDOUT.print "#{msg}\n".color(:green)
end