Class: BBortcodes::Shortcode
- Inherits:
-
Literal::Object
- Object
- Literal::Object
- BBortcodes::Shortcode
- Defined in:
- lib/bbortcodes/shortcode.rb
Class Method Summary collapse
-
.allowed_children ⇒ Object
Override in subclasses to specify allowed child shortcodes Return :all to allow all children Return [] to allow no children (content is just text) Return array of shortcode classes to allow specific children.
-
.allows_child?(shortcode_class) ⇒ Boolean
Check if a given shortcode class is allowed as a child.
-
.shortcode_attribute_reader(name, attribute_name: name) ⇒ void
Defines a helper method to retrieve a shortcode attribute.
-
.tag_name ⇒ Object
Override in subclasses to specify the shortcode tag name If not overridden, uses the class name converted to snake_case.
Instance Method Summary collapse
-
#attribute(name, default = nil, escape: nil) ⇒ String, Object
Get the value of an attribute with optional default Automatically escapes HTML if auto_escape_attributes is enabled.
-
#escape_html(text) ⇒ String
Escape HTML entities to prevent XSS attacks.
-
#has_attribute?(name) ⇒ Boolean
Check if an attribute exists.
-
#render(context) ⇒ String
Render the shortcode to a string Must be implemented by subclasses.
-
#render_content(context, parser) ⇒ String
Helper method to render content (for paired shortcodes) This will recursively render nested shortcodes and text.
Class Method Details
.allowed_children ⇒ Object
Override in subclasses to specify allowed child shortcodes Return :all to allow all children Return [] to allow no children (content is just text) Return array of shortcode classes to allow specific children
31 32 33 |
# File 'lib/bbortcodes/shortcode.rb', line 31 def allowed_children :all end |
.allows_child?(shortcode_class) ⇒ Boolean
Check if a given shortcode class is allowed as a child
36 37 38 39 40 41 |
# File 'lib/bbortcodes/shortcode.rb', line 36 def allows_child?(shortcode_class) return true if allowed_children == :all return false if allowed_children.empty? allowed_children.include?(shortcode_class) end |
.shortcode_attribute_reader(name, attribute_name: name) ⇒ void
This method returns an undefined value.
Defines a helper method to retrieve a shortcode attribute.
This macro creates an instance method that retrieves the specified attribute value.
The generated method respects the auto_escape_attributes configuration setting.
88 89 90 |
# File 'lib/bbortcodes/shortcode.rb', line 88 def shortcode_attribute_reader(name, attribute_name: name) define_method(name) { attribute(attribute_name) } end |
.tag_name ⇒ Object
Override in subclasses to specify the shortcode tag name If not overridden, uses the class name converted to snake_case
18 19 20 21 22 23 |
# File 'lib/bbortcodes/shortcode.rb', line 18 def tag_name name.split("::").last .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .downcase end |
Instance Method Details
#attribute(name, default = nil, escape: nil) ⇒ String, Object
Get the value of an attribute with optional default Automatically escapes HTML if auto_escape_attributes is enabled
151 152 153 154 155 156 157 158 159 |
# File 'lib/bbortcodes/shortcode.rb', line 151 def attribute(name, default = nil, escape: nil) value = @attributes.fetch(name.to_s, default) return value if value.nil? # Determine if we should escape should_escape = escape.nil? ? BBortcodes.config.auto_escape_attributes : escape should_escape ? escape_html(value) : value end |
#escape_html(text) ⇒ String
Escape HTML entities to prevent XSS attacks
138 139 140 141 |
# File 'lib/bbortcodes/shortcode.rb', line 138 def escape_html(text) return text if text.nil? CGI.escapeHTML(text.to_s) end |
#has_attribute?(name) ⇒ Boolean
Check if an attribute exists
162 163 164 |
# File 'lib/bbortcodes/shortcode.rb', line 162 def has_attribute?(name) @attributes.key?(name.to_s) end |
#render(context) ⇒ String
Render the shortcode to a string Must be implemented by subclasses
99 100 101 |
# File 'lib/bbortcodes/shortcode.rb', line 99 def render(context) raise NotImplementedError, "#{self.class.name} must implement #render" end |
#render_content(context, parser) ⇒ String
Helper method to render content (for paired shortcodes) This will recursively render nested shortcodes and text
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/bbortcodes/shortcode.rb', line 110 def render_content(context, parser) return "" if @content.nil? || @content.empty? @content.map do |node| case node when Shortcode node.render(context) when String node when Hash # Handle raw parsed nodes that haven't been converted to Shortcode instances yet if node[:type] == :text node[:value] else # This is a parsed shortcode node, let the parser handle it "" end else node.to_s end end.join end |