Class: Markawesome::IconSlotParser
- Inherits:
-
Object
- Object
- Markawesome::IconSlotParser
- Defined in:
- lib/markawesome/icon_slot_parser.rb
Overview
Parses icon:… tokens from parameter strings for Web Awesome component icon slots Supports default slot mapping, explicit slot names, and slot name remapping
Examples:
"icon:gear" → default slot gets "gear"
"icon:end:arrow-right" → "end" slot gets "arrow-right"
"success icon:gear large" → icons: { 'start' => 'gear' }, remaining: "success large"
Class Method Summary collapse
-
.parse(params_string, slot_config) ⇒ Hash
Parse icon tokens from a parameter string.
-
.to_html(icons_hash, slot_map = nil) ⇒ String
Generate <wa-icon> HTML elements for parsed icons.
Class Method Details
.parse(params_string, slot_config) ⇒ Hash
Parse icon tokens from a parameter string
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/markawesome/icon_slot_parser.rb', line 19 def self.parse(params_string, slot_config) return { icons: {}, remaining: '' } if params_string.nil? || params_string.strip.empty? icons = {} remaining_tokens = [] tokens = params_string.strip.split(/\s+/) tokens.each do |token| if token.start_with?('icon:') parts = token.split(':', 3) # ["icon", slot_or_name, maybe_name] if parts.length == 3 # Explicit slot: icon:slot:name slot = parts[1] name = parts[2] icons[slot] = name if slot_config[:slots]&.include?(slot) elsif parts.length == 2 && slot_config[:default] # Default slot: icon:name name = parts[1] icons[slot_config[:default]] = name unless name.empty? end else remaining_tokens << token end end { icons: icons, remaining: remaining_tokens.join(' ') } end |
.to_html(icons_hash, slot_map = nil) ⇒ String
Generate <wa-icon> HTML elements for parsed icons
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/markawesome/icon_slot_parser.rb', line 52 def self.to_html(icons_hash, slot_map = nil) icons_hash.map do |slot, name| html_slot = slot_map ? (slot_map[slot] || slot) : slot if html_slot == 'content' "<wa-icon name=\"#{name}\"></wa-icon>" else "<wa-icon slot=\"#{html_slot}\" name=\"#{name}\"></wa-icon>" end end.join end |