Class: Rich::Markdown

Inherits:
Object
  • Object
show all
Defined in:
lib/rich/markdown.rb

Overview

Markdown rendering for terminal output. Parses and renders Markdown content with styling.

Constant Summary collapse

DEFAULT_STYLES =

Default styles for Markdown elements

{
  # Headings
  h1: Style.new(color: Color.parse("bright_cyan"), bold: true),
  h2: Style.new(color: Color.parse("cyan"), bold: true),
  h3: Style.new(color: Color.parse("bright_blue"), bold: true),
  h4: Style.new(color: Color.parse("blue"), bold: true),
  h5: Style.new(color: Color.parse("bright_magenta")),
  h6: Style.new(color: Color.parse("magenta")),

  # Text formatting
  bold: Style.new(bold: true),
  italic: Style.new(italic: true),
  bold_italic: Style.new(bold: true, italic: true),
  strikethrough: Style.new(strike: true),
  code_inline: Style.new(color: Color.parse("bright_green"), bgcolor: Color.parse("color(236)")),

  # Links and references
  link: Style.new(color: Color.parse("bright_blue"), underline: true),
  link_url: Style.new(color: Color.parse("blue"), dim: true),

  # Lists
  bullet: Style.new(color: Color.parse("yellow")),
  list_number: Style.new(color: Color.parse("yellow")),

  # Blockquotes
  blockquote: Style.new(color: Color.parse("bright_black"), italic: true),
  blockquote_border: Style.new(color: Color.parse("magenta")),

  # Code blocks
  code_block: Style.new(bgcolor: Color.parse("color(236)")),
  code_border: Style.new(color: Color.parse("bright_black")),

  # Horizontal rule
  hr: Style.new(color: Color.parse("bright_black")),

  # Table
  table_header: Style.new(bold: true, color: Color.parse("cyan")),
  table_border: Style.new(color: Color.parse("bright_black"))
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, styles: {}, hyperlinks: true, code_indent: 4) ⇒ Markdown

Create a new Markdown renderer

Parameters:

  • source (String)

    Markdown source text

  • styles (Hash) (defaults to: {})

    Custom styles to override defaults

  • hyperlinks (Boolean) (defaults to: true)

    Enable terminal hyperlinks

  • code_indent (Integer) (defaults to: 4)

    Indent for code blocks



72
73
74
75
76
77
# File 'lib/rich/markdown.rb', line 72

def initialize(source, styles: {}, hyperlinks: true, code_indent: 4)
  @source = source.to_s
  @styles = DEFAULT_STYLES.merge(styles)
  @hyperlinks = hyperlinks
  @code_indent = code_indent
end

Instance Attribute Details

#code_indentInteger (readonly)

Returns Code block indent.

Returns:

  • (Integer)

    Code block indent



65
66
67
# File 'lib/rich/markdown.rb', line 65

def code_indent
  @code_indent
end

Returns Use hyperlinks.

Returns:

  • (Boolean)

    Use hyperlinks



62
63
64
# File 'lib/rich/markdown.rb', line 62

def hyperlinks
  @hyperlinks
end

#sourceString (readonly)

Returns Source markdown.

Returns:

  • (String)

    Source markdown



56
57
58
# File 'lib/rich/markdown.rb', line 56

def source
  @source
end

#stylesHash (readonly)

Returns Style configuration.

Returns:

  • (Hash)

    Style configuration



59
60
61
# File 'lib/rich/markdown.rb', line 59

def styles
  @styles
end

Class Method Details

.from_file(path, **kwargs) ⇒ String

Render markdown from file

Parameters:

  • path (String)

    File path

  • kwargs (Hash)

    Options

Returns:

  • (String)


116
117
118
119
# File 'lib/rich/markdown.rb', line 116

def from_file(path, **kwargs)
  source = File.read(path)
  new(source, **kwargs)
end

.render(source, **kwargs) ⇒ String

Render markdown from string

Parameters:

  • source (String)

    Markdown text

  • kwargs (Hash)

    Options

Returns:

  • (String)


108
109
110
# File 'lib/rich/markdown.rb', line 108

def render(source, **kwargs)
  new(source, **kwargs).render(**kwargs)
end

Instance Method Details

#render(max_width: 80) ⇒ String

Render markdown to string with ANSI codes

Parameters:

  • max_width (Integer) (defaults to: 80)

    Maximum width

Returns:

  • (String)


82
83
84
85
# File 'lib/rich/markdown.rb', line 82

def render(max_width: 80)
  lines = parse_and_render(max_width: max_width)
  lines.join("\n")
end

#to_segments(max_width: 80) ⇒ Array<Segment>

Convert to segments

Parameters:

  • max_width (Integer) (defaults to: 80)

    Maximum width

Returns:



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rich/markdown.rb', line 90

def to_segments(max_width: 80)
  segments = []
  lines = parse_and_render(max_width: max_width)

  lines.each_with_index do |line, i|
    # Line is already a rendered string with ANSI codes
    segments << Segment.new(line)
    segments << Segment.new("\n") if i < lines.length - 1
  end

  segments
end