Feat: WIP terse code documentation

This commit is contained in:
2026-02-26 01:08:16 +01:00
parent 71bd09d5ea
commit e1cf57918e
47 changed files with 499 additions and 24 deletions

View File

@@ -1,9 +1,13 @@
//! Syntax highlighting trait for fenced code blocks in markdown.
use ratatui::style::Style;
/// Produce styled spans from a single line of source code.
pub trait CodeHighlighter {
fn highlight(&self, line: &str) -> Vec<(Style, String)>;
}
/// Pass-through highlighter that applies no styling.
pub struct NoHighlight;
impl CodeHighlighter for NoHighlight {

View File

@@ -1,3 +1,5 @@
//! Parse markdown into styled ratatui lines with pluggable syntax highlighting.
mod highlighter;
mod parser;
mod theme;

View File

@@ -1,3 +1,5 @@
//! Parse markdown text into styled ratatui lines with syntax-highlighted code blocks.
use minimad::{Composite, CompositeStyle, Compound, Line, TableRow};
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line as RLine, Span};
@@ -5,17 +7,20 @@ use ratatui::text::{Line as RLine, Span};
use crate::highlighter::CodeHighlighter;
use crate::theme::MarkdownTheme;
/// Span of lines within a parsed document that form a fenced code block.
pub struct CodeBlock {
pub start_line: usize,
pub end_line: usize,
pub source: String,
}
/// Result of parsing a markdown string: styled lines and extracted code blocks.
pub struct ParsedMarkdown {
pub lines: Vec<RLine<'static>>,
pub code_blocks: Vec<CodeBlock>,
}
/// Parse markdown text into themed, syntax-highlighted ratatui lines.
pub fn parse<T: MarkdownTheme, H: CodeHighlighter>(
md: &str,
theme: &T,
@@ -44,7 +49,7 @@ pub fn parse<T: MarkdownTheme, H: CodeHighlighter>(
let close_block = |start: Option<usize>,
source: &mut Vec<String>,
blocks: &mut Vec<CodeBlock>,
lines: &Vec<RLine<'static>>| {
lines: &[RLine<'static>]| {
if let Some(start) = start {
blocks.push(CodeBlock {
start_line: start,
@@ -118,7 +123,7 @@ pub fn parse<T: MarkdownTheme, H: CodeHighlighter>(
ParsedMarkdown { lines, code_blocks }
}
pub fn preprocess_markdown(md: &str) -> String {
fn preprocess_markdown(md: &str) -> String {
let mut out = String::with_capacity(md.len());
for line in md.lines() {
let line = convert_dash_lists(line);
@@ -162,7 +167,7 @@ pub fn preprocess_markdown(md: &str) -> String {
out
}
pub fn convert_dash_lists(line: &str) -> String {
fn convert_dash_lists(line: &str) -> String {
let trimmed = line.trim_start();
if let Some(rest) = trimmed.strip_prefix("- ") {
let indent = line.len() - trimmed.len();

View File

@@ -1,5 +1,8 @@
//! Style provider trait for markdown rendering.
use ratatui::style::{Color, Modifier, Style};
/// Style provider for each markdown element type.
pub trait MarkdownTheme {
fn h1(&self) -> Style;
fn h2(&self) -> Style;
@@ -16,6 +19,7 @@ pub trait MarkdownTheme {
fn table_row_odd(&self) -> Color;
}
/// Fallback theme with hardcoded terminal colors, used in tests.
pub struct DefaultTheme;
impl MarkdownTheme for DefaultTheme {