Class: Charming::Markdown::StyleConfig

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

Defined Under Namespace

Classes: Style

Constant Summary collapse

ELEMENTS =
%i[
  document paragraph block_quote list heading h1 h2 h3 h4 h5 h6 text
  strikethrough emph strong hr item enumeration task link link_text image
  image_text code code_block table definition_list definition_term
  definition_description html_block html_span
].freeze
BUILT_INS =
{
  notty: {
    block_quote: {indent: 1, indent_token: "| "},
    list: {level_indent: 2},
    h1: {prefix: "# "},
    h2: {prefix: "## "},
    h3: {prefix: "### "},
    h4: {prefix: "#### "},
    h5: {prefix: "##### "},
    h6: {prefix: "###### "},
    emph: {block_prefix: "*", block_suffix: "*"},
    strong: {block_prefix: "**", block_suffix: "**"},
    strikethrough: {block_prefix: "~~", block_suffix: "~~"},
    hr: {format: "--------"},
    item: {block_prefix: "- "},
    enumeration: {block_prefix: ". "},
    task: {ticked: "[x] ", unticked: "[ ] "},
    code: {block_prefix: "`", block_suffix: "`"},
    code_block: {margin: 1},
    table: {column_separator: "|", row_separator: "-"},
    image_text: {format: "Image: {{text}} ->"}
  },
  dark: {
    document: {color: "252"},
    block_quote: {color: "244", indent: 1, indent_token: ""},
    list: {level_indent: 2},
    heading: {color: "39", bold: true},
    h1: {prefix: " ", suffix: " ", color: "228", background_color: "63", bold: true},
    h2: {prefix: "## "},
    h3: {prefix: "### "},
    h4: {prefix: "#### "},
    h5: {prefix: "##### "},
    h6: {prefix: "###### ", color: "35", bold: false},
    strikethrough: {crossed_out: true},
    emph: {italic: true},
    strong: {bold: true},
    hr: {color: "240", format: "--------"},
    item: {block_prefix: ""},
    enumeration: {block_prefix: ". "},
    task: {ticked: "[✓] ", unticked: "[ ] "},
    link: {color: "30", underline: true},
    link_text: {color: "35", bold: true},
    image: {color: "212", underline: true},
    image_text: {color: "243", format: "Image: {{text}} ->"},
    code: {prefix: " ", suffix: " ", color: "203", background_color: "236"},
    code_block: {color: "244", margin: 1},
    table: {column_separator: "|", row_separator: "-"}
  },
  light: {
    document: {color: "236"},
    block_quote: {color: "244", indent: 1, indent_token: ""},
    list: {level_indent: 2},
    heading: {color: "25", bold: true},
    h1: {prefix: " ", suffix: " ", color: "255", background_color: "33", bold: true},
    h2: {prefix: "## "},
    h3: {prefix: "### "},
    h4: {prefix: "#### "},
    h5: {prefix: "##### "},
    h6: {prefix: "###### ", color: "30", bold: false},
    strikethrough: {crossed_out: true},
    emph: {italic: true},
    strong: {bold: true},
    hr: {color: "250", format: "--------"},
    item: {block_prefix: ""},
    enumeration: {block_prefix: ". "},
    task: {ticked: "[✓] ", unticked: "[ ] "},
    link: {color: "25", underline: true},
    link_text: {color: "90", bold: true},
    image: {color: "162", underline: true},
    image_text: {color: "244", format: "Image: {{text}} ->"},
    code: {prefix: " ", suffix: " ", color: "161", background_color: "255"},
    code_block: {color: "244", margin: 1},
    table: {column_separator: "|", row_separator: "-"}
  }
}.freeze
ATTRIBUTES =
%i[bold faint italic underline reverse strikethrough].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(styles = {}) ⇒ StyleConfig

Returns a new instance of StyleConfig.



199
200
201
202
203
204
# File 'lib/charming/presentation/markdown/style_config.rb', line 199

def initialize(styles = {})
  styles = styles.transform_keys(&:to_sym)
  @styles = ELEMENTS.each_with_object({}) do |element, result|
    result[element] = Style.from(styles[element] || {})
  end.freeze
end

Class Method Details

.builtin(name) ⇒ Object

Raises:

  • (ArgumentError)


181
182
183
184
185
186
# File 'lib/charming/presentation/markdown/style_config.rb', line 181

def self.builtin(name)
  key = name.to_s.tr("-", "_").to_sym
  raise ArgumentError, "unknown markdown style: #{name.inspect}" unless BUILT_INS.key?(key)

  from_hash(BUILT_INS.fetch(key))
end

.from(value) ⇒ Object



188
189
190
191
192
193
# File 'lib/charming/presentation/markdown/style_config.rb', line 188

def self.from(value)
  return value if value.is_a?(self)
  return builtin(value) if value.is_a?(String) || value.is_a?(Symbol)

  from_hash(value || BUILT_INS.fetch(:dark))
end

.from_hash(value) ⇒ Object



195
196
197
# File 'lib/charming/presentation/markdown/style_config.rb', line 195

def self.from_hash(value)
  new(value.to_h)
end

Instance Method Details

#[](name) ⇒ Object



206
207
208
# File 'lib/charming/presentation/markdown/style_config.rb', line 206

def [](name)
  @styles.fetch(name.to_sym) { Style.from({}) }
end

#heading(level) ⇒ Object



210
211
212
# File 'lib/charming/presentation/markdown/style_config.rb', line 210

def heading(level)
  self[:heading].inherit_visual(self[:"h#{level}"])
end