Sha256: 6d9e621898f05568ac7dbe721a38ed9a674ff2b69065e83e0a159ce49a0d5b06

Contents?: true

Size: 1.36 KB

Versions: 15

Compression:

Stored size: 1.36 KB

Contents

# Parses an html file's <head> section,
# looks for <script ... > and <link rel="stylesheet" ... >
# and crunches them all together into one file
# 
# Warning: this script is super-ghetto and probably not robust.
# If you're concerned, use a real lexer/parser.

require 'strscan'

module HtmlAssetCrush
  def self.source_for(asset_path)
    case asset_path
    when /js$/
      <<-JS
      <script type="text/javascript" charset="utf-8">
        #{File.open(asset_path).read}
      </script>
      JS
    when /css$/
      <<-CSS
      <style type="text/css">
        #{File.open(asset_path).read}
      </style>
      CSS
    end
  rescue Errno::ENOENT
    raise "Could not find #{asset_path} to bring in"
  end
  
  def self.crush(html_filepath)
    Dir.chdir(File.dirname(html_filepath)) do
      html = File.open(html_filepath).read
      crushed_html = ""    

      s = StringScanner.new(html)

      js = /<script.+? src=['"](.+)['"].+?\/script>/
      css = /<link .+? href=['"](.+?)['"].+?>/
      asset = Regexp.union(js, css)

      while result = s.scan_until(asset) do
        asset_path = s[2] || s[1]

        # Weird that pre_match doesn't do this
        # crushed_html << s.pre_match
        crushed_html << result[0...(-s.matched_size)]

        crushed_html << source_for(asset_path) + "\n"
      end

      crushed_html << s.rest

      return crushed_html
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
edward-crazy_ivan-0.2.0 lib/html_asset_crush.rb
edward-crazy_ivan-0.2.1 lib/html_asset_crush.rb
edward-crazy_ivan-0.2.2 lib/html_asset_crush.rb
edward-crazy_ivan-0.3.0 lib/html_asset_crush.rb
edward-crazy_ivan-0.3.1 lib/html_asset_crush.rb
crazy_ivan-1.1.1 lib/crazy_ivan/html_asset_crush.rb
crazy_ivan-1.1.0 lib/crazy_ivan/html_asset_crush.rb
crazy_ivan-1.0.0 lib/crazy_ivan/html_asset_crush.rb
crazy_ivan-0.3.3 lib/crazy_ivan/html_asset_crush.rb
crazy_ivan-0.3.2 lib/crazy_ivan/html_asset_crush.rb
crazy_ivan-0.3.1 lib/crazy_ivan/html_asset_crush.rb
crazy_ivan-0.3.0 lib/html_asset_crush.rb
crazy_ivan-0.2.2 lib/html_asset_crush.rb
crazy_ivan-0.2.1 lib/html_asset_crush.rb
crazy_ivan-0.2.0 lib/html_asset_crush.rb