Class: Kramdown::ANSI::Styles
- Inherits:
-
Object
- Object
- Kramdown::ANSI::Styles
- 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.
Instance Attribute Summary collapse
-
#ansi_styles ⇒ Hash
readonly
The ansi_styles reader method provides access to the current ANSI style configuration.
-
#default_ansi_styles ⇒ Hash
readonly
The default_ansi_styles reader method provides access to the default ANSI style configuration used by the Styles class.
Class Method Summary collapse
-
.from_env_var(name) ⇒ Kramdown::ANSI::Styles
Creates a new Styles instance by parsing JSON configuration from an environment variable.
-
.from_json(json) ⇒ Kramdown::ANSI::Styles
Creates a new instance by parsing JSON configuration and applying it.
Instance Method Summary collapse
-
#apply(name) {|String| ... } ⇒ String
Applies ANSI styles to the given text block based on the specified style name.
-
#as_json(*ignored) ⇒ Hash
(also: #to_hash)
The as_json method converts the current ANSI styles configuration into a JSON-serializable format.
-
#configure(options) ⇒ Kramdown::ANSI::Styles
Configures the ANSI styles using the provided options.
-
#initialize ⇒ Styles
constructor
Initializes a new instance with default ANSI styles configuration.
-
#to_json(*args) ⇒ String
The to_json method converts the current ANSI styles configuration into a JSON-serializable format.
Constructor Details
#initialize ⇒ Styles
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_styles ⇒ Hash (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.
54 55 56 |
# File 'lib/kramdown/ansi/styles.rb', line 54 def ansi_styles @ansi_styles end |
#default_ansi_styles ⇒ Hash (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
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
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
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.
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.
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.
83 84 85 86 87 88 89 90 91 |
# File 'lib/kramdown/ansi/styles.rb', line 83 def configure() = .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() self end |
#to_json(*args) ⇒ String
The to_json method converts the current ANSI styles configuration into a JSON-serializable format.
configuration.
128 129 130 |
# File 'lib/kramdown/ansi/styles.rb', line 128 def to_json(*args) as_json.to_json(*args) end |