Feat: WIP terse code documentation
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//! Parse markdown into styled ratatui lines with pluggable syntax highlighting.
|
||||
|
||||
mod highlighter;
|
||||
mod parser;
|
||||
mod theme;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user