Sha256: 61375f694e3c059a4601cade51183ece941144c7ff4adb24231c8438ef3a3196

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module Slim
  class Embedded < Filter
    class TagEngine < Engine
      alias orig_tag_on_slim_embedded on_slim_embedded

      def on_slim_embedded(engine, body, attrs)
        body = minify(body) if engine == :css
        orig_tag_on_slim_embedded(engine, body, attrs)
      end

      private

      def minify(body)
        multiline_comment = false
        body.map do |line|
          if line.instance_of?(Array) && line.first == :slim
            remove_comments!(line)
            remove_whitespace!(line)

            stripped_quotes = stripped_quotes(line)
            if stripped_quotes.match?(%r{/\*})
              multiline_comment = true
              next
            elsif multiline_comment
              multiline_comment = false if stripped_quotes.match?(%r{\*/})
              next
            end
            line
          else
            line
          end
        end.compact
      end

      def remove_comments!(line)
        line.last.gsub!(/((?<!['"])\/\*[^*\/]*\*\/?(?<!['"]))/, '')
      end

      def remove_whitespace!(line)
        if line.last.gsub(/\n/, '').match?(/^\s*$/)
          line.last.gsub!(/^\s*$/, '')
        end
      end

      def stripped_quotes(line)
        line.last.gsub(/(['"]).*?\1/, '')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slim-embedded-minify-0.2.0 lib/slim/embedded/minify/tag_engine.rb