Sha256: da7fdb4674543521c646684d97145d89fb83942f8e6ecfe6c7a4891718e84e85

Contents?: true

Size: 1.33 KB

Versions: 5

Compression:

Stored size: 1.33 KB

Contents

require 'rhet-butler/stasis/document-transform'
module RhetButler
  module Stasis
    require 'crass'
    class CSSTransform < DocumentTransform
      register "text/css"

      def parse_document
        @parsed = Crass::parse(document.body)
      end

      def url_nodes(node_list)
        return [] if node_list.nil?

        list = []

        node_list.each do |node|
          case node[:node]
          when :url
            list << node
          when :at_rule
            if node[:name] == "import"
              list += node[:prelude].find_all do |node|
                node[:node] == :url or node[:node] == :string
              end
            else
              children = node
              [:block, :value].each do |key|
                children = children[key]
                break if children.nil?
              end
              list += url_nodes(children)
            end
          else
            list += url_nodes(node[:children])
          end
        end
        return list
      end

      def translate_links
        url_nodes(@parsed).uniq.each do |node|
          node[:value] = get_link_translation(node[:value]).to_s
          if node[:node] == :url
            node[:raw] = "url(#{node[:value]})"
          end
        end
      end

      def render_document
        Crass::Parser.stringify(@parsed)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rhet-butler-0.12.1 lib/rhet-butler/stasis/css-transform.rb
rhet-butler-0.11.1 lib/rhet-butler/stasis/css-transform.rb
rhet-butler-0.11.0 lib/rhet-butler/stasis/css-transform.rb
rhet-butler-0.10.0 lib/rhet-butler/stasis/css-transform.rb
rhet-butler-0.9.0 lib/rhet-butler/stasis/css-transform.rb