Class: Rich::Markdown
- Inherits:
-
Object
- Object
- Rich::Markdown
- 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
-
#code_indent ⇒ Integer
readonly
Code block indent.
-
#hyperlinks ⇒ Boolean
readonly
Use hyperlinks.
-
#source ⇒ String
readonly
Source markdown.
-
#styles ⇒ Hash
readonly
Style configuration.
Class Method Summary collapse
-
.from_file(path, **kwargs) ⇒ String
Render markdown from file.
-
.render(source, **kwargs) ⇒ String
Render markdown from string.
Instance Method Summary collapse
-
#initialize(source, styles: {}, hyperlinks: true, code_indent: 4) ⇒ Markdown
constructor
Create a new Markdown renderer.
-
#render(max_width: 80) ⇒ String
Render markdown to string with ANSI codes.
-
#to_segments(max_width: 80) ⇒ Array<Segment>
Convert to segments.
Constructor Details
#initialize(source, styles: {}, hyperlinks: true, code_indent: 4) ⇒ Markdown
Create a new Markdown renderer
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_indent ⇒ Integer (readonly)
Returns Code block indent.
65 66 67 |
# File 'lib/rich/markdown.rb', line 65 def code_indent @code_indent end |
#hyperlinks ⇒ Boolean (readonly)
Returns Use hyperlinks.
62 63 64 |
# File 'lib/rich/markdown.rb', line 62 def hyperlinks @hyperlinks end |
#source ⇒ String (readonly)
Returns Source markdown.
56 57 58 |
# File 'lib/rich/markdown.rb', line 56 def source @source end |
#styles ⇒ Hash (readonly)
Returns 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
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
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
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
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 |