#![deny(unsafe_code)]
mod block;
mod frontmatter;
pub use frontmatter::METADATA_ID;
mod code;
mod inline;
mod list;
mod table;
mod tag;
pub use tag::tagged;
mod width;
mod wrap;
#[cfg(test)]
mod tests;
#[allow(unused_imports)]
pub use width::{char_width, pad_right, repeat, str_width, take_width, truncate_width, visible, CONTROL};
use crate::md::ast::Document;
use crate::term::Style;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Span {
pub text: String,
pub style: Style,
pub link: Option<String>,
}
impl Span {
pub fn new(text: impl Into<String>, style: Style) -> Span {
Span { text: text.into(), style, link: None }
}
pub fn plain(text: impl Into<String>) -> Span {
Span::new(text, Style::new())
}
pub fn linked(text: impl Into<String>, style: Style, url: impl Into<String>) -> Span {
Span { text: text.into(), style, link: Some(url.into()) }
}
pub fn width(&self) -> usize {
str_width(&self.text)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LineKind {
Blank,
Heading,
Paragraph,
List,
Code,
Quote,
Table,
Rule,
Html,
Footnote,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HeadingLine {
pub level: u8,
pub id: String,
pub text: String,
pub summarised: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Line {
pub spans: Vec<Span>,
pub block: usize,
pub source_line: usize,
pub heading: Option<HeadingLine>,
pub scroll: bool,
pub kind: LineKind,
}
impl Line {
pub fn text(&self) -> String {
self.spans.iter().map(|s| s.text.as_str()).collect()
}
pub fn width(&self) -> usize {
self.spans.iter().map(Span::width).sum()
}
pub fn is_blank(&self) -> bool {
self.text().trim().is_empty()
}
#[cfg(test)]
pub fn link_at(&self, col: usize) -> Option<&str> {
let mut x = 0;
for s in &self.spans {
let w = s.width();
if col < x + w {
return s.link.as_deref();
}
x += w;
}
None
}
pub fn links(&self) -> Vec<(usize, &str)> {
let mut out: Vec<(usize, &str)> = Vec::new();
let mut x = 0;
for s in &self.spans {
if let Some(u) = s.link.as_deref() {
if out.last().map(|(_, p)| *p != u).unwrap_or(true) {
out.push((x, u));
}
}
x += s.width();
}
out
}
}
pub fn slice_spans(spans: &[Span], offset: usize, width: usize) -> Vec<Span> {
let end = offset.saturating_add(width);
let mut out: Vec<Span> = Vec::new();
let mut x = 0;
for s in spans {
let mut text = String::new();
for c in s.text.chars() {
let w = char_width(c);
let (cs, ce) = (x, x + w);
x = ce;
if ce <= offset || cs >= end {
continue;
}
if cs < offset || ce > end {
let overlap = ce.min(end).saturating_sub(cs.max(offset));
for _ in 0..overlap {
text.push(' ');
}
} else {
text.push(c);
}
}
if !text.is_empty() {
out.push(Span { text, style: s.style, link: s.link.clone() });
}
}
out
}
pub trait Highlighter {
fn spans(&self, lang: Option<&str>, line: &str) -> Vec<(usize, usize, Style)>;
}
pub struct NoHighlight;
impl Highlighter for NoHighlight {
fn spans(&self, _lang: Option<&str>, _line: &str) -> Vec<(usize, usize, Style)> {
Vec::new()
}
}
static NO_HIGHLIGHT: NoHighlight = NoHighlight;
#[derive(Clone, Copy)]
pub struct RenderOpts<'a> {
pub width: usize,
pub highlighter: &'a dyn Highlighter,
pub gutter: bool,
}
impl Default for RenderOpts<'_> {
fn default() -> Self {
RenderOpts { width: 80, highlighter: &NO_HIGHLIGHT, gutter: true }
}
}
impl<'a> RenderOpts<'a> {
pub fn new(width: usize) -> Self {
RenderOpts { width, ..RenderOpts::default() }
}
#[allow(dead_code)]
pub fn with_highlighter(mut self, h: &'a dyn Highlighter) -> Self {
self.highlighter = h;
self
}
pub fn content_width(&self) -> usize {
let g = if self.gutter { GUTTER_W } else { 0 };
self.width.saturating_sub(g).max(MIN_WIDTH)
}
}
pub const GUTTER_W: usize = 2;
pub const MIN_WIDTH: usize = 8;
pub fn render_document(doc: &Document, opts: &RenderOpts) -> Vec<Line> {
block::render_document(doc, opts)
}