Sha256: 0cfa341a78f08797d4d93ec11fbab1ceee7dd8a5f11cb3b2565a7db10e2d85a3

Contents?: true

Size: 882 Bytes

Versions: 16

Compression:

Stored size: 882 Bytes

Contents

require 'hamlit/filters/base'

module Hamlit
  module Filters
    class Javascript < Base
      def compile(lines)
        return compile_xhtml(lines) if options[:format] == :xhtml

        compile_html(lines)
      end

      private

      def compile_html(lines)
        ast = [:haml, :text, compile_lines(lines, indent_width: 2), false]
        ast = [:multi, [:static, "\n"], ast]
        ast = [:html, :tag, 'script', [:html, :attrs], ast]
        ast
      end

      def compile_xhtml(lines)
        attr  = [:html, :attr, 'type', [:static, 'text/javascript']]
        attrs = [:html, :attrs, attr]

        multi = [:multi, [:static, "\n"]]
        multi << [:static, "  //<![CDATA[\n"]
        multi << [:haml, :text, compile_lines(lines, indent_width: 4)]
        multi << [:static, "  //]]>\n"]

        [:html, :tag, 'script', attrs, multi]
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
hamlit-1.6.7 lib/hamlit/filters/javascript.rb
hamlit-1.6.6 lib/hamlit/filters/javascript.rb
hamlit-1.6.5 lib/hamlit/filters/javascript.rb
hamlit-1.6.4 lib/hamlit/filters/javascript.rb
hamlit-1.6.3 lib/hamlit/filters/javascript.rb
hamlit-1.6.2 lib/hamlit/filters/javascript.rb
hamlit-1.6.1 lib/hamlit/filters/javascript.rb
hamlit-1.6.0 lib/hamlit/filters/javascript.rb
hamlit-1.5.9 lib/hamlit/filters/javascript.rb
hamlit-1.5.8 lib/hamlit/filters/javascript.rb
hamlit-1.5.7 lib/hamlit/filters/javascript.rb
hamlit-1.5.6 lib/hamlit/filters/javascript.rb
hamlit-1.5.5 lib/hamlit/filters/javascript.rb
hamlit-1.5.4 lib/hamlit/filters/javascript.rb
hamlit-1.5.3 lib/hamlit/filters/javascript.rb
hamlit-1.5.2 lib/hamlit/filters/javascript.rb