use super::Node;
use crate::html::{ChapterTree, Element, serialize};
use crate::utils::{ToUrlPath, id_from_content, normalize_path, unique_id};
use mdbook_core::static_regex;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
pub(crate) fn render_print_page(mut chapter_trees: Vec<ChapterTree<'_>>) -> String {
let (id_remap, mut id_counter) = make_ids_unique(&mut chapter_trees);
let path_to_root_id = make_root_id_map(&mut chapter_trees, &mut id_counter);
rewrite_links(&mut chapter_trees, &id_remap, &path_to_root_id);
let mut print_content = String::new();
for ChapterTree { tree, .. } in chapter_trees {
if !print_content.is_empty() {
print_content
.push_str(r#"<div style="break-before: page; page-break-before: always;"></div>"#);
}
serialize(&tree, &mut print_content);
}
print_content
}
fn make_ids_unique(
chapter_trees: &mut [ChapterTree<'_>],
) -> (HashMap<PathBuf, HashMap<String, String>>, HashSet<String>) {
let mut id_remap = HashMap::new();
let mut id_counter = HashSet::new();
for ChapterTree {
html_path, tree, ..
} in chapter_trees
{
for value in tree.values_mut() {
if let Node::Element(el) = value
&& let Some(id) = el.attr("id")
{
let new_id = unique_id(id, &mut id_counter);
if new_id != id {
let id = id.to_string();
el.insert_attr("id", new_id.clone().into());
let map: &mut HashMap<_, _> = id_remap.entry(html_path.clone()).or_default();
map.insert(id, new_id);
}
}
}
}
(id_remap, id_counter)
}
fn make_root_id_map(
chapter_trees: &mut [ChapterTree<'_>],
id_counter: &mut HashSet<String>,
) -> HashMap<PathBuf, String> {
let mut path_to_root_id = HashMap::new();
for ChapterTree {
chapter,
html_path,
tree,
..
} in chapter_trees
{
let mut h1_found = false;
for value in tree.values_mut() {
if let Node::Element(el) = value {
if el.name() == "h1" {
if let Some(id) = el.attr("id") {
h1_found = true;
path_to_root_id.insert(html_path.clone(), id.to_string());
}
break;
} else if matches!(el.name(), "h2" | "h3" | "h4" | "h5" | "h6") {
break;
}
}
}
if !h1_found {
let mut h1 = Element::new("h1");
let id = id_from_content(&chapter.name);
let id = unique_id(&id, id_counter);
h1.insert_attr("id", id.clone().into());
let mut root = tree.root_mut();
let mut h1 = root.prepend(Node::Element(h1));
let mut a = Element::new("a");
a.insert_attr("href", format!("#{id}").into());
a.insert_attr("class", "header".into());
let mut a = h1.append(Node::Element(a));
a.append(Node::Text(chapter.name.clone().into()));
path_to_root_id.insert(html_path.clone(), id);
}
}
path_to_root_id
}
fn rewrite_links(
chapter_trees: &mut [ChapterTree<'_>],
id_remap: &HashMap<PathBuf, HashMap<String, String>>,
path_to_root_id: &HashMap<PathBuf, String>,
) {
static_regex!(
LINK,
r"(?x)
(?P<scheme>^[a-z][a-z0-9+.-]*:)?
(?P<path>[^\#]+)?
(?:\#(?P<anchor>.*))?"
);
for ChapterTree {
html_path, tree, ..
} in chapter_trees
{
let base = html_path.parent().expect("path can't be empty");
for value in tree.values_mut() {
let Node::Element(el) = value else {
continue;
};
if !matches!(el.name(), "a" | "img") {
continue;
}
for attr in ["href", "src", "xlink:href"] {
let Some(dest) = el.attr(attr) else {
continue;
};
let Some(caps) = LINK.captures(&dest) else {
continue;
};
if caps.name("scheme").is_some() {
continue;
}
let mut lookup_key = html_path.clone();
if let Some(href_path) = caps.name("path")
&& let href_path = href_path.as_str()
&& !href_path.is_empty()
{
lookup_key.pop();
lookup_key.push(href_path);
lookup_key = normalize_path(&lookup_key);
let is_a_chapter = path_to_root_id.contains_key(&lookup_key);
if !is_a_chapter {
let mut rel_path = normalize_path(&base.join(href_path)).to_url_path();
if let Some(anchor) = caps.name("anchor") {
rel_path.push('#');
rel_path.push_str(anchor.as_str());
}
el.insert_attr(attr, rel_path.into());
continue;
}
}
let id = match caps.name("anchor") {
Some(anchor_id) => {
let anchor_id = anchor_id.as_str().to_string();
match id_remap.get(&lookup_key) {
Some(id_map) => match id_map.get(&anchor_id) {
Some(new_id) => new_id.clone(),
None => anchor_id,
},
None => {
anchor_id
}
}
}
None => match path_to_root_id.get(&lookup_key) {
Some(id) => id.to_string(),
None => {
panic!(
"internal error: expected `{lookup_key:?}` to be in \
root map (chapter path is `{html_path:?}`)"
);
}
},
};
el.insert_attr(attr, format!("#{id}").into());
}
}
}
}