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 34 35 36 37 38 |
# File 'lib/kramdown/ansi/styles.rb', line 25 def initialize @default_ansi_styles = { header: %i[ bold underline ], strong: :bold, em: :italic, code: :blue, blockquote: :faint, del: :strikethrough, ins: :underline, mark: :negative, small: :faint, } @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.
59 60 61 |
# File 'lib/kramdown/ansi/styles.rb', line 59 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
49 50 51 |
# File 'lib/kramdown/ansi/styles.rb', line 49 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
77 78 79 |
# File 'lib/kramdown/ansi/styles.rb', line 77 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
66 67 68 |
# File 'lib/kramdown/ansi/styles.rb', line 66 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.
104 105 106 107 108 109 110 111 112 |
# File 'lib/kramdown/ansi/styles.rb', line 104 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.
119 120 121 |
# File 'lib/kramdown/ansi/styles.rb', line 119 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.
88 89 90 91 92 93 94 95 96 |
# File 'lib/kramdown/ansi/styles.rb', line 88 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.
133 134 135 |
# File 'lib/kramdown/ansi/styles.rb', line 133 def to_json(*args) as_json.to_json(*args) end |