Sha256: bc6250deca51b509ff319d2fdc91f1f76b227ca350792266784ad586447a5db3

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

#
# File::      JavaScriptProcessor.rb
# Author::    wkm
# Copyright:: 2009
# License::   GPL
#

module SiteFuel
  module Processor
    
    require 'jsmin'

    require 'sitefuel/processors/AbstractStringBasedProcessor'

    class JavaScriptProcessor < AbstractStringBasedProcessor

      def self.file_patterns
        ['.js']
      end
      
      # override AbstractProcessor#processor_name so output shows up as +JS+
      # instead of +JavaScript+.
      def self.processor_name
        "JS"
      end

      def self.default_filterset
        :minify
      end

      def self.filterset_minify
        [:minify]
      end

      CDATA_START = '//<![CDATA['
      CDATA_END   = '//]]>'
      def filter_minify
        return nil if @document == nil
        return nil if @document.length == 0

        # JSMin doesn't like having files without any newlines
        @document << "\n"

        # put in CDATA placeholders
        @document.gsub!(CDATA_START, '[[CDATA_START]]')
        @document.gsub!(CDATA_END,   '[[CDATA_END]]')

        # run the minification
        @document = JSMin.minify(@document).strip

        # put back CDATA
        @document.gsub!('[[CDATA_START]]', CDATA_START)
        @document.gsub!('[[CDATA_END]]',   CDATA_END)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sitefuel-0.0.0b lib/sitefuel/processors/JavaScriptProcessor.rb
sitefuel-0.0.0a lib/sitefuel/processors/JavaScriptProcessor.rb