Module: OllamaChat::KramdownANSI

Included in:
Chat
Defined in:
lib/ollama_chat/kramdown_ansi.rb

Overview

A module that provides Kramdown::ANSI styling configuration and parsing functionality for OllamaChat.

This module handles the setup and application of ANSI styles for markdown rendering, allowing for customizable terminal output formatting. It manages the configuration of ANSI styles either from environment variables or falls back to default settings, and provides methods to parse content with the configured styling.

Examples:

Configuring custom ANSI styles via environment variable

Set KRAMDOWN_ANSI_OLLAMA_CHAT_STYLES to a JSON object containing style
definitions for customizing markdown output formatting in the terminal.

Instance Method Summary collapse

Instance Method Details

#configure_kramdown_ansi_stylesHash

The configure_kramdown_ansi_styles method sets up ANSI styling for Kramdown::ANSI output by checking for specific environment variables and falling back to default styles.

variables or using default settings

Returns:

  • (Hash)

    a hash of ANSI styles configured either from environment



20
21
22
23
24
25
26
# File 'lib/ollama_chat/kramdown_ansi.rb', line 20

def configure_kramdown_ansi_styles
  if env_var = %w[ KRAMDOWN_ANSI_OLLAMA_CHAT_STYLES KRAMDOWN_ANSI_STYLES ].find { ENV.key?(_1) }
    Kramdown::ANSI::Styles.from_env_var(env_var).ansi_styles
  else
    Kramdown::ANSI::Styles.new.ansi_styles
  end
end

#kramdown_ansi_parse(content) ⇒ String

The kramdown_ansi_parse method processes content using Kramdown::ANSI with custom ANSI styles.

This method takes raw content and converts it into formatted ANSI output by applying the instance’s configured ANSI styles. It is used to render content with appropriate terminal formatting based on the application’s styling configuration.

according to the configured styles

Parameters:

  • content (String, nil)

    the raw content to be parsed and formatted. If nil, returns an empty string.

Returns:

  • (String)

    the content formatted with ANSI escape sequences



41
42
43
44
# File 'lib/ollama_chat/kramdown_ansi.rb', line 41

def kramdown_ansi_parse(content)
  content.nil? and return ''
  Kramdown::ANSI.parse(content, ansi_styles: @kramdown_ansi_styles)
end