class HtmlToHaml::Html::AttributeHandler

Constants

HTML_ONLY_ATTRIBUTE_REGEX

Public Class Methods

new() click to toggle source
# File lib/html_to_haml/tools/html/attribute_handler.rb, line 6
def initialize
  @ids = []
  @classes = []
  @plain_attributes = []
end

Public Instance Methods

add_attribute(attr:) click to toggle source
# File lib/html_to_haml/tools/html/attribute_handler.rb, line 12
def add_attribute(attr:)
  if use_id_syntax?(attr: attr)
    @ids += extract_attribute_value(attr: attr).split(' ')
  elsif use_class_syntax?(attr: attr)
    @classes += extract_attribute_value(attr: attr).split(' ')
  else
    @plain_attributes << attr.strip.gsub(/=/, ': ')
  end
end
attributes_string() click to toggle source
# File lib/html_to_haml/tools/html/attribute_handler.rb, line 22
def attributes_string
  "#{format_ids}#{format_classes}#{format_attributes}"
end

Private Instance Methods

extract_attribute_value(attr:) click to toggle source
# File lib/html_to_haml/tools/html/attribute_handler.rb, line 36
def extract_attribute_value(attr:)
  attr.gsub(/.*=['"](.*)['"]/, '\1').strip
end
format_attributes() click to toggle source
# File lib/html_to_haml/tools/html/attribute_handler.rb, line 40
def format_attributes
 @plain_attributes.empty? ? '' : "{ #{@plain_attributes.join(', ')} }"
end
format_classes() click to toggle source
# File lib/html_to_haml/tools/html/attribute_handler.rb, line 48
def format_classes
  @classes.empty? ? '' : ".#{@classes.join('.')}"
end
format_ids() click to toggle source
# File lib/html_to_haml/tools/html/attribute_handler.rb, line 44
def format_ids
  @ids.empty? ? '' : "##{@ids.join('#')}"
end
use_class_syntax?(attr:) click to toggle source
# File lib/html_to_haml/tools/html/attribute_handler.rb, line 32
def use_class_syntax?(attr:)
  attr =~ /class#{HTML_ONLY_ATTRIBUTE_REGEX}/
end
use_id_syntax?(attr:) click to toggle source
# File lib/html_to_haml/tools/html/attribute_handler.rb, line 28
def use_id_syntax?(attr:)
  attr =~ /id#{HTML_ONLY_ATTRIBUTE_REGEX}/
end