Class: Kramdown::ANSI::Styles

Inherits:
Object
  • Object
show all
Defined in:
lib/kramdown/ansi/styles.rb

Overview

A configuration class for managing ANSI styles used in Markdown rendering.

The Styles class provides functionality to initialize default ANSI styling options for various Markdown elements and allows customization through JSON configuration or environment variables.

Examples:

Using default styles

styles = Kramdown::ANSI::Styles.new

Creating styles from JSON configuration

json_config = '{"header": ["bold", "underline"], "code": "red"}'
styles = Kramdown::ANSI::Styles.from_json(json_config)

Creating styles from environment variable

ENV['MY_STYLES'] = '{"em": "italic"}'
styles = Kramdown::ANSI::Styles.from_env_var('MY_STYLES')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStyles

Initializes a new instance with default ANSI styles configuration.

Sets up the default styling options for various Markdown elements and prepares the instance variable to hold the current ANSI style configuration.



25
26
27
28
29
30
31
32
33
# File 'lib/kramdown/ansi/styles.rb', line 25

def initialize
  @default_ansi_styles = {
    header: %i[ bold underline ],
    strong: :bold,
    em:     :italic,
    code:   :blue,
  }
  @ansi_styles = @default_ansi_styles.dup
end

Instance Attribute Details

#ansi_stylesHash (readonly)

The ansi_styles reader method provides access to the current ANSI style configuration.

This method returns the @ansi_styles instance variable, which holds the active ANSI styling options used for formatting Markdown elements in terminal output.

Returns:

  • (Hash)

    the current ANSI styles configuration hash



54
55
56
# File 'lib/kramdown/ansi/styles.rb', line 54

def ansi_styles
  @ansi_styles
end

#default_ansi_stylesHash (readonly)

The default_ansi_styles reader method provides access to the default ANSI style configuration used by the Styles class.

This method returns the @default_ansi_styles instance variable, which holds the predefined styling options for various Markdown elements before any custom configuration has been applied.

and style value arrays or single style values

Returns:

  • (Hash)

    the default ANSI styles configuration hash containing symbol keys



44
45
46
# File 'lib/kramdown/ansi/styles.rb', line 44

def default_ansi_styles
  @default_ansi_styles
end

Class Method Details

.from_env_var(name) ⇒ Kramdown::ANSI::Styles

Creates a new Styles instance by parsing JSON configuration from an environment variable.

JSON configuration configuration applied

Parameters:

  • name (String)

    the name of the environment variable containing the

Returns:



72
73
74
# File 'lib/kramdown/ansi/styles.rb', line 72

def self.from_env_var(name)
  from_json(ENV.fetch(name))
end

.from_json(json) ⇒ Kramdown::ANSI::Styles

Creates a new instance by parsing JSON configuration and applying it.

configuration applied

Parameters:

  • json (String)

    A JSON string containing the configuration options

Returns:



61
62
63
# File 'lib/kramdown/ansi/styles.rb', line 61

def self.from_json(json)
  new.configure(JSON.parse(json))
end

Instance Method Details

#apply(name) {|String| ... } ⇒ String

Applies ANSI styles to the given text block based on the specified style name.

Parameters:

  • name (Symbol)

    the style name to apply

Yields:

  • (String)

    the text to be styled

Returns:

  • (String)

    the text with ANSI styles applied



99
100
101
102
103
104
105
106
107
# File 'lib/kramdown/ansi/styles.rb', line 99

def apply(name, &block)
  styles = Array(@ansi_styles.fetch(name))
  styles.reverse_each.reduce(block.()) do |text, style|
    if style.is_a?(Symbol) || style.is_a?(String)
      text = Term::ANSIColor.send(style) { text }
    end
    text
  end
end

#as_json(*ignored) ⇒ Hash Also known as: to_hash

The as_json method converts the current ANSI styles configuration into a JSON-serializable format.

Parameters:

  • ignored (Array)

    ignored args

Returns:

  • (Hash)

    A hash representation of the current ANSI styles configuration.



114
115
116
# File 'lib/kramdown/ansi/styles.rb', line 114

def as_json(*ignored)
  @ansi_styles
end

#configure(options) ⇒ Kramdown::ANSI::Styles

Configures the ANSI styles using the provided options.

Merges the given options with the default ANSI styles to customize the styling configuration for Markdown elements.

Parameters:

  • options (Hash)

    a hash containing style configuration options

Returns:



83
84
85
86
87
88
89
90
91
# File 'lib/kramdown/ansi/styles.rb', line 83

def configure(options)
  options_ansi_styles = options.to_h.transform_keys(&:to_sym).
    transform_values {
      array = Array(_1).map(&:to_sym)
      array.size == 1 ? array.first : array
    }
  @ansi_styles = @default_ansi_styles.merge(options_ansi_styles)
  self
end

#to_json(*args) ⇒ String

The to_json method converts the current ANSI styles configuration into a JSON-serializable format.

configuration.

Parameters:

  • args (Array)

    pass-through args

Returns:

  • (String)

    A JSON string representation of the current ANSI styles



128
129
130
# File 'lib/kramdown/ansi/styles.rb', line 128

def to_json(*args)
  as_json.to_json(*args)
end