module Airgun

Constants

VERSION

Public Class Methods

css(doc, arg = {}) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# File lib/airgun.rb, line 10
def self.load file
  File.open(file, 'r') { |f| f.read }
end
path(source, target, arg = {}) click to toggle source
# 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