mod build;
pub mod md;
pub mod term;
pub mod prelude {
pub use crate::build::{
Align, BlockExt, InlineExt, block, bold, code, code_block, h1, h2, h3, h4, h5, h6, hr,
italic, link, ol, p, quote, strikethrough, table, task_list, text, ul,
};
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Block {
Paragraph(Vec<Inline>),
Heading { level: u8, content: Vec<Inline> },
CodeBlock {
language: Option<String>,
content: String,
},
Blockquote(Vec<Block>),
List { ordered: bool, items: Vec<Block> },
TaskList { items: Vec<(bool, Block)> },
Table {
headers: Vec<Inline>,
rows: Vec<Vec<Inline>>,
alignments: Vec<Alignment>,
},
Image { alt: String, url: String },
HorizontalRule,
BlockList(Vec<Block>),
}
impl<T> From<T> for Block
where
T: Into<Inline>,
{
fn from(value: T) -> Self {
Block::Paragraph(vec![value.into()])
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Inline {
Text(String),
Bold(Vec<Inline>),
Italic(Vec<Inline>),
Strikethrough(Vec<Inline>),
Code(String),
Link { text: Vec<Inline>, url: String },
Image { alt: String, url: String },
LineBreak,
}
impl<T> From<T> for Inline
where
T: std::fmt::Display,
{
fn from(value: T) -> Self {
Inline::Text(value.to_string())
}
}
itemize::impl_items!(IntoItems for Block, Inline);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Alignment {
Left,
Center,
Right,
}
pub trait Render {
type Output;
fn render_block(&mut self, inner: &Block) -> Self::Output;
fn render_inline(&mut self, inner: &Inline) -> Self::Output;
}
pub trait Renderable<R> {
type Output;
fn render_with(&self, renderer: &mut R) -> Self::Output;
}
impl<R: Render> Renderable<R> for Block {
type Output = R::Output;
fn render_with(&self, renderer: &mut R) -> Self::Output {
renderer.render_block(self)
}
}
impl<R: Render> Renderable<R> for Inline {
type Output = R::Output;
fn render_with(&self, renderer: &mut R) -> Self::Output {
renderer.render_inline(self)
}
}
impl<R, U, E> Renderable<R> for [U]
where
U: Renderable<R, Output = Result<(), E>>,
{
type Output = U::Output;
fn render_with(&self, renderer: &mut R) -> Result<(), E> {
for item in self {
item.render_with(renderer)?;
}
Ok(())
}
}