kakemono 0.19.0

Convert PDF, Word and EPUB documents and web pages to HTML, Markdown, EPUB, LaTeX and JSON in Rust
Documentation
//! Styled text runs ([`Span`]) as inline Markdown: emphasis, code, links,
//! `<sup>`/`<sub>` for raised and lowered text (Markdown has no syntax of
//! its own for them), and the formulas inside the text as LaTeX (`$…$`).

use crate::model::{BOLD, ITALIC, MONO, SUB, SUP, Span};
use crate::render::esc;
use crate::render::inline_math::{Piece, has_math, split};
use crate::render::latex::math::latex;
use crate::render::notes::{Segment, segments};
use crate::render::runs::trim_breaks;
use std::collections::HashSet;

/// Characters escaped with a backslash anywhere in running text: those that
/// start emphasis, code, links, inline HTML, table cells and (for the
/// Markdown readers that typeset TeX) inline math.
const ESCAPED: &[char] = &['\\', '`', '*', '_', '[', ']', '<', '>', '|', '$'];

/// Link schemes written as Markdown links; other targets stay text.
const LINK_SCHEMES: [&str; 4] = ["http://", "https://", "mailto:", "ftp://"];

/// The style bits that change how a span is written in Markdown (colour,
/// font family and the like are dropped).
const MARKDOWN_STYLE: u8 = BOLD | ITALIC | MONO | SUP | SUB;

/// Writes spans as inline Markdown.
pub(crate) struct Inline<'a> {
    /// The anchors of the document that are written (as `<a id>`): a link
    /// to any other anchor stays text.
    pub(crate) anchors: &'a HashSet<String>,
    /// Write the formulas inside the text as LaTeX (see
    /// [`inline_math`](crate::render::inline_math)).
    pub(crate) math: bool,
}

impl Inline<'_> {
    /// The spans as one line of inline Markdown, with the formulas inside
    /// them as `$…$` when [`Inline::math`].
    pub(crate) fn spans(&self, spans: &[Span]) -> String {
        self.write(spans, false)
    }

    /// The spans of a cell of a pipe table: as [`Inline::spans`], with the
    /// bars of the formulas written as commands, since a bar ends the cell.
    pub(crate) fn cell(&self, spans: &[Span]) -> String {
        self.write(spans, true)
    }

    /// Writes spans, with formulas when wanted; `in_table` writes the bars of
    /// formulas as `\vert`.
    fn write(&self, spans: &[Span], in_table: bool) -> String {
        if !self.math || !has_math(spans) {
            return trim_breaks(&self.text(spans), LINE_BREAK).to_string();
        }
        let mut out = String::new();
        for piece in split(spans) {
            match piece {
                Piece::Text(spans) => out.push_str(&self.text(&spans)),
                Piece::Math(node, _, _) => {
                    let mut tex = latex(&node);
                    if in_table {
                        tex = tex.replace("\\|", "\\Vert ").replace('|', "\\vert ");
                    }
                    out.push_str(&format!("${}$", tex.trim()));
                }
            }
        }
        trim_breaks(&out, LINE_BREAK).to_string()
    }

    /// Text spans as inline Markdown (not trimmed), the call of a numbered
    /// added note as a footnote reference (`[^n1]`).
    fn text(&self, spans: &[Span]) -> String {
        let mut out = String::new();
        for segment in segments(spans) {
            match segment {
                Segment::Text(spans) => out.push_str(&self.runs(spans)),
                Segment::Note(call) if !call.text.is_empty() => out.push_str(&format!("[^{}]", note_label(call))),
                Segment::Note(_) => {}
            }
        }
        out
    }

    /// Text spans without notes as inline Markdown (not trimmed).
    /// Neighbouring spans of the same style and link are joined first, so
    /// that emphasis is not closed and opened again inside a word.
    fn runs(&self, spans: &[Span]) -> String {
        let mut out = String::new();
        for (text, style, href) in merge_runs(spans, true) {
            // Each line is styled on its own, so that no emphasis is left
            // open across a line break.
            let body = text
                .split('\n')
                .map(|line| styled(line, style))
                .collect::<Vec<_>>()
                .join(LINE_BREAK);
            match href.as_deref().filter(|href| self.is_link(href)) {
                Some(href) if !body.trim().is_empty() => {
                    // The spaces around the text stay outside the link.
                    let lead = &body[..body.len() - body.trim_start().len()];
                    let trail = &body[body.trim_end().len()..];
                    out.push_str(&format!("{lead}[{}]({}){trail}", body.trim(), destination(href)));
                }
                _ => out.push_str(&body),
            }
        }
        out
    }

    /// Whether `href` is written as a link: an anchor of the document that
    /// is written, or a web or mail address.
    fn is_link(&self, href: &str) -> bool {
        match href.strip_prefix('#') {
            Some(anchor) => self.anchors.contains(anchor),
            None => LINK_SCHEMES.iter().any(|scheme| href.starts_with(scheme)),
        }
    }
}

/// The label of the footnote of a numbered added note (`n1`).
pub(crate) fn note_label(call: &Span) -> String {
    format!("n{}", call.text)
}

/// A line break of the text (the lines of a poem, of an address): HTML,
/// which CommonMark, GitHub, Pandoc and the other readers read inside a
/// paragraph, a heading and a table cell alike.
const LINE_BREAK: &str = "<br>";

/// Neighbouring spans with the same Markdown style and link, joined:
/// (text, style, link).
/// Line breaks (`\n`) stay in the text when `keep_breaks`, for a format
/// that sets them; they are spaces otherwise.
pub(crate) fn merge_runs(spans: &[Span], keep_breaks: bool) -> Vec<(String, u8, Option<String>)> {
    let mut runs: Vec<(String, u8, Option<String>)> = Vec::new();
    for span in spans {
        let style = span.style & MARKDOWN_STYLE;
        let text = if keep_breaks {
            span.text.replace(['\r', '\t'], " ")
        } else {
            span.text.replace(['\n', '\r', '\t'], " ")
        };
        match runs.last_mut() {
            Some((last, last_style, last_href)) if *last_style == style && *last_href == span.href => {
                last.push_str(&text);
            }
            _ => runs.push((text, style, span.href.clone())),
        }
    }
    runs
}

/// The text of one run with its style. The spaces at its ends go outside
/// the emphasis and code marks, where Markdown expects them.
fn styled(text: &str, style: u8) -> String {
    let inner = text.trim_matches(' ');
    if inner.is_empty() {
        return text.to_string();
    }
    let lead = &text[..text.len() - text.trim_start_matches(' ').len()];
    let trail = &text[text.trim_end_matches(' ').len()..];
    let mut body = if style & MONO != 0 {
        code_span(inner)
    } else if style & (BOLD | ITALIC) != 0 {
        emphasis(inner, style)
    } else {
        escape(inner)
    };
    if style & SUP != 0 {
        body = format!("<sup>{body}</sup>");
    }
    if style & SUB != 0 {
        body = format!("<sub>{body}</sub>");
    }
    format!("{lead}{body}{trail}")
}

/// Bold or italic text: the punctuation at its ends stays outside the
/// marks, where CommonMark expects it ("*Bottom row*:", not "*Bottom
/// row:*" which does not close before a letter); punctuation alone is not
/// emphasised.
fn emphasis(text: &str, style: u8) -> String {
    let core_start = text.find(char::is_alphanumeric);
    let core_end = text
        .rfind(char::is_alphanumeric)
        .map(|idx| idx + text[idx..].chars().next().map_or(1, char::len_utf8));
    let (Some(start), Some(end)) = (core_start, core_end) else {
        return escape(text);
    };
    let mut core = escape(&text[start..end]);
    if style & ITALIC != 0 {
        core = format!("*{core}*");
    }
    if style & BOLD != 0 {
        core = format!("**{core}**");
    }
    format!("{}{core}{}", escape(&text[..start]), escape(&text[end..]))
}

/// `text` as inline code, between enough backticks that none inside closes
/// it.
fn code_span(text: &str) -> String {
    let longest = text.split(|ch| ch != '`').map(str::len).max().unwrap_or(0);
    let fence = "`".repeat(longest + 1);
    // A space separates the fence from a backtick at either end.
    let pad = if text.starts_with('`') || text.ends_with('`') {
        " "
    } else {
        ""
    };
    format!("{fence}{pad}{text}{pad}{fence}")
}

/// Escapes the characters of running text that Markdown would read as
/// syntax (see [`ESCAPED`]).
pub(crate) fn escape(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    for ch in text.chars() {
        if ESCAPED.contains(&ch) {
            out.push('\\');
        }
        out.push(ch);
    }
    out
}

/// Escapes what would start a block at the beginning of a line of text: a
/// heading (`#`), a list item (`-`, `+`, `1.`), a quote (`>`, escaped
/// already), a rule or a setext underline (`=`).
pub(crate) fn escape_line_start(line: &str) -> String {
    if line.starts_with(['#', '-', '+', '=']) {
        return format!("\\{line}");
    }
    let digits = line.len() - line.trim_start_matches(|ch: char| ch.is_ascii_digit()).len();
    let rest = &line[digits..];
    if digits > 0 && (rest.starts_with(". ") || rest.starts_with(") ") || rest == "." || rest == ")") {
        return format!("{}\\{}", &line[..digits], rest);
    }
    line.to_string()
}

/// A link destination: in angle brackets when it holds spaces or
/// parentheses, which would end a bare one.
fn destination(href: &str) -> String {
    if href.contains([' ', '(', ')', '<', '>']) {
        format!("<{}>", href.replace('<', "%3C").replace('>', "%3E").replace(' ', "%20"))
    } else {
        href.to_string()
    }
}

/// Spans as HTML for the cells of a table written as HTML (Markdown is not
/// read inside them by every reader). The call of a numbered added
/// note is its raised number: footnote references are not read in HTML.
pub(crate) fn spans_html(spans: &[Span], anchors: &HashSet<String>) -> String {
    let mut out = String::new();
    for segment in segments(spans) {
        match segment {
            Segment::Text(spans) => out.push_str(&runs_html(spans, anchors)),
            Segment::Note(call) if !call.text.is_empty() => out.push_str(&format!("<sup>{}</sup>", esc(&call.text))),
            Segment::Note(_) => {}
        }
    }
    trim_breaks(&out, LINE_BREAK).to_string()
}

/// Spans without notes as HTML (see [`spans_html`]), not trimmed.
fn runs_html(spans: &[Span], anchors: &HashSet<String>) -> String {
    let mut out = String::new();
    for (text, style, href) in merge_runs(spans, true) {
        let inner = text.trim_matches(' ');
        let lead = &text[..text.len() - text.trim_start_matches(' ').len()];
        let trail = if inner.is_empty() {
            ""
        } else {
            &text[text.trim_end_matches(' ').len()..]
        };
        let mut body = esc(if inner.is_empty() { &text } else { inner }).replace('\n', LINE_BREAK);
        for (bit, tag) in [
            (MONO, "code"),
            (ITALIC, "em"),
            (BOLD, "strong"),
            (SUP, "sup"),
            (SUB, "sub"),
        ] {
            if style & bit != 0 {
                body = format!("<{tag}>{body}</{tag}>");
            }
        }
        let inline = Inline { anchors, math: false };
        let body = match href.filter(|href| inline.is_link(href)) {
            Some(href) => format!("<a href=\"{}\">{body}</a>", esc(&href)),
            None => body,
        };
        out.push_str(&format!("{lead}{body}{trail}"));
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    fn span(text: &str, style: u8) -> Span {
        Span {
            style,
            ..Span::plain(text)
        }
    }

    #[test]
    fn emphasis_keeps_spaces_outside() {
        let anchors = HashSet::new();
        let inline = Inline {
            anchors: &anchors,
            math: false,
        };
        let spans = [span("a ", 0), span("bold ", BOLD), span("x_1 * y", 0)];
        assert_eq!(inline.spans(&spans), "a **bold** x\\_1 \\* y");
        let spans = [span("Bottom row:", ITALIC), span("A", 0)];
        assert_eq!(inline.spans(&spans), "*Bottom row*:A");
    }

    #[test]
    fn links_only_to_known_anchors_and_web() {
        let anchors: HashSet<String> = ["ref-1".to_string()].into();
        let inline = Inline {
            anchors: &anchors,
            math: false,
        };
        let mut known = span("[1]", 0);
        known.href = Some("#ref-1".into());
        let mut unknown = span(" [2]", 0);
        unknown.href = Some("#ref-2".into());
        let mut web = span(" site", 0);
        web.href = Some("https://example.org/a b".into());
        assert_eq!(
            inline.spans(&[known, unknown, web]),
            "[\\[1\\]](#ref-1) \\[2\\] [site](<https://example.org/a%20b>)"
        );
    }

    #[test]
    fn code_with_backticks() {
        assert_eq!(code_span("a`b"), "``a`b``");
        assert_eq!(code_span("`x"), "`` `x ``");
    }

    #[test]
    fn line_start_is_not_a_block() {
        assert_eq!(escape_line_start("# not a heading"), "\\# not a heading");
        assert_eq!(escape_line_start("2019. A year"), "2019\\. A year");
        assert_eq!(escape_line_start("2019 was"), "2019 was");
    }
}