Class: Tiny::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/tiny/tag.rb

Overview

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#attrsObject (readonly)

Returns the value of attribute attrs.



4
5
6
# File 'lib/tiny/tag.rb', line 4

def attrs
  @attrs
end

#tag_nameObject (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_attributesObject



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

Returns:

  • (Boolean)


43
44
45
# File 'lib/tiny/tag.rb', line 43

def void_tag?
  HTML.void_tags.include? tag_name.to_s
end