Class: Charming::Markdown::StyleConfig::Style

Inherits:
Data
  • Object
show all
Defined in:
lib/charming/presentation/markdown/style_config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#background_colorObject (readonly)

Returns the value of attribute background_color

Returns:

  • (Object)

    the current value of background_color



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def background_color
  @background_color
end

#block_prefixObject (readonly)

Returns the value of attribute block_prefix

Returns:

  • (Object)

    the current value of block_prefix



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def block_prefix
  @block_prefix
end

#block_suffixObject (readonly)

Returns the value of attribute block_suffix

Returns:

  • (Object)

    the current value of block_suffix



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def block_suffix
  @block_suffix
end

#boldObject (readonly)

Returns the value of attribute bold

Returns:

  • (Object)

    the current value of bold



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def bold
  @bold
end

#colorObject (readonly)

Returns the value of attribute color

Returns:

  • (Object)

    the current value of color



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def color
  @color
end

#column_separatorObject (readonly)

Returns the value of attribute column_separator

Returns:

  • (Object)

    the current value of column_separator



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def column_separator
  @column_separator
end

#faintObject (readonly)

Returns the value of attribute faint

Returns:

  • (Object)

    the current value of faint



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def faint
  @faint
end

#formatObject (readonly)

Returns the value of attribute format

Returns:

  • (Object)

    the current value of format



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def format
  @format
end

#indentObject (readonly)

Returns the value of attribute indent

Returns:

  • (Object)

    the current value of indent



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def indent
  @indent
end

#indent_tokenObject (readonly)

Returns the value of attribute indent_token

Returns:

  • (Object)

    the current value of indent_token



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def indent_token
  @indent_token
end

#italicObject (readonly)

Returns the value of attribute italic

Returns:

  • (Object)

    the current value of italic



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def italic
  @italic
end

#level_indentObject (readonly)

Returns the value of attribute level_indent

Returns:

  • (Object)

    the current value of level_indent



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def level_indent
  @level_indent
end

#marginObject (readonly)

Returns the value of attribute margin

Returns:

  • (Object)

    the current value of margin



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def margin
  @margin
end

#prefixObject (readonly)

Returns the value of attribute prefix

Returns:

  • (Object)

    the current value of prefix



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def prefix
  @prefix
end

#reverseObject (readonly)

Returns the value of attribute reverse

Returns:

  • (Object)

    the current value of reverse



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def reverse
  @reverse
end

#row_separatorObject (readonly)

Returns the value of attribute row_separator

Returns:

  • (Object)

    the current value of row_separator



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def row_separator
  @row_separator
end

#strikethroughObject (readonly)

Returns the value of attribute strikethrough

Returns:

  • (Object)

    the current value of strikethrough



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def strikethrough
  @strikethrough
end

#suffixObject (readonly)

Returns the value of attribute suffix

Returns:

  • (Object)

    the current value of suffix



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def suffix
  @suffix
end

#tickedObject (readonly)

Returns the value of attribute ticked

Returns:

  • (Object)

    the current value of ticked



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def ticked
  @ticked
end

#underlineObject (readonly)

Returns the value of attribute underline

Returns:

  • (Object)

    the current value of underline



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def underline
  @underline
end

#untickedObject (readonly)

Returns the value of attribute unticked

Returns:

  • (Object)

    the current value of unticked



91
92
93
# File 'lib/charming/presentation/markdown/style_config.rb', line 91

def unticked
  @unticked
end

Class Method Details

.from(value) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/charming/presentation/markdown/style_config.rb', line 97

def self.from(value)
  value = symbolize_keys(value || {})
  new(
    block_prefix: value[:block_prefix].to_s,
    block_suffix: value[:block_suffix].to_s,
    prefix: value[:prefix].to_s,
    suffix: value[:suffix].to_s,
    color: normalize_color(value[:color] || value[:foreground] || value[:fg]),
    background_color: normalize_color(value[:background_color] || value[:background] || value[:bg]),
    bold: value[:bold],
    faint: value[:faint],
    italic: value[:italic],
    underline: value[:underline],
    reverse: value[:reverse] || value[:inverse],
    strikethrough: value[:strikethrough] || value[:crossed_out],
    format: value[:format].to_s,
    indent: value[:indent]&.to_i,
    indent_token: value[:indent_token]&.to_s,
    margin: value[:margin]&.to_i,
    level_indent: value[:level_indent]&.to_i,
    ticked: value[:ticked]&.to_s,
    unticked: value[:unticked]&.to_s,
    column_separator: value[:column_separator]&.to_s,
    row_separator: value[:row_separator]&.to_s
  )
end

.normalize_color(value) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/charming/presentation/markdown/style_config.rb', line 172

def self.normalize_color(value)
  return if value.nil?
  return value if value.is_a?(Integer)
  return value.to_i if value.to_s.match?(/\A\d+\z/)

  value
end

.symbolize_keys(value) ⇒ Object



168
169
170
# File 'lib/charming/presentation/markdown/style_config.rb', line 168

def self.symbolize_keys(value)
  value.to_h.each_with_object({}) { |(key, item), result| result[key.to_sym] = item }
end

Instance Method Details

#apply_block_layout(value) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/charming/presentation/markdown/style_config.rb', line 142

def apply_block_layout(value)
  lines = value.to_s.lines(chomp: true)
  lines = [""] if lines.empty?

  if indent&.positive?
    indentation = (indent_token || " ") * indent
    lines = lines.map { |line| "#{indentation}#{line}" }
  end

  rendered = lines.join("\n")
  return rendered unless margin.to_i.positive?

  blank = Array.new(margin.to_i, "").join("\n")
  "#{blank}\n#{rendered}\n#{blank}"
end

#inherit_visual(child) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/charming/presentation/markdown/style_config.rb', line 124

def inherit_visual(child)
  child = self.class.from(child) unless child.is_a?(self.class)
  self.class.new(**child.to_h.merge(
    color: child.color || color,
    background_color: child.background_color || background_color,
    bold: child.bold.nil? ? bold : child.bold,
    faint: child.faint.nil? ? faint : child.faint,
    italic: child.italic.nil? ? italic : child.italic,
    underline: child.underline.nil? ? underline : child.underline,
    reverse: child.reverse.nil? ? reverse : child.reverse,
    strikethrough: child.strikethrough.nil? ? strikethrough : child.strikethrough
  ))
end

#render(value) ⇒ Object



138
139
140
# File 'lib/charming/presentation/markdown/style_config.rb', line 138

def render(value)
  ansi_codes.apply("#{block_prefix}#{prefix}#{value}#{suffix}#{block_suffix}")
end