class HtmlRB::Tag
Public Class Methods
new(name=nil,content=nil,**attrs,&block)
click to toggle source
# File lib/html_rb/tag.rb, line 3 def initialize(name=nil,content=nil,**attrs,&block) @strings = [] tag name, content, **attrs, &block end
register(tag_name, void: false)
click to toggle source
# File lib/html_rb/tag.rb, line 12 def self.register(tag_name, void: false) method_name = tag_name.to_s.downcase.gsub('-','_') # Rubify the tag name. define_method(method_name) do |content=nil,**attrs,&block| tag tag_name, content, _void: void, **attrs, &block end end
unregister(tag_name)
click to toggle source
# File lib/html_rb/tag.rb, line 19 def self.unregister(tag_name) method_name = tag_name.to_s.downcase.gsub('-','_') # Rubify the tag name. remove_method method_name end
Public Instance Methods
to_s()
click to toggle source
# File lib/html_rb/tag.rb, line 8 def to_s @strings.join end
Private Instance Methods
attribute_key(k)
click to toggle source
# File lib/html_rb/tag.rb, line 70 def attribute_key(k) return k if k.is_a?(String) k.to_s.gsub("_","-") end
attribute_pair(k,v)
click to toggle source
# File lib/html_rb/tag.rb, line 62 def attribute_pair(k,v) if HtmlRB::BOOL_ATTRS.include?(k) attribute_key(k) if v else %Q|#{attribute_key(k)}="#{v}"| end end
attribute_string(hash={})
click to toggle source
ATTRIBUTE HANDLING
# File lib/html_rb/tag.rb, line 55 def attribute_string(hash={}) hash.delete_if{|k,v| v.nil? || v == "" } .map{|k,v| attribute_pair(k,v) } .compact .join(" ") end
render(content)
Really, text is just appending a string, so we can append anything. But saying `text` before some block of HTML stored in a variable feels wrong, so we'll add an alias `render` which feels less strange.
Alias for: text
tag(name=nil, content=nil, _void: false, **attrs, &block)
click to toggle source
# File lib/html_rb/tag.rb, line 30 def tag(name=nil, content=nil, _void: false, **attrs, &block) void = _void raise HtmlRB::Error, "May not provide both content and block" if content && block_given? raise HtmlRB::Error, "Void elements cannot enclose content" if void && (content || block_given?) @strings << "<#{[name,attribute_string(attrs)].join(" ").strip}>" if name return if void @strings << content if content instance_eval(&block) if block_given? @strings << "</#{name}>" if name end
text(content)
click to toggle source
Used for Text Nodes
# File lib/html_rb/tag.rb, line 45 def text(content) tag nil, content end
Also aliased as: render