Module: Airgun

Defined in:
lib/airgun.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.css(doc, arg = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/airgun.rb', line 89

def self.css doc, arg = {}
  arg[:exclude] = [] unless arg[:exclude]
  arg[:path] = '' unless arg[:path]
  arg[:erb] = false unless arg[:erb]

  if doc.kind_of? Nokogiri::XML::Node
    c = YUI::CssCompressor.new
    files = []

    doc.xpath("//link[@rel='stylesheet']").each do |node|
      next unless node['href']
      if include? arg[:exclude], node['href']
        puts "excluding #{node['href']}"
        node.remove
        next
      end
      files << node['href']

      puts "expanding #{node['href']}"
      css = c.compress(File.open(File.join(arg[:path], node['href'])))
      n = Nokogiri::XML::Node.new('style', doc)
      n.content = css
      node.add_next_sibling n
      node.remove
    end

    doc.xpath("//style").each do |node|
      node.content = c.compress(node.content)
    end
  else
    c = YUI::CssCompressor.new
    s = File.open(doc, 'r') { |f| f.read }
    s = ERB.new(s).result(binding) if arg[:erb]
    s = c.compress(s)

    if arg[:outfile]
      File.open(arg[:outfile], 'w') { |f| f.write(s) }
    end

    return s
  end
end

.html(htmlfile, arg = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/airgun.rb', line 38

def self.html htmlfile, arg = {}
  arg[:outfile] = nil unless arg[:outfile]
  arg[:exclude] = [] unless arg[:exclude]
  arg[:jscompressor] = :closure unless arg[:jscompressor]
  arg[:fragment] = false unless arg[:fragment]
  arg[:erb] = false unless arg[:erb]
  arg[:compressjs] = true unless arg[:compressjs]
  arg[:compresscss] = true unless arg[:compresscss]
  arg[:path] = File.join(Dir.pwd, File.dirname(htmlfile)) unless arg[:path]

  html = File.open(htmlfile, 'r') { |f| f.read }

  html = ERB.new(html).result(binding) if arg[:erb]

  doc = html

  unless arg[:fragment]
    doc = Nokogiri::HTML(html)
    a = {
      :exclude => arg[:exclude],
      :path => arg[:path]
    }

    Airgun::css doc, a if arg[:compresscss]

    a[:compressor] = arg[:jscompressor]
    Airgun::js doc, a if arg[:compresscss]
  end

  s = doc.to_s
  s = HtmlCompressor::HtmlCompressor.new.compress(s)

  if arg[:outfile]
    File.open(arg[:outfile], 'w') { |f| f.write(s) }
  end

  s
end

.include?(arr, str) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/airgun.rb', line 77

def self.include? arr, str
  arr.any? do |x|
    if x.is_a? String
      r = (str == x)
    elsif x.is_a? Regexp
      r = (str =~ x)
    else raise "cant check #{x}"
    end
    r
  end
end

.js(doc, arg = {}) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/airgun.rb', line 132

def self.js doc, arg = {}
  arg[:compressor] = :closure unless arg[:compressor]
  arg[:exclude] = [] unless arg[:exclude]
  arg[:path] = '' unless arg[:path]
  arg[:erb] = false unless arg[:erb]

  if doc.kind_of? Nokogiri::XML::Node
    c = nil

    if arg[:compressor] == :closure
      c = Closure::Compiler.new :language_in => 'ECMASCRIPT5'
    elsif arg[:compressor] == :yui
      c = YUI::JavaScriptCompressor.new
    else
      raise ':compressor should be set to either :closure (default) or :yui'
    end

    doc.xpath('//script').each do |node|
      if include? arg[:exclude], node['src']
        puts "excluding #{node['src']}"
        node.remove
        next
      end

      if node['src']
        puts "expanding #{node['src']}"
        js = c.compress(File.open(File.join(arg[:path], node['src'])))
        node.remove_attribute 'src'
        node.content = js
      else
        node.content = c.compress(node.text)
      end
    end
  else
    c = nil

    if arg[:compressor] == :closure
      c = Closure::Compiler.new :language_in => 'ECMASCRIPT5'
    elsif arg[:compressor] == :yui
      c = YUI::JavaScriptCompressor.new
    else
      raise 'compressor setting is bad'
    end

    s = File.open(doc, 'r') { |f| f.read }
    s = ERB.new(s).result(binding) if arg[:erb]
    s = c.compress(s)

    if arg[:outfile]
      File.open(arg[:outfile], 'w') { |f| f.write(s) }
    end

    return s
  end
end

.load(file) ⇒ Object



10
11
12
# File 'lib/airgun.rb', line 10

def self.load file
  File.open(file, 'r') { |f| f.read }
end

.path(source, target, arg = {}) ⇒ Object



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/airgun.rb', line 14

def self.path source, target, arg = {}
  unless arg[:target]
    arg[:target] = target[0] == '/' ? target : File.join(Dir.pwd, target)
  end
  unless arg[:htmlmatcher]
    arg[:htmlmatcher] = /.*\.(htm|html)/
  end
  arg[:path] = Dir.pwd unless arg[:path]
  arg[:exclude] = [] unless arg[:exclude]

  Dir[File.join(source, '*')].each do |f|
    if f =~ arg[:htmlmatcher]
      arg = {
        :exclude => arg[:exclude],
        :path => arg[:path],
        :outfile => File.join(arg[:target], f)
      }

      Airgun::html f, arg
    end
  end
  
end