Skip to content

Commit

Permalink
Small refactoring: moved Nokogiri manipulations into a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Chaplinsky committed Aug 2, 2014
1 parent e0deb80 commit 6cfc882
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/polymer-rails.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "polymer-rails/version"
require "polymer-rails/processors/directive_processor"
require "polymer-rails/processors/assets_processor"
require "polymer-rails/processors/components_processor"
require "polymer-rails/helpers/asset_tag_helper"
require "polymer-rails/engine" if defined?(::Rails)
require "polymer-rails/railtie" if defined?(::Rails)
37 changes: 37 additions & 0 deletions lib/polymer-rails/component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Polymer
module Rails
class Component

def initialize(data)
@doc = Nokogiri::HTML.fragment(data)
end

def create_node(name, content)
node = Nokogiri::XML::Node.new(name, @doc)
node.content = content
node
end

def replace_node(node, name, content)
node.replace create_node(name, content)
end

def stylesheets
@doc.css("link[rel='stylesheet']")
end

def javascripts
@doc.css("script[src]")
end

def imports
@doc.css("link[rel='import']")
end

def stringify
@doc.inner_html
end

end
end
end
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
require 'nokogiri'
require 'polymer-rails/component'

module Polymer
module Rails
class AssetsProcessor < Sprockets::Processor
class ComponentsProcessor < Sprockets::Processor

def evaluate(context, locals)
def initialize(context, data)
@context = context
@doc = Nokogiri::HTML::Document.parse(data)
@component = Component.new(data)
end

def process
inline_styles
inline_javascripts
require_imports
@doc.inner_html
@component.stringify
end

private

def require_imports
@doc.css("link[rel='import']").each do |import|
@component.imports.each do |import|
@context.require_asset component_path(import.attributes['href'].value)
import.remove
end
end

def inline_javascripts
@doc.css("script[src]").each do |src|
script = Nokogiri::XML::Node.new('script', @doc)
script.content = asset_content(src.attributes['src'].value)
src.replace script
@component.javascripts.each do |script|
@component.replace_node(script, 'script', asset_content(script.attributes['src'].value))
end
end

def inline_styles
@doc.css("link[rel='stylesheet']").each do |link|
style = Nokogiri::XML::Node.new('style', @doc)
style.content = asset_content(link.attributes['href'].value)
link.replace style
@component.stylesheets.each do |link|
@component.replace_node(link, 'style', asset_content(link.attributes['href'].value))
end
end

Expand All @@ -44,7 +46,6 @@ def asset_content(file)

def component_path(file)
dir = File.dirname(@context.pathname)
path = File.absolute_path(file, dir)
dir.gsub!('/app/assets/', '/vendor/assets/') unless File.exist?(File.absolute_path(file, dir))
File.absolute_path(file, dir)
end
Expand Down
4 changes: 3 additions & 1 deletion lib/polymer-rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class Railtie < ::Rails::Railtie
initializer :add_preprocessors do |app|
app.assets.register_mime_type "text/html", ".html"
app.assets.register_preprocessor "text/html", Polymer::Rails::DirectiveProcessor
app.assets.register_postprocessor 'text/html', Polymer::Rails::AssetsProcessor
app.assets.register_postprocessor 'text/html', :web do |context, data|
Polymer::Rails::ComponentsProcessor.new(context, data).process
end
end

end
Expand Down

0 comments on commit 6cfc882

Please sign in to comment.