| 1 |
//! Support for generating the print page. |
| 2 |
//! |
| 3 |
//! The print page takes all the individual chapters (as `Tree<Node>` |
| 4 |
//! elements) and modifies the chapters so that they work on a consolidated |
| 5 |
//! print page, and then serializes it all as one HTML page. |
| 6 |
|
| 7 |
use super::Node; |
| 8 |
use crate::html::{ChapterTree, Element, serialize}; |
| 9 |
use crate::utils::{ToUrlPath, id_from_content, normalize_path, unique_id}; |
| 10 |
use mdbook_core::static_regex; |
| 11 |
use std::collections::{HashMap, HashSet}; |
| 12 |
use std::path::PathBuf; |
| 13 |
|
| 14 |
/// Takes all the chapter trees, modifies them to be suitable to render for |
| 15 |
/// the print page, and returns a string of all the chapters rendered to a |
| 16 |
/// single HTML page. |
| 17 |
pub(crate) fn render_print_page(mut chapter_trees: Vec<ChapterTree<'_>>) -> String { |
| 18 |
let (id_remap, mut id_counter) = make_ids_unique(&mut chapter_trees); |
| 19 |
let path_to_root_id = make_root_id_map(&mut chapter_trees, &mut id_counter); |
| 20 |
rewrite_links(&mut chapter_trees, &id_remap, &path_to_root_id); |
| 21 |
|
| 22 |
let mut print_content = String::new(); |
| 23 |
for ChapterTree { tree, .. } in chapter_trees { |
| 24 |
if !print_content.is_empty() { |
| 25 |
// Add page break between chapters |
| 26 |
// See https://developer.mozilla.org/en-US/docs/Web/CSS/break-before and https://developer.mozilla.org/en-US/docs/Web/CSS/page-break-before |
| 27 |
// Add both two CSS properties because of the compatibility issue |
| 28 |
print_content |
| 29 |
.push_str(r#"<div style="break-before: page; page-break-before: always;"></div>"#); |
| 30 |
} |
| 31 |
serialize(&tree, &mut print_content); |
| 32 |
} |
| 33 |
print_content |
| 34 |
} |
| 35 |
|
| 36 |
/// Make all IDs unique, and create a map from old to new IDs. |
| 37 |
/// |
| 38 |
/// The first map is a map of the chapter path to the IDs that were rewritten |
| 39 |
/// in that chapter (old ID to new ID). |
| 40 |
/// |
| 41 |
/// The second map is a map of every ID seen to the number of times it has |
| 42 |
/// been seen. This is used to generate unique IDs. |
| 43 |
fn make_ids_unique( |
| 44 |
chapter_trees: &mut [ChapterTree<'_>], |
| 45 |
) -> (HashMap<PathBuf, HashMap<String, String>>, HashSet<String>) { |
| 46 |
let mut id_remap = HashMap::new(); |
| 47 |
let mut id_counter = HashSet::new(); |
| 48 |
for ChapterTree { |
| 49 |
html_path, tree, .. |
| 50 |
} in chapter_trees |
| 51 |
{ |
| 52 |
for value in tree.values_mut() { |
| 53 |
if let Node::Element(el) = value |
| 54 |
&& let Some(id) = el.attr("id") |
| 55 |
{ |
| 56 |
let new_id = unique_id(id, &mut id_counter); |
| 57 |
if new_id != id { |
| 58 |
let id = id.to_string(); |
| 59 |
el.insert_attr("id", new_id.clone().into()); |
| 60 |
|
| 61 |
let map: &mut HashMap<_, _> = id_remap.entry(html_path.clone()).or_default(); |
| 62 |
map.insert(id, new_id); |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
(id_remap, id_counter) |
| 68 |
} |
| 69 |
|
| 70 |
/// Generates a map of a chapter path to the ID of the top of the chapter. |
| 71 |
/// |
| 72 |
/// If a chapter is missing an `h1` tag, then one is synthesized so that the |
| 73 |
/// print output has something to link to. |
| 74 |
fn make_root_id_map( |
| 75 |
chapter_trees: &mut [ChapterTree<'_>], |
| 76 |
id_counter: &mut HashSet<String>, |
| 77 |
) -> HashMap<PathBuf, String> { |
| 78 |
let mut path_to_root_id = HashMap::new(); |
| 79 |
for ChapterTree { |
| 80 |
chapter, |
| 81 |
html_path, |
| 82 |
tree, |
| 83 |
.. |
| 84 |
} in chapter_trees |
| 85 |
{ |
| 86 |
let mut h1_found = false; |
| 87 |
for value in tree.values_mut() { |
| 88 |
if let Node::Element(el) = value { |
| 89 |
if el.name() == "h1" { |
| 90 |
if let Some(id) = el.attr("id") { |
| 91 |
h1_found = true; |
| 92 |
path_to_root_id.insert(html_path.clone(), id.to_string()); |
| 93 |
} |
| 94 |
break; |
| 95 |
} else if matches!(el.name(), "h2" | "h3" | "h4" | "h5" | "h6") { |
| 96 |
// h1 not found. |
| 97 |
break; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
if !h1_found { |
| 102 |
// Synthesize a root id to be able to link to the start of the page. |
| 103 |
// TODO: This might want to be a warning? Chapters generally |
| 104 |
// should start with an h1. |
| 105 |
let mut h1 = Element::new("h1"); |
| 106 |
let id = id_from_content(&chapter.name); |
| 107 |
let id = unique_id(&id, id_counter); |
| 108 |
h1.insert_attr("id", id.clone().into()); |
| 109 |
let mut root = tree.root_mut(); |
| 110 |
let mut h1 = root.prepend(Node::Element(h1)); |
| 111 |
let mut a = Element::new("a"); |
| 112 |
a.insert_attr("href", format!("#{id}").into()); |
| 113 |
a.insert_attr("class", "header".into()); |
| 114 |
let mut a = h1.append(Node::Element(a)); |
| 115 |
a.append(Node::Text(chapter.name.clone().into())); |
| 116 |
path_to_root_id.insert(html_path.clone(), id); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
path_to_root_id |
| 121 |
} |
| 122 |
|
| 123 |
/// Rewrite links so that they point to IDs on the print page. |
| 124 |
fn rewrite_links( |
| 125 |
chapter_trees: &mut [ChapterTree<'_>], |
| 126 |
id_remap: &HashMap<PathBuf, HashMap<String, String>>, |
| 127 |
path_to_root_id: &HashMap<PathBuf, String>, |
| 128 |
) { |
| 129 |
static_regex!( |
| 130 |
LINK, |
| 131 |
r"(?x) |
| 132 |
(?P<scheme>^[a-z][a-z0-9+.-]*:)? |
| 133 |
(?P<path>[^\#]+)? |
| 134 |
(?:\#(?P<anchor>.*))?" |
| 135 |
); |
| 136 |
|
| 137 |
// Rewrite path links to go to the appropriate place. |
| 138 |
for ChapterTree { |
| 139 |
html_path, tree, .. |
| 140 |
} in chapter_trees |
| 141 |
{ |
| 142 |
let base = html_path.parent().expect("path can't be empty"); |
| 143 |
|
| 144 |
for value in tree.values_mut() { |
| 145 |
let Node::Element(el) = value else { |
| 146 |
continue; |
| 147 |
}; |
| 148 |
if !matches!(el.name(), "a" | "img") { |
| 149 |
continue; |
| 150 |
} |
| 151 |
for attr in ["href", "src", "xlink:href"] { |
| 152 |
let Some(dest) = el.attr(attr) else { |
| 153 |
continue; |
| 154 |
}; |
| 155 |
let Some(caps) = LINK.captures(&dest) else { |
| 156 |
continue; |
| 157 |
}; |
| 158 |
if caps.name("scheme").is_some() { |
| 159 |
continue; |
| 160 |
} |
| 161 |
// The lookup_key is the key to look up in the remap table. |
| 162 |
let mut lookup_key = html_path.clone(); |
| 163 |
if let Some(href_path) = caps.name("path") |
| 164 |
&& let href_path = href_path.as_str() |
| 165 |
&& !href_path.is_empty() |
| 166 |
{ |
| 167 |
lookup_key.pop(); |
| 168 |
lookup_key.push(href_path); |
| 169 |
lookup_key = normalize_path(&lookup_key); |
| 170 |
let is_a_chapter = path_to_root_id.contains_key(&lookup_key); |
| 171 |
if !is_a_chapter { |
| 172 |
// Make the link relative to the print page location. |
| 173 |
let mut rel_path = normalize_path(&base.join(href_path)).to_url_path(); |
| 174 |
if let Some(anchor) = caps.name("anchor") { |
| 175 |
rel_path.push('#'); |
| 176 |
rel_path.push_str(anchor.as_str()); |
| 177 |
} |
| 178 |
el.insert_attr(attr, rel_path.into()); |
| 179 |
continue; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
let id = match caps.name("anchor") { |
| 184 |
Some(anchor_id) => { |
| 185 |
let anchor_id = anchor_id.as_str().to_string(); |
| 186 |
match id_remap.get(&lookup_key) { |
| 187 |
Some(id_map) => match id_map.get(&anchor_id) { |
| 188 |
Some(new_id) => new_id.clone(), |
| 189 |
None => anchor_id, |
| 190 |
}, |
| 191 |
None => { |
| 192 |
// Assume the anchor goes to some non-remapped |
| 193 |
// ID that already exists. |
| 194 |
anchor_id |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
None => match path_to_root_id.get(&lookup_key) { |
| 199 |
Some(id) => id.to_string(), |
| 200 |
None => { |
| 201 |
// This should be guaranteed that either the |
| 202 |
// chapter itself is in the map (for anchor-only |
| 203 |
// links), or the is_a_chapter check above. |
| 204 |
panic!( |
| 205 |
"internal error: expected `{lookup_key:?}` to be in \ |
| 206 |
root map (chapter path is `{html_path:?}`)" |
| 207 |
); |
| 208 |
} |
| 209 |
}, |
| 210 |
}; |
| 211 |
el.insert_attr(attr, format!("#{id}").into()); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
} |