mod blocks;
mod code;
mod emoji;
pub mod glyphs;
pub mod inline;
mod math;
mod table;
use std::collections::HashMap;
use std::io::Write;
use crate::markdown::{Document, ImageRef};
use crate::term::caps::Capabilities;
use crate::term::style::{Color, Style, StyleWriter};
use crate::theme::Theme;
pub use blocks::{heading_leaf, heading_text};
pub use code::Highlighter;
pub use glyphs::Glyphs;
pub use inline::Align;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LinkMode {
#[default]
Auto,
Hyperlink,
Inline,
Reference,
Hide,
}
#[derive(Debug, Clone)]
pub struct RenderOptions {
pub width: usize,
pub margin: usize,
pub links: LinkMode,
pub images: bool,
pub max_image_rows: u16,
pub line_numbers: bool,
pub center_figures: bool,
pub tab_width: usize,
pub glyphs: Glyphs,
pub heading_rules: bool,
}
impl Default for RenderOptions {
fn default() -> Self {
Self {
width: 80,
margin: 1,
links: LinkMode::Auto,
images: true,
max_image_rows: 24,
line_numbers: false,
center_figures: true,
tab_width: 4,
glyphs: glyphs::UNICODE,
heading_rules: true,
}
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Span {
pub text: String,
pub style: Style,
pub link: Option<usize>,
}
impl Span {
pub fn new(text: impl Into<String>, style: Style, link: Option<usize>) -> Self {
Self {
text: text.into(),
style,
link,
}
}
pub fn plain(text: impl Into<String>) -> Self {
Self {
text: text.into(),
style: Style::PLAIN,
link: None,
}
}
pub fn width(&self) -> usize {
inline::display_width(&self.text)
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Line {
pub spans: Vec<Span>,
pub image: Option<ImagePlacement>,
pub image_filler: bool,
pub anchor: Option<usize>,
pub code: Option<usize>,
}
impl Line {
pub fn from_spans(spans: Vec<Span>) -> Self {
Self {
spans: merge_spans(spans),
..Default::default()
}
}
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.image.is_none() && self.spans.iter().all(|s| s.text.trim().is_empty())
}
pub fn align(&mut self, width: usize, align: Align) {
let w = self.width();
if w >= width {
return;
}
let pad = match align {
Align::Left => return,
Align::Center => (width - w) / 2,
Align::Right => width - w,
};
if pad > 0 {
self.spans.insert(0, Span::plain(" ".repeat(pad)));
}
}
pub fn prefix(&mut self, spans: Vec<Span>) {
let added: u16 = spans.iter().map(|s| s.width() as u16).sum();
let mut new = spans;
new.append(&mut self.spans);
self.spans = merge_spans(new);
if let Some(img) = &mut self.image {
img.indent += added;
}
}
pub fn slice_columns(&self, from: usize, to: usize) -> Vec<Span> {
let mut out = Vec::new();
let mut col = 0usize;
for span in &self.spans {
let sw = span.width();
if col + sw <= from {
col += sw;
continue;
}
if col >= to {
break;
}
if col >= from && col + sw <= to {
out.push(span.clone());
col += sw;
continue;
}
let mut text = String::new();
let mut c = col;
for g in unicode_segmentation::UnicodeSegmentation::graphemes(span.text.as_str(), true)
{
let gw = inline::display_width(g).max(1);
if c + gw <= from || c >= to {
c += gw;
continue;
}
if c < from || c + gw > to {
text.push(' ');
} else {
text.push_str(g);
}
c += gw;
}
if !text.is_empty() {
out.push(Span::new(text, span.style, span.link));
}
col += sw;
}
merge_spans(out)
}
}
pub fn merge_spans(spans: Vec<Span>) -> Vec<Span> {
let mut out: Vec<Span> = Vec::with_capacity(spans.len());
for span in spans {
if span.text.is_empty() {
continue;
}
match out.last_mut() {
Some(last) if last.style == span.style && last.link == span.link => {
last.text.push_str(&span.text);
}
_ => out.push(span),
}
}
out
}
#[derive(Debug, Clone, PartialEq)]
pub struct ImagePlacement {
pub image: ImageRef,
pub cols: u16,
pub rows: u16,
pub indent: u16,
}
#[derive(Debug, Clone, PartialEq)]
pub struct HeadingEntry {
pub level: u8,
pub text: String,
pub id: String,
pub line: usize,
}
#[derive(Debug, Clone, Default)]
pub struct Screen {
pub lines: Vec<Line>,
pub links: Vec<String>,
pub code_blocks: Vec<String>,
pub headings: Vec<HeadingEntry>,
pub anchors: HashMap<String, usize>,
pub width: usize,
pub title: Option<String>,
}
impl Screen {
pub fn len(&self) -> usize {
self.lines.len()
}
pub fn is_empty(&self) -> bool {
self.lines.is_empty()
}
pub fn plain_text(&self) -> String {
let mut s = String::new();
for line in &self.lines {
s.push_str(line.text().trim_end());
s.push('\n');
}
s
}
pub fn anchor(&self, id: &str) -> Option<usize> {
self.anchors.get(id).copied()
}
}
#[derive(Debug, Clone, Copy)]
pub struct ImageRequest<'a> {
pub image: &'a ImageRef,
pub cols: u16,
pub rows: u16,
pub skip_top: u16,
pub skip_bottom: u16,
pub indent: u16,
}
impl<'a> ImageRequest<'a> {
pub fn new(image: &'a ImageRef, cols: u16, rows: u16) -> Self {
Self {
image,
cols,
rows,
skip_top: 0,
skip_bottom: 0,
indent: 0,
}
}
pub fn visible_rows(&self) -> u16 {
self.rows
.saturating_sub(self.skip_top)
.saturating_sub(self.skip_bottom)
}
}
pub trait ImageProvider {
fn measure(&mut self, image: &ImageRef) -> Option<(u32, u32)>;
fn encode(&mut self, request: ImageRequest<'_>) -> Option<String>;
fn problem(&mut self, _image: &ImageRef) -> Option<String> {
None
}
}
#[derive(Debug, Default, Clone, Copy)]
pub struct NoImages;
impl ImageProvider for NoImages {
fn measure(&mut self, _image: &ImageRef) -> Option<(u32, u32)> {
None
}
fn encode(&mut self, _request: ImageRequest<'_>) -> Option<String> {
None
}
}
pub fn render(
doc: &Document,
options: &RenderOptions,
theme: &Theme,
caps: &Capabilities,
images: &mut dyn ImageProvider,
highlighter: &Highlighter,
) -> Screen {
blocks::render_document(doc, options, theme, caps, images, highlighter)
}
#[derive(Debug, Clone)]
pub struct WriteOptions {
pub hyperlinks: bool,
pub images: bool,
}
impl Default for WriteOptions {
fn default() -> Self {
Self {
hyperlinks: true,
images: true,
}
}
}
impl Screen {
pub fn write(
&self,
out: &mut impl Write,
caps: &Capabilities,
opts: &WriteOptions,
images: &mut dyn ImageProvider,
) -> std::io::Result<()> {
let mut sw = StyleWriter::new(caps.color).with_italic_fallback(caps.italic_broken);
let mut buf = String::new();
let mut current_link: Option<usize> = None;
for line in &self.lines {
if line.image_filler {
continue;
}
if let (Some(placement), true) = (&line.image, opts.images) {
sw.reset(&mut buf);
let request = ImageRequest {
image: &placement.image,
cols: placement.cols,
rows: placement.rows,
skip_top: 0,
skip_bottom: 0,
indent: placement.indent,
};
if let Some(seq) = images.encode(request) {
for _ in 0..placement.rows {
buf.push('\n');
}
buf.push_str(&format!("\x1b[{}A", placement.rows));
buf.push_str("\x1b7"); if placement.indent > 0 {
buf.push_str(&" ".repeat(placement.indent as usize));
}
buf.push_str(&seq);
buf.push_str("\x1b8"); buf.push_str(&format!("\x1b[{}B", placement.rows));
out.write_all(buf.as_bytes())?;
buf.clear();
continue;
}
}
for span in &line.spans {
if span.link != current_link {
if current_link.is_some() {
buf.push_str("\x1b]8;;\x1b\\");
}
if let Some(i) = span.link {
if opts.hyperlinks {
if let Some(url) = self.links.get(i) {
buf.push_str(&format!("\x1b]8;;{}\x1b\\", hyperlink_uri(url)));
}
}
}
current_link = span.link;
}
sw.transition(span.style, &mut buf);
buf.push_str(&span.text);
}
if current_link.is_some() {
buf.push_str("\x1b]8;;\x1b\\");
current_link = None;
}
sw.reset(&mut buf);
buf.push('\n');
if buf.len() > 8192 {
out.write_all(buf.as_bytes())?;
buf.clear();
}
}
sw.reset(&mut buf);
out.write_all(buf.as_bytes())?;
out.flush()
}
}
pub fn hyperlink_uri(target: &str) -> std::borrow::Cow<'_, str> {
if target.split_once("://").is_some()
|| target.starts_with('#')
|| !std::path::Path::new(target).is_absolute()
{
return std::borrow::Cow::Borrowed(target);
}
let mut path = target.replace('\\', "/");
if !path.starts_with('/') {
path.insert(0, '/');
}
let mut encoded = String::with_capacity(path.len() + 8);
for ch in path.chars() {
match ch {
' ' => encoded.push_str("%20"),
'"' => encoded.push_str("%22"),
'#' => encoded.push_str("%23"),
'?' => encoded.push_str("%3F"),
'%' => encoded.push_str("%25"),
_ => encoded.push(ch),
}
}
std::borrow::Cow::Owned(format!("file://{encoded}"))
}
pub fn pad_line_background(line: &mut Line, width: usize, style: Style) {
if style.bg == Color::Default {
return;
}
let w = line.width();
if w < width {
line.spans
.push(Span::new(" ".repeat(width - w), style, None));
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn merges_adjacent_spans_with_equal_style() {
let spans = vec![
Span::plain("a"),
Span::plain("b"),
Span::new("c", Style::PLAIN.bold(), None),
];
let merged = merge_spans(spans);
assert_eq!(merged.len(), 2);
assert_eq!(merged[0].text, "ab");
}
#[test]
fn drops_empty_spans() {
assert_eq!(
merge_spans(vec![Span::plain(""), Span::plain("x")]).len(),
1
);
}
#[test]
fn slices_columns_for_horizontal_scrolling() {
let line = Line::from_spans(vec![
Span::plain("abcdef"),
Span::new("ghij", Style::PLAIN.bold(), None),
]);
let spans = line.slice_columns(4, 8);
let text: String = spans.iter().map(|s| s.text.as_str()).collect();
assert_eq!(text, "efgh");
}
#[test]
fn slicing_keeps_width_exact_across_wide_chars() {
let line = Line::from_spans(vec![Span::plain("あいう")]);
let spans = line.slice_columns(1, 4);
let text: String = spans.iter().map(|s| s.text.as_str()).collect();
assert_eq!(inline::display_width(&text), 3);
assert!(text.starts_with(' '));
}
#[test]
fn local_paths_become_file_uris_for_hyperlinks() {
assert_eq!(hyperlink_uri("https://example.com"), "https://example.com");
assert_eq!(hyperlink_uri("#anchor"), "#anchor");
assert_eq!(hyperlink_uri("relative.md"), "relative.md");
let absolute = std::path::absolute("notes.md").unwrap();
let uri = hyperlink_uri(absolute.to_str().unwrap());
assert!(uri.starts_with("file:///"), "got {uri}");
assert!(!uri.contains('\\'), "a URI uses forward slashes: {uri}");
assert!(uri.ends_with("notes.md"), "got {uri}");
}
#[cfg(unix)]
#[test]
fn file_uris_encode_awkward_characters() {
assert_eq!(hyperlink_uri("/docs/notes.md"), "file:///docs/notes.md");
assert_eq!(
hyperlink_uri("/docs/my notes.md"),
"file:///docs/my%20notes.md"
);
}
#[cfg(windows)]
#[test]
fn windows_paths_become_well_formed_file_uris() {
assert_eq!(
hyperlink_uri(r"C:\docs\notes.md"),
"file:///C:/docs/notes.md"
);
assert_eq!(
hyperlink_uri(r"C:\docs\my notes.md"),
"file:///C:/docs/my%20notes.md"
);
}
#[test]
fn alignment_pads_the_left() {
let mut line = Line::from_spans(vec![Span::plain("ab")]);
line.align(6, Align::Right);
assert_eq!(line.text(), " ab");
}
}