Class: CustomTag::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/custom_tag/base.rb

Constant Summary collapse

@@tags =
{}

Class Method Summary collapse

Class Method Details

.build(tag_name, attrs, content) ⇒ Object



39
40
41
# File 'lib/custom_tag/base.rb', line 39

def self.build(tag_name, attrs, content)
  "<#{tag_name} #{attrs.map { |k, v| "#{k}=\"#{v}\"" }.join(" ")}>#{content}</#{tag_name}>"
end

.register(tag_name, klass) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/custom_tag/base.rb', line 5

def self.register(tag_name, klass)
  @@tags[tag_name.to_s] = klass
  @@tags[tag_name.to_s.gsub(/([A-Z])/) { |m| "_#{m.downcase}" }] = klass
  @@tags[tag_name.to_s.gsub(/([A-Z])/) { |m| "-#{m.downcase}" }] = klass
  @@tags[tag_name.to_s.tr("_", "-").downcase] = klass
  @@tags[tag_name.to_s.gsub(/_([a-z])/) { |m| m[1].upcase }] = klass
  @@tags[tag_name.to_s.tr("-", "_").downcase] = klass
  @@tags[tag_name.to_s.gsub(/-([a-z])/) { |m| m[1].upcase }] = klass
  @@tags[tag_name.to_s.delete("-").downcase] = klass
  @@tags[tag_name.to_s.delete("_").downcase] = klass
  @@tags[tag_name.to_s.downcase] = klass
end

.replace(tag_name, attrs, content) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/custom_tag/base.rb', line 22

def self.replace(tag_name, attrs, content)
  ret = if @@tags[tag_name]
    @@tags[tag_name].build(tag_name, attrs, content)
  else
    build(tag_name, attrs, content)
  end

  doc = Nokogiri::HTML.fragment(ret)
  doc.search("*").each do |element|
    if CustomTag::Base.tags[element.name]
      element.replace(CustomTag::Base.replace(element.name, element.attributes, element.children.to_html))
    end
  end

  doc.to_html.strip
end

.tagsObject



18
19
20
# File 'lib/custom_tag/base.rb', line 18

def self.tags
  @@tags
end