Class: Fasti::StyleParser

Inherits:
Object
  • Object
show all
Defined in:
lib/fasti/style_parser.rb

Overview

Parses style definition strings into Style objects for calendar formatting.

This class handles the parsing of style definition strings in the format: “target:attribute=value,attribute,no-attribute target:attribute=value”

Supported targets:

  • Weekdays: sunday, monday, tuesday, wednesday, thursday, friday, saturday

  • Special days: holiday, today

Supported attributes:

  • Colors: foreground=color, background=color

  • Text effects: bold, italic, underline, underline=double, overline, blink, inverse, hide, faint

  • Negation: no-bold, no-italic, etc.

Examples:

Basic usage

parser = StyleParser.new
styles = parser.parse("sunday:bold,foreground=red holiday:bold today:inverse")
styles[:sunday] #=> Style.new(bold: true, foreground: :red)

With negation

styles = parser.parse("sunday:foreground=red,no-bold saturday:no-italic")
styles[:sunday] #=> Style.new(foreground: :red, bold: false)

Instance Method Summary collapse

Instance Method Details

#parse(style_string) ⇒ Hash<Symbol, Style>

Parses a style definition string into a hash of Style objects.

Examples:

parse("sunday:bold,foreground=red holiday:bold today:inverse")
#=> { sunday: Style.new(...), holiday: Style.new(...), today: Style.new(...) }

Raises:

  • (ArgumentError)

    If the style string contains invalid syntax or values



52
53
54
55
56
57
58
59
# File 'lib/fasti/style_parser.rb', line 52

def parse(style_string)
  return {} if style_string.nil? || style_string.strip.empty?

  style_string.strip.split(/\s+/).each_with_object({}) do |entry, styles|
    target, attributes_hash = parse_entry(entry)
    styles[target] = create_style(attributes_hash)
  end
end