mod fit;
mod measure;
mod render;
pub use fit::FitOptions;
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Document {
pub blocks: Vec<Block>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Block {
Paragraph(Vec<Inline>),
Heading {
level: u8,
content: Vec<Inline>,
},
Preformatted {
language: Option<String>,
text: String,
},
Footer(Vec<Inline>),
Divider,
MathBlock(String),
Anchor(String),
List {
ordered: bool,
start: Option<i64>,
items: Vec<ListItem>,
},
Quote {
blocks: Vec<Block>,
credit: Option<Vec<Inline>>,
},
ExpandableQuote {
content: Vec<Inline>,
credit: Option<Vec<Inline>>,
},
PullQuote {
content: Vec<Inline>,
credit: Option<Vec<Inline>>,
},
Media(Media),
Table(Table),
Details {
summary: Vec<Inline>,
blocks: Vec<Block>,
open: bool,
},
Buttons {
align: Option<Align>,
buttons: Vec<UrlButton>,
},
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ListItem {
pub blocks: Vec<Block>,
pub checkbox: Option<bool>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Media {
pub kind: MediaKind,
pub src: String,
pub caption: Option<Vec<Inline>>,
pub credit: Option<Vec<Inline>>,
pub spoiler: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MediaKind {
Photo,
Video,
Animation,
Audio,
Document,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Table {
pub rows: Vec<Vec<TableCell>>,
pub caption: Option<Vec<Inline>>,
pub bordered: bool,
pub striped: bool,
pub compact: bool,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct TableCell {
pub content: Vec<Inline>,
pub header: bool,
pub colspan: Option<u32>,
pub rowspan: Option<u32>,
pub align: Option<Align>,
pub valign: Option<VAlign>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Align {
Left,
Center,
Right,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VAlign {
Top,
Middle,
Bottom,
}
#[derive(Debug, Clone, PartialEq)]
pub struct UrlButton {
pub text: String,
pub url: String,
pub style: Option<ButtonStyle>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ButtonStyle {
Danger,
Success,
Primary,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Inline {
Text(String),
Bold(Vec<Inline>),
Italic(Vec<Inline>),
Underline(Vec<Inline>),
Strikethrough(Vec<Inline>),
Spoiler(Vec<Inline>),
Marked(Vec<Inline>),
Subscript(Vec<Inline>),
Superscript(Vec<Inline>),
Code(String),
Math(String),
Link {
href: String,
content: Vec<Inline>,
},
LineBreak,
}
impl Inline {
pub fn text(s: impl Into<String>) -> Self {
Inline::Text(s.into())
}
}
impl Block {
pub fn paragraph(s: impl Into<String>) -> Self {
Block::Paragraph(vec![Inline::Text(s.into())])
}
pub fn link_button(text: impl Into<String>, url: impl Into<String>) -> Self {
Block::Buttons {
align: None,
buttons: vec![UrlButton {
text: text.into(),
url: url.into(),
style: None,
}],
}
}
}
impl Document {
pub fn new(blocks: Vec<Block>) -> Self {
Self { blocks }
}
pub fn is_empty(&self) -> bool {
self.blocks.is_empty()
}
pub fn for_each_media_mut(&mut self, mut f: impl FnMut(&mut Media)) {
fn walk(blocks: &mut [Block], f: &mut dyn FnMut(&mut Media)) {
for b in blocks {
match b {
Block::Media(m) => f(m),
Block::List { items, .. } => {
for it in items {
walk(&mut it.blocks, f);
}
}
Block::Quote { blocks, .. } | Block::Details { blocks, .. } => walk(blocks, f),
_ => {}
}
}
}
walk(&mut self.blocks, &mut f);
}
pub fn media(&self) -> Vec<&Media> {
fn walk<'a>(blocks: &'a [Block], out: &mut Vec<&'a Media>) {
for b in blocks {
match b {
Block::Media(m) => out.push(m),
Block::List { items, .. } => {
for it in items {
walk(&it.blocks, out);
}
}
Block::Quote { blocks, .. } | Block::Details { blocks, .. } => {
walk(blocks, out)
}
_ => {}
}
}
}
let mut out = Vec::new();
walk(&self.blocks, &mut out);
out
}
}