use super::tree::{Element, Node};
use crate::markdown;
use crate::report::{Warning, WarningKind};
use std::collections::{HashMap, HashSet};
const OPEN: char = '\u{e000}';
const CLOSE: char = '\u{e001}';
const MAX_LEVEL: usize = 6;
const EMPTY_NOTE: &str = "\u{2026}";
const CONTINUATION: &str = " ";
const BLOCKS: [&str; 12] = [
"p",
"subtitle",
"poem",
"cite",
"table",
"epigraph",
"section",
"title",
"annotation",
"empty-line",
"stanza",
"text-author",
];
const FRONT: &str = "front";
#[derive(Clone, Copy)]
enum Place {
Section(usize),
Quote,
}
fn push_plain(nodes: &[Node], out: &mut String) {
for node in nodes {
match node {
Node::Text(text) => out.push_str(text),
Node::Element(element) if element.name == "image" => {}
Node::Element(element) => push_plain(&element.children, out),
}
}
}
pub(super) fn plain(element: &Element) -> String {
let mut out = String::new();
push_plain(&element.children, &mut out);
collapse(&out).trim_matches(' ').to_owned()
}
fn collapse(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut space = false;
for character in text.chars().filter(|c| !matches!(*c, OPEN | CLOSE)) {
if character.is_ascii_whitespace() {
space = true;
} else {
if space {
out.push(' ');
space = false;
}
out.push(character);
}
}
if space {
out.push(' ');
}
out
}
fn fragment(text: &str) -> String {
let collapsed = collapse(text);
let core = collapsed.trim_matches(' ');
if core.is_empty() {
return if collapsed.is_empty() {
String::new()
} else {
" ".to_owned()
};
}
let mut out = String::with_capacity(collapsed.len() + 2);
if collapsed.starts_with(' ') {
out.push(' ');
}
out.push_str(&markdown::escape(core));
if collapsed.ends_with(' ') {
out.push(' ');
}
out
}
fn code(text: &str) -> String {
let collapsed = collapse(text);
let core = collapsed.trim_matches(' ');
if core.is_empty() {
return if collapsed.is_empty() {
String::new()
} else {
" ".to_owned()
};
}
let longest = core
.split(|c| c != '`')
.map(str::len)
.max()
.unwrap_or_default();
let fence = "`".repeat(longest + 1);
let pad = if core.starts_with('`') || core.ends_with('`') {
" "
} else {
""
};
let lead = if collapsed.starts_with(' ') { " " } else { "" };
let trail = if collapsed.ends_with(' ') { " " } else { "" };
format!("{lead}{fence}{pad}{core}{pad}{fence}{trail}")
}
fn surround(inner: &str, open: &str, close: &str) -> String {
let core = inner.trim_matches(' ');
if core.is_empty() {
return inner.to_owned();
}
let lead = &inner[..inner.len() - inner.trim_start_matches(' ').len()];
let trail = &inner[inner.trim_end_matches(' ').len()..];
format!("{lead}{open}{core}{close}{trail}")
}
fn push_piece(out: &mut String, piece: &str) {
match piece.strip_prefix(' ') {
Some(rest) if out.ends_with(' ') => out.push_str(rest),
_ => out.push_str(piece),
}
}
fn external_destination(href: &str) -> String {
let mut out = String::with_capacity(href.len());
for character in href.chars() {
match character {
' ' => out.push_str("%20"),
'"' => out.push_str("%22"),
'(' => out.push_str("%28"),
')' => out.push_str("%29"),
'<' => out.push_str("%3C"),
'>' => out.push_str("%3E"),
'[' => out.push_str("%5B"),
'\\' => out.push_str("%5C"),
']' => out.push_str("%5D"),
'`' => out.push_str("%60"),
c if c.is_control() => {}
c => out.push(c),
}
}
out
}
fn quoted(block: &str) -> String {
block
.split('\n')
.map(|line| {
if line.is_empty() {
">".to_owned()
} else {
format!("> {line}")
}
})
.collect::<Vec<_>>()
.join("\n")
}
fn local_id(href: Option<&str>) -> Option<String> {
href.and_then(|href| href.trim().strip_prefix('#'))
.map(|id| id.chars().filter(|c| !matches!(*c, OPEN | CLOSE)).collect())
}
pub(super) struct Renderer<'a> {
notes: HashMap<String, &'a Element>,
pictures: HashSet<String>,
ids: HashMap<String, String>,
marked: HashSet<String>,
pub(super) warnings: Vec<Warning>,
section: String,
warned: HashSet<&'static str>,
used_notes: Vec<String>,
in_note: bool,
quoting: usize,
hoisted: Vec<String>,
}
impl<'a> Renderer<'a> {
pub(super) fn new(notes: HashMap<String, &'a Element>, pictures: HashSet<String>) -> Self {
Self {
notes,
pictures,
ids: HashMap::new(),
marked: HashSet::new(),
warnings: Vec::new(),
section: String::new(),
warned: HashSet::new(),
used_notes: Vec::new(),
in_note: false,
quoting: 0,
hoisted: Vec::new(),
}
}
pub(super) fn render_body(&mut self, body: &Element) -> Vec<(String, String)> {
let mut texts = Vec::new();
let mut sections = 0_usize;
let mut runs = 0_usize;
let mut run: Option<(String, Vec<String>)> = None;
for node in &body.children {
match node {
Node::Element(child) if child.name == "section" => {
self.flush(&mut run, &mut texts);
sections += 1;
let name = format!("s{sections}");
self.begin(&name);
let mut out = Vec::new();
self.section(child, 1, &mut out);
let markdown = self.finish(out);
texts.push((name, markdown));
}
Node::Text(text) if text.trim().is_empty() => {}
loose => {
if run.is_none() {
let name = if sections == 0 && runs == 0 {
FRONT.to_owned()
} else {
format!("t{}", runs + 1)
};
runs += 1;
self.begin(&name);
run = Some((name, Vec::new()));
}
if let Some((_, out)) = &mut run {
self.node(loose, Place::Section(1), out);
}
}
}
}
self.flush(&mut run, &mut texts);
texts
}
fn flush(
&mut self,
run: &mut Option<(String, Vec<String>)>,
texts: &mut Vec<(String, String)>,
) {
if let Some((name, out)) = run.take() {
let markdown = self.finish(out);
texts.push((name, markdown));
}
}
pub(super) fn resolve(&self, markdown: &str) -> String {
let mut out = String::with_capacity(markdown.len());
let mut rest = markdown;
while let Some((before, after)) = rest.split_once(OPEN) {
out.push_str(before);
let (id, tail) = after.split_once(CLOSE).unwrap_or((after, ""));
let section = self.ids.get(id).map_or("", String::as_str);
out.push_str(&markdown::target(
&markdown::destination(section, Some(id)),
None,
));
rest = tail;
}
out.push_str(rest);
out
}
fn begin(&mut self, name: &str) {
name.clone_into(&mut self.section);
self.warned.clear();
self.used_notes.clear();
}
fn finish(&mut self, mut out: Vec<String>) -> String {
let used = std::mem::take(&mut self.used_notes);
for id in used {
let text = self.note_text(&id);
out.push(format!("[^{}]: {text}", markdown::anchor_name(&id)));
}
out.join("\n\n")
}
fn warn_once(&mut self, what: &'static str) {
if self.warned.insert(what) {
self.warnings.push(Warning::new(
WarningKind::MarkupDropped(what.to_owned()),
&self.section,
));
}
}
fn claim(&mut self, id: Option<&str>) -> Option<String> {
let id = id.map(str::trim).filter(|id| !id.is_empty())?;
if self.in_note || !self.marked.insert(id.to_owned()) {
return None;
}
self.ids.insert(id.to_owned(), self.section.clone());
Some(id.to_owned())
}
fn release(&mut self, id: &str) {
self.marked.remove(id);
self.ids.remove(id);
}
fn mark(&mut self, id: Option<&str>, out: &mut Vec<String>) {
let Some(id) = self.claim(id) else {
return;
};
let mark = markdown::definition(&markdown::anchor_name(&id));
if self.quoting > 0 {
self.hoisted.push(mark);
} else {
out.push(mark);
}
}
fn begin_quote(&mut self) {
self.quoting += 1;
}
fn end_quote(&mut self, inner: &[String], out: &mut Vec<String>) {
self.quoting = self.quoting.saturating_sub(1);
if self.quoting == 0 {
out.append(&mut self.hoisted);
}
if !inner.is_empty() {
out.push(quoted(&inner.join("\n\n")));
}
}
fn note_text(&mut self, id: &str) -> String {
let Some(section) = self.notes.get(id).copied() else {
return EMPTY_NOTE.to_owned();
};
let was = std::mem::replace(&mut self.in_note, true);
let mut blocks = Vec::new();
for node in §ion.children {
let skipped = matches!(
node,
Node::Element(child)
if matches!(
child.name.as_str(),
"section" | "title" | "epigraph" | "annotation"
)
);
if !skipped {
self.node(node, Place::Section(1), &mut blocks);
}
}
if blocks.is_empty()
&& let Some(title) = section.child("title")
{
let text = self.title_line(title);
if !text.is_empty() {
blocks.push(text);
}
}
self.in_note = was;
if blocks.is_empty() {
return EMPTY_NOTE.to_owned();
}
blocks
.join("\n\n")
.split('\n')
.enumerate()
.map(|(index, line)| {
if index > 0 && !line.is_empty() {
format!("{CONTINUATION}{line}")
} else {
line.to_owned()
}
})
.collect::<Vec<_>>()
.join("\n")
}
fn section(&mut self, section: &Element, depth: usize, out: &mut Vec<String>) {
let claimed = self.claim(section.attr("id"));
let start = out.len();
for node in §ion.children {
self.node(node, Place::Section(depth), out);
}
match claimed {
Some(id) if out.len() > start => {
out.insert(start, markdown::definition(&markdown::anchor_name(&id)));
}
Some(id) => self.release(&id),
None => {}
}
}
fn node(&mut self, node: &Node, place: Place, out: &mut Vec<String>) {
match node {
Node::Text(text) => {
let text = fragment(text);
let text = text.trim_matches(' ');
if !text.is_empty() {
out.push(text.to_owned());
}
}
Node::Element(element) => self.block(element, place, out),
}
}
fn block(&mut self, element: &Element, place: Place, out: &mut Vec<String>) {
match (element.name.as_str(), place) {
("p" | "text-author", _) => self.paragraph(element, out),
("poem", _) => self.poem(element, out),
("cite" | "epigraph", _) | ("annotation", Place::Section(_)) => {
self.quote(element, out);
}
("table", _) => self.table(element, out),
("image", _) => self.figure(element, out),
("empty-line", _) => {}
("subtitle", Place::Quote) => self.bold(element, out),
("subtitle", Place::Section(depth)) => {
let text = self.line(element);
if !text.is_empty() {
self.mark(element.attr("id"), out);
let level = (depth + 1).clamp(1, MAX_LEVEL);
out.push(format!("{} {text}", "#".repeat(level)));
}
}
("title", Place::Section(depth)) => self.heading(depth, element, out),
("section", Place::Section(depth)) => self.section(element, depth + 1, out),
_ => self.generic(element, place, out),
}
}
fn generic(&mut self, element: &Element, place: Place, out: &mut Vec<String>) {
if element
.elements()
.any(|child| BLOCKS.contains(&child.name.as_str()))
{
for node in &element.children {
self.node(node, place, out);
}
} else {
self.paragraph(element, out);
}
}
fn heading(&mut self, level: usize, title: &Element, out: &mut Vec<String>) {
let text = self.title_line(title);
if !text.is_empty() {
out.push(format!("{} {text}", "#".repeat(level.clamp(1, MAX_LEVEL))));
}
}
fn title_line(&mut self, title: &Element) -> String {
let mut lines = Vec::new();
for node in &title.children {
let line = match node {
Node::Element(element) if element.name == "empty-line" => String::new(),
Node::Element(element) => self.line(element),
Node::Text(text) => fragment(text).trim_matches(' ').to_owned(),
};
if !line.is_empty() {
lines.push(line);
}
}
lines.join(" ")
}
fn line(&mut self, element: &Element) -> String {
self.inline(&element.children).trim_matches(' ').to_owned()
}
fn paragraph(&mut self, paragraph: &Element, out: &mut Vec<String>) {
let text = self.line(paragraph);
if !text.is_empty() {
self.mark(paragraph.attr("id"), out);
out.push(text);
}
}
fn bold(&mut self, paragraph: &Element, out: &mut Vec<String>) {
let text = self.line(paragraph);
if !text.is_empty() {
self.mark(paragraph.attr("id"), out);
out.push(surround(&text, "**", "**"));
}
}
fn quote(&mut self, element: &Element, out: &mut Vec<String>) {
self.mark(element.attr("id"), out);
self.begin_quote();
let mut inner = Vec::new();
for node in &element.children {
self.node(node, Place::Quote, &mut inner);
}
self.end_quote(&inner, out);
}
fn poem(&mut self, poem: &Element, out: &mut Vec<String>) {
self.mark(poem.attr("id"), out);
for child in poem.elements() {
match child.name.as_str() {
"title" => self.bold_title(child, out),
"epigraph" => self.quote(child, out),
"subtitle" => self.bold(child, out),
"stanza" => self.stanza(child, out),
"date" => {
let text = plain(child);
let date = if text.is_empty() {
child.attr("value").unwrap_or_default().to_owned()
} else {
text
};
let date = fragment(&date);
if !date.trim().is_empty() {
out.push(date.trim_matches(' ').to_owned());
}
}
"empty-line" => {}
_ => self.paragraph(child, out),
}
}
}
fn bold_title(&mut self, title: &Element, out: &mut Vec<String>) {
let text = self.title_line(title);
if !text.is_empty() {
out.push(surround(&text, "**", "**"));
}
}
fn stanza(&mut self, stanza: &Element, out: &mut Vec<String>) {
let mut verses = Vec::new();
for child in stanza.elements() {
match child.name.as_str() {
"title" | "subtitle" => {
Self::flush_verses(&mut verses, out);
if child.name == "title" {
self.bold_title(child, out);
} else {
self.bold(child, out);
}
}
"empty-line" => {}
_ => {
self.mark(child.attr("id"), out);
let text = self.line(child);
if !text.is_empty() {
verses.push(text);
}
}
}
}
Self::flush_verses(&mut verses, out);
}
fn flush_verses(verses: &mut Vec<String>, out: &mut Vec<String>) {
if !verses.is_empty() {
out.push(verses.join("\\\n"));
verses.clear();
}
}
fn table(&mut self, table: &Element, out: &mut Vec<String>) {
let spans = |cell: &Element, name: &str| {
cell.attr(name)
.and_then(|span| span.trim().parse::<u32>().ok())
.is_some_and(|span| span > 1)
};
let mut merged = false;
let mut rows: Vec<Vec<String>> = Vec::new();
let mut cell_ids = Vec::new();
for row in table.named("tr") {
let mut cells = Vec::new();
for cell in row.elements() {
merged |= spans(cell, "colspan") || spans(cell, "rowspan");
cell_ids.push(cell.attr("id"));
cells.push(self.line(cell));
}
rows.push(cells);
}
let columns = rows.iter().map(Vec::len).max().unwrap_or_default();
if columns == 0 {
return;
}
if merged {
self.warn_once("table with merged cells");
}
self.mark(table.attr("id"), out);
for id in cell_ids {
self.mark(id, out);
}
let lines: Vec<String> = rows
.iter()
.map(|row| {
let cells: Vec<&str> = (0..columns)
.map(|column| row.get(column).map_or("", String::as_str))
.collect();
format!("| {} |", cells.join(" | "))
})
.collect();
let separator = format!("| {} |", vec!["---"; columns].join(" | "));
let mut table_lines = lines.into_iter();
let mut block: Vec<String> = table_lines.by_ref().take(1).collect();
block.push(separator);
block.extend(table_lines);
out.push(block.join("\n"));
}
fn figure(&mut self, image: &Element, out: &mut Vec<String>) {
self.mark(image.attr("id"), out);
let alt = image.attr("alt").unwrap_or_default();
if let Some(id) = local_id(image.attr("href")).filter(|id| self.pictures.contains(id)) {
out.push(markdown::image(
alt,
&markdown::encoded_path(&id),
image.attr("title"),
));
} else {
self.picture_lost(image.attr("href"));
let text = fragment(alt);
if !text.trim().is_empty() {
out.push(text.trim_matches(' ').to_owned());
}
}
}
fn picture_lost(&mut self, href: Option<&str>) {
let name = href.unwrap_or_default().trim().trim_start_matches('#');
self.warnings.push(Warning::new(
WarningKind::PictureDropped(name.to_owned()),
&self.section,
));
}
fn inline(&mut self, nodes: &[Node]) -> String {
let mut out = String::new();
for node in nodes {
let piece = match node {
Node::Text(text) => fragment(text),
Node::Element(element) => self.inline_element(element),
};
push_piece(&mut out, &piece);
}
out
}
fn inline_element(&mut self, element: &Element) -> String {
match element.name.as_str() {
"code" => {
let mut plain = String::new();
push_plain(&element.children, &mut plain);
code(&plain)
}
"image" => self.inline_image(element),
"a" => self.link(element),
"strong" | "emphasis" | "strikethrough" | "sub" | "sup" | "style" => {
let inner = self.inline(&element.children);
self.mark_up(&element.name, inner)
}
_ => self.inline(&element.children),
}
}
fn mark_up(&mut self, name: &str, inner: String) -> String {
match name {
"strong" => surround(&inner, "**", "**"),
"emphasis" => surround(&inner, "*", "*"),
"strikethrough" => surround(&inner, "~~", "~~"),
"sub" => {
self.warn_once("sub");
inner
}
"sup" => {
self.warn_once("sup");
inner
}
_ => {
self.warn_once("style");
inner
}
}
}
fn inline_image(&mut self, image: &Element) -> String {
let alt = image.attr("alt").unwrap_or_default();
if let Some(id) = local_id(image.attr("href")).filter(|id| self.pictures.contains(id)) {
markdown::image(alt, &markdown::encoded_path(&id), None)
} else {
self.picture_lost(image.attr("href"));
fragment(alt)
}
}
fn link(&mut self, link: &Element) -> String {
let text = self.inline(&link.children);
let href = link.attr("href").unwrap_or_default().trim();
if let Some(id) = local_id(link.attr("href")) {
let is_note = link.attr("type") == Some("note") || self.notes.contains_key(&id);
if is_note {
return self.note_reference(&id, href, &text);
}
let core = text.trim_matches(' ');
if core.is_empty() {
return String::new();
}
return surround(&text, "[", &format!("]{OPEN}{id}{CLOSE}"));
}
let lower = href.to_ascii_lowercase();
let external = ["http://", "https://", "mailto:"]
.iter()
.any(|scheme| lower.starts_with(scheme));
if external && !text.trim_matches(' ').is_empty() {
let end = markdown::target(&external_destination(href), None);
return surround(&text, "[", &format!("]{end}"));
}
if !external {
self.warnings.push(Warning::new(
WarningKind::LinkDropped(href.to_owned()),
&self.section,
));
}
text
}
fn note_reference(&mut self, id: &str, href: &str, text: &str) -> String {
if !self.notes.contains_key(id) {
self.warnings.push(Warning::new(
WarningKind::LinkDropped(href.to_owned()),
&self.section,
));
return text.to_owned();
}
if self.in_note {
return text.to_owned();
}
if !self.used_notes.iter().any(|used| used == id) {
self.used_notes.push(id.to_owned());
}
format!("[^{}]", markdown::anchor_name(id))
}
}
#[cfg(test)]
mod tests {
use super::{
code, collapse, external_destination, fragment, plain, push_piece, quoted, surround,
};
use crate::formats::fb2::tree::{Node, parse};
use mbook::Limits;
#[test]
fn white_space_runs_become_one_space() {
assert_eq!(collapse("a \n\t b\r\n"), "a b ");
assert_eq!(collapse("\u{e000}x\u{e001}"), "x");
assert_eq!(collapse(""), "");
}
#[test]
fn a_fragment_keeps_one_space_at_each_end_and_escapes_the_rest() {
assert_eq!(fragment(" *a* b\n"), " \\*a\\* b ");
assert_eq!(fragment(" \n "), " ");
assert_eq!(fragment(""), "");
}
#[test]
fn code_is_fenced_longer_than_its_backticks() {
assert_eq!(code("a"), "`a`");
assert_eq!(code("a`b"), "``a`b``");
assert_eq!(code("`a"), "`` `a ``");
assert_eq!(code(""), "");
}
#[test]
fn spaces_stay_outside_the_fence_of_code() {
assert_eq!(code(" a b\n"), " `a b` ");
assert_eq!(code(" "), " ");
}
#[test]
fn spaces_move_out_of_markup() {
assert_eq!(surround(" a ", "**", "**"), " **a** ");
assert_eq!(surround(" ", "*", "*"), " ");
}
#[test]
fn two_spaces_that_meet_are_one() {
let mut out = "a ".to_owned();
push_piece(&mut out, " b");
push_piece(&mut out, "c");
push_piece(&mut out, " d");
assert_eq!(out, "a bc d");
}
#[test]
fn an_address_loses_what_would_end_a_destination() {
assert_eq!(
external_destination("http://a.b/c d(e)\u{7}"),
"http://a.b/c%20d%28e%29"
);
assert_eq!(
external_destination("<a>[b]\\\"`"),
"%3Ca%3E%5Bb%5D%5C%22%60"
);
}
#[test]
fn a_quote_prefixes_every_line() {
assert_eq!(quoted("a\n\nb"), "> a\n>\n> b");
}
#[test]
fn the_plain_text_of_an_element_has_no_markup_and_no_pictures()
-> Result<(), crate::error::Error> {
let document = parse(
b"<p> a <strong>b\n</strong><image href=\"#x\"/><a>c</a> d </p>",
&Limits::default(),
)?;
assert_eq!(plain(&document.root), "a b c d");
assert!(matches!(document.root.children[0], Node::Text(_)));
Ok(())
}
}