Module: RuVim::RichView::MarkdownRenderer
- Defined in:
- lib/ruvim/rich_view/markdown_renderer.rb
Constant Summary collapse
- HEADING_STYLES =
--- Heading rendering ---
Lang::Markdown::HEADING_COLORS
Class Method Summary collapse
-
.apply_inline(line) ⇒ Object
--- Inline decoration ---.
- .build_table_group(table_lines) ⇒ Object
- .cursor_display_col(raw_line, cursor_x, visible_lines:, delimiter:) ⇒ Object
- .delimiter_for(_format) ⇒ Object
-
.identify_table_groups(lines) ⇒ Object
--- Table rendering ---.
- .needs_pre_context? ⇒ Boolean
- .pad_cell(cell, target_width) ⇒ Object
- .render_heading(line, level) ⇒ Object
-
.render_hr(line) ⇒ Object
--- HR rendering ---.
-
.render_line(line, state, table_group) ⇒ Object
--- Line rendering ---.
- .render_table_data_row(line, group) ⇒ Object
- .render_table_line(line, stripped, group) ⇒ Object
- .render_table_separator(group) ⇒ Object
- .render_visible(lines, delimiter:, context: {}) ⇒ Object
-
.table_cursor_display_col(raw_line, cursor_x, group) ⇒ Object
--- Table cursor mapping ---.
Class Method Details
.apply_inline(line) ⇒ Object
--- Inline decoration ---
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ruvim/rich_view/markdown_renderer.rb', line 107 def apply_inline(line) result = line.to_s.dup # Checkbox (must come before bold/italic to avoid interference) result = result.gsub(Lang::Markdown::CHECKBOX_CHECKED_RE) { "#{$1}\e[32m[x]\e[m" } result = result.gsub(Lang::Markdown::CHECKBOX_UNCHECKED_RE) { "#{$1}\e[90m[ ]\e[m" } # Bold **text** result = result.gsub(Lang::Markdown::BOLD_RE) { "\e[1m**#{$1}**\e[22m" } # Italic *text* (but not ** which is bold) result = result.gsub(Lang::Markdown::ITALIC_RE) { "\e[3m*#{$1}*\e[23m" } # Inline code `text` result = result.gsub(Lang::Markdown::INLINE_CODE_RE) { "\e[33m`#{$1}`\e[m" } # Links [text](url) result = result.gsub(Lang::Markdown::LINK_RE) { "\e[4m#{$1}\e[24m(\e[2m#{$2}\e[22m)" } result end |
.build_table_group(table_lines) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/ruvim/rich_view/markdown_renderer.rb', line 151 def build_table_group(table_lines) return nil if table_lines.nil? || table_lines.empty? cells = table_lines.map { |l| Lang::Markdown.parse_table_cells(l) } max_cols = cells.map(&:length).max return nil if max_cols.nil? || max_cols.zero? col_widths = Array.new(max_cols, 0) cells.each do |row| row.each_with_index do |cell, ci| w = RuVim::DisplayWidth.display_width(cell) col_widths[ci] = w if w > col_widths[ci] end end { col_widths: col_widths, max_cols: max_cols } end |
.cursor_display_col(raw_line, cursor_x, visible_lines:, delimiter:) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ruvim/rich_view/markdown_renderer.rb', line 35 def cursor_display_col(raw_line, cursor_x, visible_lines:, delimiter:) if Lang::Markdown.table_line?(raw_line) table_lines = visible_lines.select { |l| Lang::Markdown.table_line?(l) } group = build_table_group(table_lines) if group return table_cursor_display_col(raw_line, cursor_x, group) end end RuVim::TextMetrics.screen_col_for_char_index(raw_line, cursor_x) end |
.delimiter_for(_format) ⇒ Object
8 9 10 |
# File 'lib/ruvim/rich_view/markdown_renderer.rb', line 8 def delimiter_for(_format) nil end |
.identify_table_groups(lines) ⇒ Object
--- Table rendering ---
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/ruvim/rich_view/markdown_renderer.rb', line 131 def identify_table_groups(lines) groups = {} i = 0 while i < lines.length if Lang::Markdown.table_line?(lines[i]) start = i table_lines = [] while i < lines.length && Lang::Markdown.table_line?(lines[i]) table_lines << lines[i] i += 1 end group = build_table_group(table_lines) (start...(start + table_lines.length)).each { |j| groups[j] = group } else i += 1 end end groups end |
.needs_pre_context? ⇒ Boolean
12 13 14 |
# File 'lib/ruvim/rich_view/markdown_renderer.rb', line 12 def needs_pre_context? true end |
.pad_cell(cell, target_width) ⇒ Object
197 198 199 200 201 |
# File 'lib/ruvim/rich_view/markdown_renderer.rb', line 197 def pad_cell(cell, target_width) current = RuVim::DisplayWidth.display_width(cell) gap = target_width - current gap > 0 ? "#{cell}#{" " * gap}" : cell end |
.render_heading(line, level) ⇒ Object
92 93 94 95 |
# File 'lib/ruvim/rich_view/markdown_renderer.rb', line 92 def render_heading(line, level) style = HEADING_STYLES[level] || HEADING_STYLES[6] "#{style}#{line}\e[m" end |
.render_hr(line) ⇒ Object
--- HR rendering ---
99 100 101 102 103 |
# File 'lib/ruvim/rich_view/markdown_renderer.rb', line 99 def render_hr(line) # Replace the HR marker with box-drawing horizontal line width = [line.length, 3].max "\e[90m#{"─" * width}\e[m" end |
.render_line(line, state, table_group) ⇒ Object
--- Line rendering ---
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ruvim/rich_view/markdown_renderer.rb', line 48 def render_line(line, state, table_group) stripped = line.to_s.strip # Code fence open/close if !state.in_code_block && Lang::Markdown.fence_line?(stripped) return "\e[90m#{line}\e[m" end if state.in_code_block if Lang::Markdown.fence_line?(stripped) return "\e[90m#{line}\e[m" end return "\e[38;5;223m#{line}\e[m" end # HR if Lang::Markdown.horizontal_rule?(stripped) return render_hr(line) end # Heading level = Lang::Markdown.heading_level(line) if level > 0 return render_heading(line, level) end # Block quote if Lang::Markdown.block_quote?(line) return "\e[36m#{apply_inline(line)}\e[m" end # Table if table_group return render_table_line(line, stripped, table_group) end # Default: inline decoration only apply_inline(line) end |
.render_table_data_row(line, group) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/ruvim/rich_view/markdown_renderer.rb', line 183 def render_table_data_row(line, group) col_widths = group[:col_widths] cells = Lang::Markdown.parse_table_cells(line) padded = cells.each_with_index.map do |cell, i| target = col_widths[i] || 0 pad_cell(cell, target) end # Fill missing columns (cells.length...group[:max_cols]).each do |i| padded << " " * (col_widths[i] || 0) end "│ #{padded.join(" │ ")} │" end |
.render_table_line(line, stripped, group) ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/ruvim/rich_view/markdown_renderer.rb', line 169 def render_table_line(line, stripped, group) if Lang::Markdown.table_separator?(stripped) render_table_separator(group) else render_table_data_row(line, group) end end |
.render_table_separator(group) ⇒ Object
177 178 179 180 181 |
# File 'lib/ruvim/rich_view/markdown_renderer.rb', line 177 def render_table_separator(group) col_widths = group[:col_widths] cells = col_widths.map { |w| "─" * (w + 2) } "├#{cells.join("┼")}┤" end |
.render_visible(lines, delimiter:, context: {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ruvim/rich_view/markdown_renderer.rb', line 16 def render_visible(lines, delimiter:, context: {}) return lines if lines.nil? || lines.empty? state = Lang::Markdown::FenceState.new pre = context[:pre_context_lines] if pre pre.each { |l| state.scan_line(l) } end # Identify table groups for column-width alignment table_groups = identify_table_groups(lines) lines.each_with_index.map do |line, idx| rendered = render_line(line, state, table_groups[idx]) state.scan_line(line) rendered end end |
.table_cursor_display_col(raw_line, cursor_x, group) ⇒ Object
--- Table cursor mapping ---
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/ruvim/rich_view/markdown_renderer.rb', line 205 def table_cursor_display_col(raw_line, cursor_x, group) col_widths = group[:col_widths] cells = Lang::Markdown.parse_table_cells(raw_line) # Map cursor_x (in raw line) to display col in formatted line # Raw line: "| cell1 | cell2 |" # Formatted: "│ cell1 │ cell2 │" # Walk through the raw line to find which cell cursor_x is in pos = 0 raw = raw_line.to_s # Skip leading whitespace pos += 1 while pos < raw.length && raw[pos] == " " # Skip leading | pos += 1 if pos < raw.length && raw[pos] == "|" display_col = 2 # "│ " prefix cells.each_with_index do |cell, ci| # Skip whitespace before cell content pos += 1 while pos < raw.length && raw[pos] == " " cell_start = pos cell_end = cell_start + cell.length if cursor_x < cell_end || ci == cells.length - 1 # Cursor is in this cell offset_in_cell = [cursor_x - cell_start, 0].max offset_in_cell = [offset_in_cell, cell.length].min return display_col + RuVim::DisplayWidth.display_width(cell[0...offset_in_cell]) end display_col += (col_widths[ci] || 0) + 3 # " │ " pos = cell_end # Skip whitespace after cell pos += 1 while pos < raw.length && raw[pos] == " " # Skip | pos += 1 if pos < raw.length && raw[pos] == "|" end display_col end |