Module: Spider::ContentUtils

Defined in:
lib/spiderfw/content_utils.rb

Class Method Summary collapse

Class Method Details

.combine_js(path, combined_file) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/spiderfw/content_utils.rb', line 63

def self.combine_js(path, combined_file)
    combined = open(combined_file, 'w')
    Find.find(path) do |path|
        next if File.directory?(path)
        next if File.basename(path)[0].chr == '.'
        if File.extname(path) == '.js'
            p "ADDING #{path}"
            js = IO.read(path)
            combined.write(js+"\n")                    
        end
    end
    combined.close
end

.compress(*apps) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/spiderfw/content_utils.rb', line 20

def self.compress(*apps)
    require 'yui/compressor'
    apps = Spider.apps.keys if apps.empty?
    apps.each do |app_name|
        app = Spider.apps[app_name]
        raise "Can't find app #{app_name}" unless app
        next unless File.directory?(app.pub_path)
        pub_path = nil
        if Spider.conf.get('static_content.mode') == 'publish'
            pub_path = Spider::HomeController.app_pub_path(app)
        else
            pub_path = app.pub_path
        end
        tmp_combined = Spider.paths[:tmp]+'/_combined.js'
        combined = pub_path+'/.'+app.short_name+'.js'
        combine_js(pub_path, tmp_combined)
        recompress = true
        recompress = false if File.exists?(combined) && md5sum(combined) == md5sum(tmp_combined)
        File.cp(tmp_combined, combined)
        File.rm(tmp_combined)
        next unless recompress
        version = 0
        curr = Dir.glob(pub_path+'/'+app.short_name+'.*.js')
        unless curr.empty?
            curr.each do |f|
                name = File.basename(f)
                if name =~ /(\d+)\.js$/
                    version = $1 if $1 > version
                    File.unlink(f)
                end
            end
        end
        version += 1
        dest = "#{pub_path}/#{app.short_name}.#{version}.js"
        compressor = YUI::JavaScriptCompressor.new("charset" => "UTF-8")
        io = open(combined, 'r')
        res = compressor.compress(io)
        open(dest, 'w') do |f|
            f << res
        end
    end
end

.md5sum(file) ⇒ Object



77
78
79
# File 'lib/spiderfw/content_utils.rb', line 77

def self.md5sum(file)
    Digest::MD5.hexdigest(File.read(file))
end

.publishObject



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/spiderfw/content_utils.rb', line 8

def self.publish
    Spider.apps.each do |name, app|
        next unless File.directory?(app.pub_path)
        dest = Spider::HomeController.app_pub_path(app)
        FileUtils.mkdir_p(dest)
        Dir.new(app.pub_path).each do |file|
            next if file[0].chr == '.'
            FileUtils.cp_r("#{app.pub_path}/#{file}", "#{dest}/#{file}")
        end
    end
end

.resolve_css_includes(file) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/spiderfw/content_utils.rb', line 81

def self.resolve_css_includes(file)
    files = [file]
    includes_regexp = /^\s*@import(?:\surl\(|\s)(['"]?)([^\?'"\)\s]+)(\?(?:[^'"\)]*))?\1\)?(?:[^?;]*);?/im
    content_regexp = /\/*|^[\.\#a-zA-Z\:]/
    dir = File.dirname(file)
    IO.foreach(file) do |line|
        catch(:done) do
            throw :done if line =~ content_regexp
            if line =~ includes_regexp
                absolute = nil
                if ($2[0].chr == '/')
                    absolute = $2
                else
                    absolute = File.expand_path(File.join(dir, $2))
                end
                files += resolve_css_includes(absolute)
            end
        end
    end
    files
end