Sha256: 1f7a9f68eacaea692174c52dff344fd3dbe77babe6ae30e8ac6691912bf704bb

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 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)

        unless has_block?
          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

3 entries across 3 versions & 1 rubygems

Version Path
hamlit-1.5.6 lib/hamlit/parsers/tag.rb
hamlit-1.5.5 lib/hamlit/parsers/tag.rb
hamlit-1.5.4 lib/hamlit/parsers/tag.rb