Sha256: 6d67ed55fc7a27566e4da0dfdb47f45af6b7c837bacbbd25af5ef193f9835e68

Contents?: true

Size: 969 Bytes

Versions: 8

Compression:

Stored size: 969 Bytes

Contents

# encoding: UTF-8
require 'css_parser'
require 'nokogiri'

class Galakei::DocomoCss::InlineStylesheet
  def self.filter(controller)
    return unless controller.request.imode_browser_1_0?
    doc = Nokogiri::HTML(controller.response.body)
    stylesheets = doc.xpath('//link[@rel="stylesheet"]')
    return if stylesheets.empty?
    stylesheets.each do |e|
      e.unlink
      stylesheet = Galakei::DocomoCss::Stylesheet.new(parser(e['href']))
      stylesheet.apply(doc)
    end
    controller.response.body = doc.to_xhtml(:encoding => doc.encoding)
  end

  private
  def self.parser(href)
    parser = CssParser::Parser.new
    uri = URI.parse(href)
    if uri.host && uri.scheme && uri.port
      parser.load_uri!(uri)
    else
      parser.load_file!(path(href))
    end
    return parser
  end

  def self.path(href)
    base_path = href.gsub(ActionController::Base.asset_host || '', '').gsub(/\?\d+/,'')
    File.join(Rails.root, 'public', base_path)
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
galakei-0.7.1 lib/galakei/docomo_css/inline_stylesheet.rb
galakei-0.7.0 lib/galakei/docomo_css/inline_stylesheet.rb
galakei-0.6.6 lib/galakei/docomo_css/inline_stylesheet.rb
galakei-0.6.5 lib/galakei/docomo_css/inline_stylesheet.rb
galakei-0.6.4 lib/galakei/docomo_css/inline_stylesheet.rb
galakei-0.6.3 lib/galakei/docomo_css/inline_stylesheet.rb
galakei-0.6.2 lib/galakei/docomo_css/inline_stylesheet.rb
galakei-0.6.1 lib/galakei/docomo_css/inline_stylesheet.rb