Class: CustomTag::Base
- Inherits:
-
Object
- Object
- CustomTag::Base
- Defined in:
- lib/custom_tag/base.rb
Constant Summary collapse
{}
Class Method Summary collapse
- .build(tag_name, attrs, content) ⇒ Object
- .register(tag_name, klass) ⇒ Object
- .replace(tag_name, attrs, content) ⇒ Object
- .tags ⇒ Object
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) [tag_name.to_s] = klass [tag_name.to_s.gsub(/([A-Z])/) { |m| "_#{m.downcase}" }] = klass [tag_name.to_s.gsub(/([A-Z])/) { |m| "-#{m.downcase}" }] = klass [tag_name.to_s.tr("_", "-").downcase] = klass [tag_name.to_s.gsub(/_([a-z])/) { |m| m[1].upcase }] = klass [tag_name.to_s.tr("-", "_").downcase] = klass [tag_name.to_s.gsub(/-([a-z])/) { |m| m[1].upcase }] = klass [tag_name.to_s.delete("-").downcase] = klass [tag_name.to_s.delete("_").downcase] = klass [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 [tag_name] [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.[element.name] element.replace(CustomTag::Base.replace(element.name, element.attributes, element.children.to_html)) end end doc.to_html.strip end |
.tags ⇒ Object
18 19 20 |
# File 'lib/custom_tag/base.rb', line 18 def self. end |