Sha256: 687352499f10f37819ed788bb934e0bb6fc8ba328a750f9aecbc6ef01e903fb9

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

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

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

      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)

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

        if !has_block? || scanner.match?(/=|&=|!=/)
          return ast if scanner.scan(/\//)
          return ast << parse_line(scanner, inline: true)
        end

        content = [:multi, [:static, "\n"]]
        if inner_removal || Helpers::DEFAULT_PRESERVE_TAGS.include?(tag)
          content[0, 1] = [:haml, :strip]
        end
        content += with_tag_nested { 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

4 entries across 4 versions & 1 rubygems

Version Path
hamlit-1.6.1 lib/hamlit/parsers/tag.rb
hamlit-1.6.0 lib/hamlit/parsers/tag.rb
hamlit-1.5.9 lib/hamlit/parsers/tag.rb
hamlit-1.5.8 lib/hamlit/parsers/tag.rb