Class: Tiny::Tag
- Inherits:
-
Object
- Object
- Tiny::Tag
- Defined in:
- lib/tiny/tag.rb
Overview
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#tag_name ⇒ Object
readonly
Returns the value of attribute tag_name.
Instance Method Summary collapse
-
#initialize(tag_name, aoc = {}, attrs = nil) ⇒ Tag
constructor
A new instance of Tag.
- #render(&block) ⇒ Object
- #tag_attributes ⇒ Object
- #void_tag? ⇒ Boolean
Constructor Details
#initialize(tag_name, aoc = {}, attrs = nil) ⇒ Tag
Returns a new instance of Tag.
5 6 7 8 9 10 |
# File 'lib/tiny/tag.rb', line 5 def initialize(tag_name, aoc = {}, attrs = nil) @attrs, @content = Hash === aoc && attrs.nil? ? [aoc] : [attrs || {}, aoc] @content = Helpers.sanitize(@content) if @content @tag_name = tag_name end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
4 5 6 |
# File 'lib/tiny/tag.rb', line 4 def attrs @attrs end |
#tag_name ⇒ Object (readonly)
Returns the value of attribute tag_name.
4 5 6 |
# File 'lib/tiny/tag.rb', line 4 def tag_name @tag_name end |
Instance Method Details
#render(&block) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/tiny/tag.rb', line 27 def render(&block) if void_tag? "<#{tag_name}#{tag_attributes} />" else content = @content if block_given? context = eval('self', block.binding) content = context.with_buffer(&block) content.gsub!(/^(?!\s*$)/, " ") content.gsub!(/\A(?!$)|(?<!^|\n)\z/, "\n") end "<#{tag_name}#{tag_attributes}>#{content}</#{tag_name}>" end end |
#tag_attributes ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/tiny/tag.rb', line 12 def tag_attributes tag_attrs = attrs.map do |name, val| next if val.nil? || val == [] || val == false next name if val == true vals = [*val].map do |value| Helpers.escape_html value end %(#{name}="#{vals.join(' ')}") end.compact.join(' ') " #{tag_attrs}" unless tag_attrs.empty? end |
#void_tag? ⇒ Boolean
43 44 45 |
# File 'lib/tiny/tag.rb', line 43 def void_tag? HTML..include? tag_name.to_s end |