pub(crate) mod inline;
use super::latex::math::latex;
use super::{
CAP_LABEL, Equations, HtmlOptions, ImageStore, Mode, PageImage, code, code_run, doc_title, figure_alt, float_class,
heading_tag, is_abstract_text, is_scene_break, plain, stray_equation_number, table_walk,
};
use crate::analysis::pipeline::Analysis;
use crate::backend::PdfFile;
use crate::model::{Block, BlockKind, Document, FloatKind, ImageSpec, Span, Table, TableCell};
use inline::{Inline, escape, escape_line_start, spans_html};
use std::collections::HashSet;
const MIN_FENCE: usize = 3;
const CONTENTS_INDENT: usize = 2;
#[derive(Debug, Clone)]
pub struct MarkdownOptions {
pub inline_images: bool,
pub asset_dir: String,
pub scale: f32,
pub jobs: usize,
pub equations: Equations,
}
impl Default for MarkdownOptions {
fn default() -> Self {
Self {
inline_images: false,
asset_dir: "images".into(),
scale: 3.0,
jobs: 0,
equations: Equations::MathMl,
}
}
}
pub struct MarkdownOutput {
pub markdown: String,
pub files: Vec<(String, Vec<u8>)>,
}
pub fn render_markdown(analysis: &Analysis, pdf: &PdfFile, opts: &MarkdownOptions) -> MarkdownOutput {
let html_opts = HtmlOptions {
mode: Mode::Friendly,
inline_images: opts.inline_images,
asset_dir: opts.asset_dir.clone(),
scale: opts.scale,
page_image: PageImage::Svg,
jobs: opts.jobs,
embed_fonts: false,
equations: opts.equations,
};
let mut images = ImageStore {
opts: &html_opts,
jobs: Vec::new(),
};
let doc = &analysis.doc;
let anchors = linked_anchors(doc);
let mut writer = Writer {
opts,
inline: Inline {
anchors: &anchors,
math: opts.equations == Equations::MathMl,
},
images: &mut images,
out: String::new(),
list_indent: 0,
bracket_src: !opts.inline_images && opts.asset_dir.contains(' '),
};
writer.write(doc);
let text = writer.out.trim_end().to_string() + "\n";
let out = images.finish(pdf, text);
MarkdownOutput {
markdown: out.html,
files: out.files,
}
}
pub(crate) fn linked_anchors(doc: &Document) -> HashSet<String> {
let targets: HashSet<&str> = doc
.blocks
.iter()
.flat_map(|block| block.spans.iter().chain(block.rows.iter().flatten()))
.chain(
doc.blocks
.iter()
.filter_map(|block| block.table.as_ref())
.flat_map(|table| table.rows.iter().flatten().flat_map(|cell| cell.spans.iter())),
)
.filter_map(|span| span.href.as_deref()?.strip_prefix('#'))
.collect();
doc.blocks
.iter()
.filter_map(|block| block.anchor.as_deref())
.filter(|anchor| targets.contains(anchor))
.map(str::to_string)
.collect()
}
struct Writer<'a, 'r> {
opts: &'a MarkdownOptions,
inline: Inline<'a>,
images: &'a mut ImageStore<'r>,
out: String,
list_indent: usize,
bracket_src: bool,
}
impl Writer<'_, '_> {
fn write(&mut self, doc: &Document) {
let blocks: Vec<&Block> = doc.blocks.iter().collect();
let mut idx = 0;
let mut in_abstract = false;
let mut after_front = false;
while idx < blocks.len() {
let block = blocks[idx];
if !after_front && !matches!(block.kind, BlockKind::Title | BlockKind::Authors | BlockKind::Note) {
self.arxiv_link(doc);
after_front = true;
}
if let Some(end) = code_run(&blocks, idx) {
self.anchor(block);
self.fence(&code::listing_lines(&blocks[idx..end]));
(idx, in_abstract) = (end, false);
continue;
}
match &block.kind {
BlockKind::Title => {
idx = self.title(doc, &blocks, idx);
continue;
}
BlockKind::Paragraph { in_abstract: true } => {
let had_heading = idx > 0
&& matches!(blocks[idx - 1].kind, BlockKind::Heading { .. })
&& is_abstract_text(&blocks[idx - 1].spans);
if !in_abstract && !had_heading {
self.paragraph(&format!("{} Abstract", heading_marks(1)));
}
in_abstract = true;
}
BlockKind::Equation { .. } => {}
_ => in_abstract = false,
}
if let BlockKind::Equation { image, number: None } = &block.kind
&& let Some(number) = blocks.get(idx + 1).and_then(|next| stray_equation_number(next))
{
self.equation(block, image, Some(&number));
idx += 2;
continue;
}
self.block(block);
idx += 1;
}
if !after_front {
self.arxiv_link(doc);
}
}
fn block(&mut self, block: &Block) {
self.anchor(block);
match &block.kind {
BlockKind::Title => self.paragraph(&format!("# {}", self.inline.spans(&block.spans))),
BlockKind::Authors => self.authors(block),
BlockKind::Note => self.text_paragraph(&block.spans),
BlockKind::Heading { level } => {
let text = self.inline.spans(&block.spans);
self.paragraph(&format!("{} {text}", heading_marks(*level)));
}
BlockKind::Paragraph { .. } if is_scene_break(&block.spans) => self.paragraph("* * *"),
BlockKind::Paragraph { .. } => self.text_paragraph(&block.spans),
BlockKind::ListItem { marker } => self.list_item(block, marker.as_deref()),
BlockKind::ListContinuation => {
let text = escape_line_start(&self.inline.spans(&block.spans));
self.paragraph(&format!("{}{text}", " ".repeat(self.list_indent.max(2))));
}
BlockKind::Reference { .. } => self.text_paragraph(&block.spans),
BlockKind::Footnote { marker } if marker.is_empty() => {
let text = self.inline.spans(&block.spans);
if !text.is_empty() {
self.paragraph(&format!("> {text}"));
}
}
BlockKind::Footnote { marker } => {
let mark = format!("<sup>{}</sup> ", escape(marker));
self.paragraph(&format!("{mark}{}", self.inline.spans(&block.spans)));
}
BlockKind::Equation { image, number } => self.equation(block, image, number.as_deref()),
BlockKind::Figure { kind, image, label } => self.figure(block, *kind, image.as_ref(), label),
BlockKind::ContentsEntry { level, page } => {
let indent = " ".repeat(usize::from(level.saturating_sub(1)) * CONTENTS_INDENT);
let text = self.inline.spans(&block.spans);
let page = if page.is_empty() {
String::new()
} else {
format!(" — {}", escape(page))
};
self.line(&format!("{indent}- {text}{page}"));
}
BlockKind::Code => {
let lines: Vec<String> = block
.rows
.iter()
.map(|row| row.iter().map(|span| span.text.as_str()).collect())
.collect();
self.fence(&lines);
}
}
}
fn paragraph(&mut self, text: &str) {
self.end_tight_list();
self.out.push_str(text);
self.out.push_str("\n\n");
}
fn line(&mut self, text: &str) {
self.out.push_str(text);
self.out.push('\n');
}
fn end_tight_list(&mut self) {
if !self.out.is_empty() && !self.out.ends_with("\n\n") {
self.out.push('\n');
}
}
fn text_paragraph(&mut self, spans: &[Span]) {
let text = self.inline.spans(spans);
if !text.is_empty() {
self.paragraph(&escape_line_start(&text));
}
}
fn anchor(&mut self, block: &Block) {
if let Some(anchor) = block
.anchor
.as_deref()
.filter(|anchor| self.inline.anchors.contains(*anchor))
{
self.end_tight_list();
self.out.push_str(&format!("<a id=\"{}\"></a>\n\n", super::esc(anchor)));
}
}
fn arxiv_link(&mut self, doc: &Document) {
if let Some(id) = &doc.meta.arxiv_id {
self.paragraph(&format!("[arXiv:{}](https://arxiv.org/abs/{id})", escape(id)));
}
}
fn title(&mut self, doc: &Document, blocks: &[&Block], start: usize) -> usize {
let end = start
+ blocks[start..]
.iter()
.take_while(|block| matches!(block.kind, BlockKind::Title))
.count();
let mut spans: Vec<Span> = Vec::new();
for block in &blocks[start..end] {
self.anchor(block);
if !spans.is_empty() {
spans.push(Span::plain(" "));
}
spans.extend(block.spans.iter().cloned());
}
let text = self.inline.spans(&spans);
if text.is_empty() {
self.paragraph(&format!("# {}", escape(&doc_title(doc))));
} else {
self.paragraph(&format!("# {text}"));
}
end
}
fn authors(&mut self, block: &Block) {
let rows = if block.rows.is_empty() {
std::slice::from_ref(&block.spans)
} else {
&block.rows[..]
};
let lines: Vec<String> = rows
.iter()
.enumerate()
.map(|(i, row)| {
let text = self.inline.spans(row);
if i == 0 && !text.is_empty() && !text.contains('*') {
format!("**{text}**")
} else {
text
}
})
.filter(|text| !text.is_empty())
.collect();
if !lines.is_empty() {
self.paragraph(&escape_line_start(&lines.join(" \n")));
}
}
fn list_item(&mut self, block: &Block, marker: Option<&str>) {
let number = marker
.map(|marker| marker.trim_end_matches(['.', ')']))
.filter(|digits| !digits.is_empty() && digits.len() <= 9 && digits.bytes().all(|b| b.is_ascii_digit()));
let rest = marker.and_then(|marker| strip_label(&block.spans, marker));
let prefix = match (number, marker, &rest) {
(Some(number), _, _) => format!("{number}. "),
(None, Some(marker), Some(_)) => format!("- {} ", escape(marker)),
_ => "- ".to_string(),
};
let text = self.inline.spans(rest.as_deref().unwrap_or(&block.spans));
self.list_indent = if number.is_some() { prefix.len() } else { 2 };
self.paragraph(&format!("{prefix}{text}"));
}
fn fence(&mut self, lines: &[String]) {
let longest = lines
.iter()
.flat_map(|line| line.split(|ch| ch != '`'))
.map(str::len)
.max()
.unwrap_or(0);
let fence = "`".repeat((longest + 1).max(MIN_FENCE));
let body = lines.join("\n");
self.paragraph(&format!("{fence}\n{}\n{fence}", body.trim_end()));
}
fn equation(&mut self, block: &Block, image: &ImageSpec, number: Option<&str>) {
let tag = number.map(|number| number.trim().trim_start_matches('(').trim_end_matches(')').trim());
match &block.math {
Some(math) if self.opts.equations == Equations::MathMl => {
let tag = tag.map(|tag| format!(" \\tag{{{tag}}}")).unwrap_or_default();
self.paragraph(&format!("$$\n{}{tag}\n$$", latex(math)));
}
_ => {
let picture = self.picture(image, &plain(&block.spans), "eq");
let number = number.map(|number| format!(" {}", escape(number))).unwrap_or_default();
self.paragraph(&format!("{picture}{number}"));
}
}
}
fn figure(&mut self, block: &Block, kind: FloatKind, image: Option<&ImageSpec>, label: &Option<String>) {
let caption = (!block.spans.is_empty()).then(|| self.caption(&block.spans));
if kind == FloatKind::Algorithm
&& block.table.is_none()
&& let Some(lines) = code::float_listing_lines(block)
{
if let Some(caption) = &caption {
self.paragraph(caption);
}
return self.fence(&lines);
}
let caption_first = match (image, block.lines.first()) {
(Some(spec), Some(line)) => line.bbox.cy() < spec.rect.cy(),
_ => false,
};
if caption_first && let Some(caption) = &caption {
self.paragraph(caption);
}
if let Some(table) = &block.table {
let text = self.table(table);
self.paragraph(&text);
} else if let Some(spec) = image {
let picture = self.picture(spec, &figure_alt(block, label), float_class(kind));
self.paragraph(&picture);
}
if !caption_first && let Some(caption) = &caption {
self.paragraph(caption);
}
}
fn caption(&self, spans: &[Span]) -> String {
if let Some(first) = spans.first()
&& let Some(found) = CAP_LABEL.find(&first.text)
{
let label = first.text[..found.end()].trim_end();
let mut rest = spans.to_vec();
rest[0].text = first.text[label.len()..].to_string();
let text = self.inline.spans(&rest);
return format!("**{}** {text}", escape(label)).trim_end().to_string();
}
escape_line_start(&self.inline.spans(spans))
}
fn picture(&mut self, spec: &ImageSpec, alt: &str, hint: &str) -> String {
let src = self.images.region(spec, hint);
let alt = escape(&alt.replace(['\n', '\r'], " "));
if self.bracket_src {
format!("")
} else {
format!("")
}
}
fn table(&self, table: &Table) -> String {
let simple = table.header_rows == 1
&& table.n_cols > 0
&& table
.rows
.iter()
.flatten()
.all(|cell| cell.colspan == 1 && cell.rowspan == 1);
if simple {
self.pipe_table(table)
} else {
self.html_table(table)
}
}
fn pipe_table(&self, table: &Table) -> String {
let mut lines = Vec::new();
for (row_idx, row) in table.rows.iter().enumerate() {
let mut cells = vec![String::new(); table.n_cols];
for cell in row.iter().filter(|cell| cell.col < table.n_cols) {
cells[cell.col] = self.inline.cell(&cell.spans);
}
lines.push(format!("| {} |", cells.join(" | ")));
if row_idx == 0 {
let rule: Vec<&str> = (0..table.n_cols)
.map(|col| match table.col_align.get(col).copied() {
Some("center") => ":---:",
Some("right") => "---:",
_ => "---",
})
.collect();
lines.push(format!("| {} |", rule.join(" | ")));
}
}
lines.join("\n")
}
fn html_table(&self, table: &Table) -> String {
let mut out = String::from("<table>");
let mut open_row: Option<usize> = None;
table_walk(table, |row_idx, _, cell| {
if open_row != Some(row_idx) {
if open_row.is_some() {
out.push_str("</tr>");
}
out.push_str("\n<tr>");
open_row = Some(row_idx);
}
let tag = if row_idx < table.header_rows { "th" } else { "td" };
out.push_str(&match cell {
None => format!("<{tag}></{tag}>"),
Some(cell) => self.html_cell(cell, tag),
});
});
if open_row.is_some() {
out.push_str("</tr>");
}
out.push_str("\n</table>");
out
}
fn html_cell(&self, cell: &TableCell, tag: &str) -> String {
format!(
"<{tag}{}>{}</{tag}>",
super::span_attrs(cell),
spans_html(&cell.spans, self.inline.anchors)
)
}
}
fn heading_marks(level: u8) -> &'static str {
match heading_tag(level) {
"h2" => "##",
"h3" => "###",
"h4" => "####",
_ => "#####",
}
}
pub(crate) fn strip_label(spans: &[Span], label: &str) -> Option<Vec<Span>> {
let first = spans.first()?;
let rest = first.text.strip_prefix(label).filter(|_| !label.is_empty())?;
let mut spans = spans.to_vec();
spans[0].text = rest.trim_start().to_string();
Some(spans)
}