Sha256: 65e2cd644a7f44a6b798e2cb4809a39d7c679c955fbeeae0e8163b97e4d8eee8

Contents?: true

Size: 1.77 KB

Versions: 26

Compression:

Stored size: 1.77 KB

Contents

require 'hamlit/concerns/error'
require 'hamlit/concerns/indentable'
require 'hamlit/helpers'

module Hamlit
  module Parsers
    module Tag
      include Concerns::Error
      include Concerns::Indentable

      TAG_ID_CLASS_REGEXP = /[a-zA-Z0-9_-]+/
      TAG_REGEXP  = /[a-zA-Z0-9\-_:]+/
      DEFAULT_TAG = 'div'

      def parse_tag(scanner)
        tag = DEFAULT_TAG
        tag = scanner.scan(TAG_REGEXP) if scanner.scan(/%/)

        attrs = [:haml, :attrs]
        attrs += parse_tag_id_and_class(scanner)
        attrs += parse_attributes(scanner)

        inner_removal = parse_whitespace_removal(scanner)
        ast = [:html, :tag, tag, attrs]

        if scanner.match?(/=/)
          ast << parse_script(scanner)
          return ast
        elsif scanner.scan(/\//)
          return ast
        elsif scanner.rest.match(/[^ ]/)
          ast << parse_text(scanner)
          return ast
        elsif next_indent <= @current_indent
          return ast << [:multi]
        end

        content = [:multi, [:static, "\n"]]
        if inner_removal || Helpers::DEFAULT_PRESERVE_TAGS.include?(tag)
          content[0, 1] = [:haml, :strip]
        end
        content += with_indented { parse_lines }
        ast << content
        ast
      end

      private

      def parse_tag_id_and_class(scanner)
        attributes = Hash.new { |h, k| h[k] = [] }

        while prefix = scanner.scan(/[#.]/)
          name = assert_scan!(scanner, TAG_ID_CLASS_REGEXP)

          case prefix
          when '#'
            attributes['id'] = [name]
          when '.'
            attributes['class'] << name
          end
        end

        ast = []
        attributes.each do |name, values|
          ast << [:html, :attr, name, [:static, values.join(' ')]]
        end
        ast
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
hamlit-1.3.0 lib/hamlit/parsers/tag.rb
hamlit-1.2.1 lib/hamlit/parsers/tag.rb
hamlit-1.2.0 lib/hamlit/parsers/tag.rb
hamlit-1.1.1 lib/hamlit/parsers/tag.rb
hamlit-1.1.0 lib/hamlit/parsers/tag.rb
hamlit-1.0.0 lib/hamlit/parsers/tag.rb
hamlit-0.6.2 lib/hamlit/parsers/tag.rb
hamlit-0.6.1 lib/hamlit/parsers/tag.rb
hamlit-0.6.0 lib/hamlit/parsers/tag.rb
hamlit-0.5.3 lib/hamlit/parsers/tag.rb
hamlit-0.5.2 lib/hamlit/parsers/tag.rb
hamlit-0.5.1 lib/hamlit/parsers/tag.rb
hamlit-0.5.0 lib/hamlit/parsers/tag.rb
hamlit-0.4.3 lib/hamlit/parsers/tag.rb
hamlit-0.4.2 lib/hamlit/parsers/tag.rb
hamlit-0.4.1 lib/hamlit/parsers/tag.rb
hamlit-0.3.4 lib/hamlit/parsers/tag.rb
hamlit-0.3.3 lib/hamlit/parsers/tag.rb
hamlit-0.3.2 lib/hamlit/parsers/tag.rb
hamlit-0.3.1 lib/hamlit/parsers/tag.rb