Sha256: b00fe074b032448978653cc4bf3ef0abed00cf9abd70a897b456a0cf73eb3f66

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 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 syntax_error("Self-closing tags can't have content.") if scanner.scan(/\/ *[^ ]/)
          return ast if scanner.scan(/\//)
          return ast << parse_line(scanner, inline: true)
        elsif scanner.match?(/\//)
          return syntax_error("Illegal nesting: nesting within a self-closing tag is illegal.")
        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

2 entries across 2 versions & 1 rubygems

Version Path
hamlit-1.6.3 lib/hamlit/parsers/tag.rb
hamlit-1.6.2 lib/hamlit/parsers/tag.rb