| 1 | //! A notebook as a static website: an index listing its section groups, sections and pages, |
| 2 | //! and each page as OneNote's view lays it out, its outlines, pictures, files and drawings |
| 3 | //! placed where they lie on the page. Text is HTML the browser wraps, written as Copy writes it, |
| 4 | //! within each outline's laid-out width. Pictures and attached files are copied beside the |
| 5 | //! pages, drawings become SVG, and nothing written runs a script or asks a server for anything. |
| 6 | |
| 7 | use crate::editor::html::{Extras, blocks, escape, font_name, linkable}; |
| 8 | use crate::editor::page::{Content, build, ink_bounds}; |
| 9 | use crate::gpu::{colorref, page::append_ink, tag_sources}; |
| 10 | use crate::layout::TextEngine; |
| 11 | use crate::outline::{OutlineLayout, ParagraphLayout, ParagraphTag, TagIcon}; |
| 12 | use draw::Primitive; |
| 13 | use onestore::{ |
| 14 | ExGuid, |
| 15 | page::{Attachment, Definition, Image, Ink, Page, PageObject, PageParagraph, ParagraphContent}, |
| 16 | }; |
| 17 | use sha2::{Digest, Sha256}; |
| 18 | use std::{ |
| 19 | collections::{BTreeMap, BTreeSet}, |
| 20 | fmt::Write as _, |
| 21 | io, |
| 22 | }; |
| 23 | |
| 24 | /// A section as the site lists it, before its pages are read. |
| 25 | pub struct Section { |
| 26 | /// The section groups holding it, outermost first. |
| 27 | pub groups: Vec<String>, |
| 28 | pub name: String, |
| 29 | /// COLORREF. |
| 30 | pub color: Option<u32>, |
| 31 | /// Its file identity, which links into it name. |
| 32 | pub identity: Option<[u8; 16]>, |
| 33 | /// Its pages in order; none for a section the site lists as locked. |
| 34 | pub pages: Option<Vec<Listing>>, |
| 35 | } |
| 36 | |
| 37 | /// A page as its section lists it. |
| 38 | pub struct Listing { |
| 39 | pub title: String, |
| 40 | /// 1 for a page, more for its subpages. |
| 41 | pub level: u32, |
| 42 | /// What links to the page name it by. |
| 43 | pub identity: Option<[u8; 16]>, |
| 44 | } |
| 45 | |
| 46 | /// Room left past the page's content, as OneNote's view leaves it. |
| 47 | const MARGIN: f32 = 36.0; |
| 48 | |
| 49 | const STYLE: &str = include_str!("../assets/site.css"); |
| 50 | |
| 51 | /// The icon of a file stored without one. |
| 52 | const BLANK_FILE: &str = "files/file.png"; |
| 53 | |
| 54 | /// A website being written: the notebook's pages, written one at a time with `page`, then the |
| 55 | /// index and stylesheet with `finish`. Every write goes through a `write` that takes |
| 56 | /// a path relative to the site's folder and the bytes to put there. |
| 57 | pub struct Site { |
| 58 | notebook: String, |
| 59 | sections: Vec<Section>, |
| 60 | /// Each section's folder and its pages' files. |
| 61 | listed: Vec<(String, Vec<Listed>)>, |
| 62 | links: Links, |
| 63 | written: BTreeSet<String>, |
| 64 | calibri: bool, |
| 65 | /// The language of the first page written that names one, the index's. |
| 66 | lang: Option<&'static str>, |
| 67 | /// Whether the site is one page alone, without the notebook around it. |
| 68 | alone: bool, |
| 69 | } |
| 70 | |
| 71 | impl Site { |
| 72 | /// The site of `sections` of `notebook`, each given a folder of its own and each page a |
| 73 | /// file, so that links between them are known before any is written. |
| 74 | pub fn new(notebook: &str, sections: Vec<Section>) -> Self { |
| 75 | Self::build(notebook, sections, false) |
| 76 | } |
| 77 | |
| 78 | /// The site of `section`'s one page alone: the page as `index.html`, without the notebook's |
| 79 | /// header and page list around it. |
| 80 | pub fn page_alone(notebook: &str, section: Section) -> Self { |
| 81 | Self::build(notebook, vec![section], true) |
| 82 | } |
| 83 | |
| 84 | fn build(notebook: &str, sections: Vec<Section>, alone: bool) -> Self { |
| 85 | let mut taken = BTreeSet::new(); |
| 86 | let mut links = Links::default(); |
| 87 | let listed = (sections.iter()) |
| 88 | .map(|section| { |
| 89 | let folder = folder(section, &mut taken); |
| 90 | let pages: Vec<Listed> = (section.pages.iter().flatten().enumerate()) |
| 91 | .map(|(at, page)| { |
| 92 | let title = shown_title(&page.title); |
| 93 | Listed { |
| 94 | href: match alone { |
| 95 | true => "index.html".to_owned(), |
| 96 | false => { |
| 97 | format!("{folder}/{:02} {}.html", at + 1, component(&title)) |
| 98 | } |
| 99 | }, |
| 100 | title, |
| 101 | level: page.level, |
| 102 | } |
| 103 | }) |
| 104 | .collect(); |
| 105 | if let (Some(identity), Some(first)) = (section.identity, pages.first()) { |
| 106 | links.sections.insert(identity, first.href.clone()); |
| 107 | } |
| 108 | for (page, listed) in section.pages.iter().flatten().zip(&pages) { |
| 109 | if let Some(identity) = page.identity { |
| 110 | links.pages.insert(identity, listed.href.clone()); |
| 111 | } |
| 112 | } |
| 113 | (folder, pages) |
| 114 | }) |
| 115 | .collect(); |
| 116 | Self { |
| 117 | notebook: notebook.to_owned(), |
| 118 | sections, |
| 119 | listed, |
| 120 | links, |
| 121 | written: BTreeSet::new(), |
| 122 | calibri: false, |
| 123 | lang: None, |
| 124 | alone, |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /// Writes `page`, the one at `at` in its section's listing at `section`. A page that can't |
| 129 | /// be laid out is written as a note saying so. |
| 130 | pub fn page( |
| 131 | &mut self, |
| 132 | [section, at]: [usize; 2], |
| 133 | page: Page, |
| 134 | engine: &mut TextEngine, |
| 135 | write: &mut dyn FnMut(&str, &[u8]) -> io::Result<()>, |
| 136 | ) -> io::Result<()> { |
| 137 | let (folder, listed) = &self.listed[section]; |
| 138 | let section = &self.sections[section]; |
| 139 | let listing = &listed[at]; |
| 140 | let root = match self.alone { |
| 141 | true => String::new(), |
| 142 | false => "../".repeat(folder.matches('/').count() + 1), |
| 143 | }; |
| 144 | let trail: String = (section.groups.iter()) |
| 145 | .map(|group| format!(" › {}", text(group))) |
| 146 | .collect(); |
| 147 | let formats = formats(&page); |
| 148 | self.calibri |= (formats.iter()).any(|format| format.font.as_deref() == Some("Calibri")); |
| 149 | let lang = (formats.iter()).find_map(|format| crate::language::tag(format.language?)); |
| 150 | self.lang = self.lang.or(lang); |
| 151 | let mut assets = Assets { |
| 152 | write, |
| 153 | written: &mut self.written, |
| 154 | }; |
| 155 | let body = match page_html(page, engine, &mut assets, &self.links, &root) { |
| 156 | Ok(body) => body?, |
| 157 | Err(error) => format!( |
| 158 | "<main class=page><p class=failed>This page couldn't be exported: {}</p></main>", |
| 159 | text(&error.to_string()) |
| 160 | ), |
| 161 | }; |
| 162 | if self.alone { |
| 163 | let head = head(&listing.title, lang, &root); |
| 164 | let html = format!("{head}{body}\n</body>\n</html>\n"); |
| 165 | return (assets.write)(&listing.href, html.as_bytes()); |
| 166 | } |
| 167 | let mut nav = String::new(); |
| 168 | page_list(listed, Some(at), &root, &mut nav); |
| 169 | let html = format!( |
| 170 | "{head}<header><a href=\"{root}index.html\">{notebook}</a>{trail} › {name}</header>\n\ |
| 171 | <div class=view>\n{body}\n<nav class=pages aria-label=Pages>\n{nav}</nav>\n</div>\n\ |
| 172 | </body>\n</html>\n", |
| 173 | head = head( |
| 174 | &format!("{} - {}", listing.title, section.name), |
| 175 | lang, |
| 176 | &root |
| 177 | ), |
| 178 | name = text(&section.name), |
| 179 | notebook = text(&self.notebook), |
| 180 | ); |
| 181 | (assets.write)(&listing.href, html.as_bytes()) |
| 182 | } |
| 183 | |
| 184 | /// Writes the index, listing every section in its groups, and the stylesheet. |
| 185 | pub fn finish(self, write: &mut dyn FnMut(&str, &[u8]) -> io::Result<()>) -> io::Result<()> { |
| 186 | let mut index = String::new(); |
| 187 | let mut groups: Vec<&String> = Vec::new(); |
| 188 | for (section, (_, pages)) in self.sections.iter().zip(&self.listed) { |
| 189 | // The index nests sections in their groups as the notebook does. |
| 190 | let kept = (groups.iter().zip(&section.groups)) |
| 191 | .take_while(|(open, group)| *open == group) |
| 192 | .count(); |
| 193 | for _ in kept..groups.len() { |
| 194 | index.push_str("</ul></li>\n"); |
| 195 | } |
| 196 | groups.truncate(kept); |
| 197 | for group in &section.groups[kept..] { |
| 198 | let level = (groups.len() + 2).min(6); |
| 199 | let _ = writeln!( |
| 200 | index, |
| 201 | "<li class=group><h{level}>{}</h{level}><ul>", |
| 202 | text(group) |
| 203 | ); |
| 204 | groups.push(group); |
| 205 | } |
| 206 | let level = (groups.len() + 2).min(6); |
| 207 | let color = section |
| 208 | .color |
| 209 | .map(|color| format!(" style='border-color:{}'", hex(color))); |
| 210 | let name = match pages.first() { |
| 211 | Some(page) => format!( |
| 212 | "<a href=\"{}\">{}</a>", |
| 213 | href(&page.href), |
| 214 | text(&section.name) |
| 215 | ), |
| 216 | None => text(&section.name), |
| 217 | }; |
| 218 | let _ = writeln!( |
| 219 | index, |
| 220 | "<li class=section><h{level} class=section{}>{name}</h{level}>", |
| 221 | color.unwrap_or_default() |
| 222 | ); |
| 223 | if section.pages.is_none() { |
| 224 | index.push_str("<p class=locked>Locked</p>\n"); |
| 225 | } |
| 226 | page_list(pages, None, "", &mut index); |
| 227 | index.push_str("</li>\n"); |
| 228 | } |
| 229 | for _ in groups { |
| 230 | index.push_str("</ul></li>\n"); |
| 231 | } |
| 232 | let index = format!( |
| 233 | "{}<main class=index>\n<h1>{}</h1>\n<ul>\n{index}</ul>\n</main>\n</body>\n</html>\n", |
| 234 | head(&self.notebook, self.lang, ""), |
| 235 | text(&self.notebook) |
| 236 | ); |
| 237 | if !self.alone { |
| 238 | write("index.html", index.as_bytes())?; |
| 239 | } |
| 240 | let mut style = String::new(); |
| 241 | // Calibri, OneNote's default, is missing from macOS and Linux; its metric-compatible |
| 242 | // stand-in keeps lines wrapping where they do on the page. |
| 243 | if self.calibri { |
| 244 | for face in crate::layout::bundled("Calibri").into_iter().flatten() { |
| 245 | use skrifa::{MetadataProvider, attribute::Style}; |
| 246 | let Ok(font) = skrifa::FontRef::new(face.as_ref()) else { |
| 247 | continue; |
| 248 | }; |
| 249 | let attributes = font.attributes(); |
| 250 | let weight = attributes.weight.value(); |
| 251 | let italic = attributes.style != Style::Normal; |
| 252 | let name = format!( |
| 253 | "fonts/Calibri-{weight}{}.ttf", |
| 254 | if italic { "-italic" } else { "" } |
| 255 | ); |
| 256 | write(&name, face.as_ref())?; |
| 257 | let _ = writeln!( |
| 258 | style, |
| 259 | "@font-face {{ font-family: Calibri; src: local(\"Calibri\"), url(\"{name}\"); \ |
| 260 | font-weight: {weight}; font-style: {}; }}", |
| 261 | if italic { "italic" } else { "normal" } |
| 262 | ); |
| 263 | } |
| 264 | } |
| 265 | style.push_str(STYLE); |
| 266 | write("style.css", style.as_bytes()) |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /// The formats of the text on `page`, title first. |
| 271 | fn formats(page: &Page) -> Vec<&onestore::document::Format> { |
| 272 | fn walk<'a>( |
| 273 | paragraphs: &'a [PageParagraph], |
| 274 | formats: &mut Vec<&'a onestore::document::Format>, |
| 275 | ) { |
| 276 | for paragraph in paragraphs { |
| 277 | match &paragraph.content { |
| 278 | ParagraphContent::Text(text) => { |
| 279 | formats.extend(text.text.spans().iter().map(|span| &span.format)) |
| 280 | } |
| 281 | ParagraphContent::Table(table) => { |
| 282 | for cell in table.rows.iter().flat_map(|row| &row.cells) { |
| 283 | walk(&cell.paragraphs, formats); |
| 284 | } |
| 285 | } |
| 286 | _ => {} |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | let mut formats = Vec::new(); |
| 291 | let titles = page.objects.iter().filter_map(|object| match object { |
| 292 | PageObject::Title(title) => Some(&title.outlines), |
| 293 | _ => None, |
| 294 | }); |
| 295 | for outline in titles.flatten() { |
| 296 | walk(&outline.paragraphs, &mut formats); |
| 297 | } |
| 298 | for object in &page.objects { |
| 299 | if let PageObject::Outline(outline) = object { |
| 300 | walk(&outline.paragraphs, &mut formats); |
| 301 | } |
| 302 | } |
| 303 | formats |
| 304 | } |
| 305 | |
| 306 | /// A document in `lang`, titled `title`, its stylesheet from `root`, to its body's start. |
| 307 | fn head(title: &str, lang: Option<&str>, root: &str) -> String { |
| 308 | let lang = lang.map(|lang| format!(" lang={lang}")).unwrap_or_default(); |
| 309 | format!( |
| 310 | "<!DOCTYPE html>\n<html{lang}>\n<head>\n<meta charset=utf-8>\n\ |
| 311 | <meta name=viewport content=\"width=device-width\">\n\ |
| 312 | <meta name=generator content=Snowbound>\n<title>{}</title>\n\ |
| 313 | <link rel=stylesheet href=\"{root}style.css\">\n</head>\n<body>\n", |
| 314 | text(title) |
| 315 | ) |
| 316 | } |
| 317 | |
| 318 | /// Where the exported files lie that links into the notebook name, by identity. |
| 319 | #[derive(Default)] |
| 320 | struct Links { |
| 321 | /// A section's first page. |
| 322 | sections: BTreeMap<[u8; 16], String>, |
| 323 | /// A page's file. |
| 324 | pages: BTreeMap<[u8; 16], String>, |
| 325 | } |
| 326 | |
| 327 | impl Links { |
| 328 | /// Where stored link `target` leads from `root`: into the export, to its file; out of the |
| 329 | /// notebook, as stored where HTML may link it. None for anything else, a link into the |
| 330 | /// notebook outside the export among them, whose address names the writer's own files. |
| 331 | fn link(&self, target: &str, root: &str) -> Option<String> { |
| 332 | self.resolve(target, root).or_else(|| { |
| 333 | linkable(target).filter(|target| { |
| 334 | !(target.get(..8)).is_some_and(|scheme| scheme.eq_ignore_ascii_case("onenote:")) |
| 335 | }) |
| 336 | }) |
| 337 | } |
| 338 | |
| 339 | /// Where `target` leads in the site, from `root`, a paragraph to its anchor; none for a |
| 340 | /// link to something outside it, or to anything but the notebook. |
| 341 | fn resolve(&self, target: &str, root: &str) -> Option<String> { |
| 342 | let link = onestore::page::link::parse_internal_link(target)?; |
| 343 | let (file, fragment) = match link.page { |
| 344 | Some(page) => ( |
| 345 | self.pages.get(&page)?, |
| 346 | link.object.map(|object| format!("#{}", anchor(object))), |
| 347 | ), |
| 348 | None => (self.sections.get(&link.section)?, None), |
| 349 | }; |
| 350 | Some(format!( |
| 351 | "{root}{}{}", |
| 352 | href(file), |
| 353 | fragment.unwrap_or_default() |
| 354 | )) |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /// The element id of paragraph `id`. |
| 359 | fn anchor(id: ExGuid) -> String { |
| 360 | let guid: String = id.guid.iter().map(|byte| format!("{byte:02x}")).collect(); |
| 361 | format!("p{guid}-{}", id.n) |
| 362 | } |
| 363 | |
| 364 | /// A page as navigation lists it. |
| 365 | struct Listed { |
| 366 | href: String, |
| 367 | title: String, |
| 368 | level: u32, |
| 369 | } |
| 370 | |
| 371 | /// `pages` as a list nested by level, `current` marked, hrefs from `root`. |
| 372 | fn page_list(pages: &[Listed], current: Option<usize>, root: &str, out: &mut String) { |
| 373 | let mut levels: Vec<u32> = Vec::new(); |
| 374 | for (at, page) in pages.iter().enumerate() { |
| 375 | while levels.last().is_some_and(|level| *level > page.level) { |
| 376 | out.push_str("</li></ul>\n"); |
| 377 | levels.pop(); |
| 378 | } |
| 379 | match levels.last() { |
| 380 | Some(level) if *level == page.level => out.push_str("</li>\n"), |
| 381 | _ => { |
| 382 | out.push_str("<ul>\n"); |
| 383 | levels.push(page.level); |
| 384 | } |
| 385 | } |
| 386 | let current = if current == Some(at) { |
| 387 | " aria-current=page" |
| 388 | } else { |
| 389 | "" |
| 390 | }; |
| 391 | let _ = write!( |
| 392 | out, |
| 393 | "<li><a href=\"{root}{}\"{current}>{}</a>", |
| 394 | href(&page.href), |
| 395 | text(&page.title) |
| 396 | ); |
| 397 | } |
| 398 | for _ in levels { |
| 399 | out.push_str("</li></ul>\n"); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /// Pictures, files, tag icons and fonts copied beside the pages, each once. |
| 404 | struct Assets<'a> { |
| 405 | write: &'a mut dyn FnMut(&str, &[u8]) -> io::Result<()>, |
| 406 | written: &'a mut BTreeSet<String>, |
| 407 | } |
| 408 | |
| 409 | impl Assets<'_> { |
| 410 | /// `bytes` at `name` under the site's folder, unless already there. |
| 411 | fn put(&mut self, name: String, bytes: &[u8]) -> io::Result<String> { |
| 412 | if !self.written.contains(&name) { |
| 413 | (self.write)(&name, bytes)?; |
| 414 | self.written.insert(name.clone()); |
| 415 | } |
| 416 | Ok(name) |
| 417 | } |
| 418 | |
| 419 | /// Where the picture lies: its stored bytes in a format browsers show, else the raster |
| 420 | /// OneNote made of them; none where it has neither. |
| 421 | fn picture(&mut self, image: &Image) -> io::Result<Option<String>> { |
| 422 | let shown = [&image.bytes, &image.display] |
| 423 | .into_iter() |
| 424 | .flatten() |
| 425 | .find_map(|bytes| Some((bytes, shown_extension(bytes)?))); |
| 426 | let Some((bytes, extension)) = shown else { |
| 427 | return Ok(None); |
| 428 | }; |
| 429 | self.put(format!("files/{}.{extension}", digest(bytes)), bytes) |
| 430 | .map(Some) |
| 431 | } |
| 432 | |
| 433 | /// Where an attached file's copy lies, under its own name, and its icon. |
| 434 | fn file(&mut self, file: &Attachment) -> io::Result<(Option<String>, String)> { |
| 435 | let copy = match &file.bytes { |
| 436 | Some(bytes) => Some(self.put( |
| 437 | format!("files/{}/{}", digest(bytes), component(&file.filename)), |
| 438 | bytes, |
| 439 | )?), |
| 440 | None => None, |
| 441 | }; |
| 442 | let preview = |
| 443 | (file.preview.as_deref()).and_then(|icon| Some((icon, shown_extension(icon)?))); |
| 444 | let icon = match preview { |
| 445 | Some((icon, extension)) => { |
| 446 | self.put(format!("files/{}.{extension}", digest(icon)), icon)? |
| 447 | } |
| 448 | None if self.written.contains(BLANK_FILE) => BLANK_FILE.to_owned(), |
| 449 | None => self.put(BLANK_FILE.to_owned(), &crate::gpu::page::file_icon())?, |
| 450 | }; |
| 451 | Ok((copy, icon)) |
| 452 | } |
| 453 | |
| 454 | /// The tag's icon, laid out as the page draws it in `[x, y]` points from where its |
| 455 | /// element starts. |
| 456 | fn tag(&mut self, tag: &ParagraphTag, [x, y]: [f32; 2], root: &str) -> io::Result<String> { |
| 457 | let checked = matches!(tag.icon, TagIcon::Symbol { checked: true, .. }); |
| 458 | let label = match (tag.label.as_str(), checked) { |
| 459 | ("", _) => "Tag".to_owned(), |
| 460 | (label, true) => format!("{label} (done)"), |
| 461 | (label, false) => label.to_owned(), |
| 462 | }; |
| 463 | let mut out = format!( |
| 464 | "<span class=tag role=img aria-label=\"{label}\" title=\"{label}\" \ |
| 465 | style='left:{x}pt;top:{y}pt;width:{side}pt;height:{side}pt'>", |
| 466 | label = text(&label), |
| 467 | side = tag.size, |
| 468 | ); |
| 469 | for source in tag_sources(tag.icon) { |
| 470 | let name = self.put( |
| 471 | format!("tags/{}.svg", digest(source.as_bytes())), |
| 472 | source.as_bytes(), |
| 473 | )?; |
| 474 | let _ = write!(out, "<img src=\"{root}{name}\" alt=\"\">"); |
| 475 | } |
| 476 | out.push_str("</span>"); |
| 477 | Ok(out) |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | /// The file extension of a picture in a format every browser shows; none for another. |
| 482 | fn shown_extension(bytes: &[u8]) -> Option<&'static str> { |
| 483 | use image::ImageFormat; |
| 484 | match image::guess_format(bytes).ok()? { |
| 485 | ImageFormat::Png => Some("png"), |
| 486 | ImageFormat::Jpeg => Some("jpg"), |
| 487 | ImageFormat::Gif => Some("gif"), |
| 488 | ImageFormat::Bmp => Some("bmp"), |
| 489 | ImageFormat::WebP => Some("webp"), |
| 490 | _ => None, |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /// The page's `<main>`: each object on the page where the page lays it out. |
| 495 | fn page_html( |
| 496 | mut page: Page, |
| 497 | engine: &mut TextEngine, |
| 498 | assets: &mut Assets, |
| 499 | links: &Links, |
| 500 | root: &str, |
| 501 | ) -> Result<io::Result<String>, crate::editor::EditorError> { |
| 502 | let objects = build(&mut page, engine, false)?.objects; |
| 503 | Ok(PageWriter { |
| 504 | assets, |
| 505 | links, |
| 506 | root, |
| 507 | definitions: &page.definitions, |
| 508 | } |
| 509 | .write(&page, &objects)) |
| 510 | } |
| 511 | |
| 512 | struct PageWriter<'a, 'b> { |
| 513 | assets: &'a mut Assets<'b>, |
| 514 | links: &'a Links, |
| 515 | root: &'a str, |
| 516 | definitions: &'a BTreeMap<ExGuid, Definition>, |
| 517 | } |
| 518 | |
| 519 | impl PageWriter<'_, '_> { |
| 520 | fn write(&mut self, page: &Page, objects: &[Content]) -> io::Result<String> { |
| 521 | let mut bounds = [f32::INFINITY, f32::INFINITY, 0.0_f32, 0.0_f32]; |
| 522 | let mut body = String::new(); |
| 523 | for object in objects { |
| 524 | let rect = match object { |
| 525 | Content::Outline { source, layout, .. } => { |
| 526 | let [x, y] = crate::origin(&source.layout); |
| 527 | let heading = if source.title { |
| 528 | " role=heading aria-level=1" |
| 529 | } else { |
| 530 | "" |
| 531 | }; |
| 532 | self.outline(&source.paragraphs, layout, [x, y], heading, &mut body)?; |
| 533 | [x, y, x + layout.size[0], y + layout.size[1]] |
| 534 | } |
| 535 | Content::Image(image) => { |
| 536 | let [x, y] = crate::origin(&image.layout); |
| 537 | let Some([width, height]) = crate::outline::image_size(image) else { |
| 538 | continue; |
| 539 | }; |
| 540 | let style = format!("left:{x}pt;top:{y}pt;width:{width}pt;height:{height}pt"); |
| 541 | body.push_str(&self.picture(image, &style)?); |
| 542 | [x, y, x + width, y + height] |
| 543 | } |
| 544 | Content::File { source, .. } => { |
| 545 | let Some((_, rect)) = object.file() else { |
| 546 | continue; |
| 547 | }; |
| 548 | let style = format!("left:{}pt;top:{}pt", rect[0], rect[1]); |
| 549 | body.push_str(&self.file(source, &style)?); |
| 550 | rect |
| 551 | } |
| 552 | Content::Ink(ink) => { |
| 553 | let Some(rect) = ink_bounds(ink) else { |
| 554 | continue; |
| 555 | }; |
| 556 | let style = format!("left:{}pt;top:{}pt;", rect[0], rect[1]); |
| 557 | body.push_str(&ink_svg(ink, &style)); |
| 558 | rect |
| 559 | } |
| 560 | Content::ReadOnly(object) => { |
| 561 | let [x0, y0, x1, y1] = object.rect(); |
| 562 | let _ = write!( |
| 563 | body, |
| 564 | "<div class=missing style='left:{x0}pt;top:{y0}pt;width:{}pt;height:{}pt'>\ |
| 565 | {}</div>", |
| 566 | x1 - x0, |
| 567 | y1 - y0, |
| 568 | text(&object.message.replace("\nRead-only", "")) |
| 569 | ); |
| 570 | [x0, y0, x1, y1] |
| 571 | } |
| 572 | Content::Date { .. } | Content::Editable(_) => continue, |
| 573 | }; |
| 574 | if let Some((_, tags, rect)) = object.tagged() { |
| 575 | for tag in crate::outline::object_tags(tags, self.definitions, rect) { |
| 576 | body.push_str(&self.assets.tag(&tag, tag.origin, self.root)?); |
| 577 | } |
| 578 | } |
| 579 | bounds = [ |
| 580 | bounds[0].min(rect[0]), |
| 581 | bounds[1].min(rect[1]), |
| 582 | bounds[2].max(rect[2]), |
| 583 | bounds[3].max(rect[3]), |
| 584 | ]; |
| 585 | } |
| 586 | // A right-to-left page lies far right of its origin, which OneNote opens scrolled to. |
| 587 | let left = if page.rtl && bounds[0].is_finite() { |
| 588 | bounds[0] - MARGIN |
| 589 | } else { |
| 590 | 0.0 |
| 591 | }; |
| 592 | let color = page |
| 593 | .color |
| 594 | .map(|color| format!(" style='background:{}'", hex(color))); |
| 595 | Ok(format!( |
| 596 | "<main class=page{}>\n<div class=sheet style='left:{}pt;width:{}pt;height:{}pt'>\n\ |
| 597 | {}{body}</div>\n</main>", |
| 598 | color.unwrap_or_default(), |
| 599 | -left, |
| 600 | bounds[2] + MARGIN, |
| 601 | bounds[3] + MARGIN, |
| 602 | rules(page) |
| 603 | )) |
| 604 | } |
| 605 | |
| 606 | fn outline( |
| 607 | &mut self, |
| 608 | paragraphs: &[PageParagraph], |
| 609 | layout: &OutlineLayout, |
| 610 | [x, y]: [f32; 2], |
| 611 | heading: &str, |
| 612 | out: &mut String, |
| 613 | ) -> io::Result<()> { |
| 614 | let mut visible: Vec<PageParagraph> = crate::outline::visible_paragraphs(paragraphs.iter()) |
| 615 | .cloned() |
| 616 | .collect(); |
| 617 | repaint(&mut visible, layout); |
| 618 | let definitions = self.definitions; |
| 619 | let column = layout.tag_column_offset(); |
| 620 | let mut extras = OutlineExtras { |
| 621 | writer: self, |
| 622 | paragraphs: (layout.paragraphs.iter()) |
| 623 | .map(|paragraph| (paragraph.id, paragraph)) |
| 624 | .collect(), |
| 625 | column, |
| 626 | failed: None, |
| 627 | }; |
| 628 | let mut html = String::new(); |
| 629 | blocks(&visible, definitions, &mut extras, &mut html); |
| 630 | if let Some(error) = extras.failed { |
| 631 | return Err(error); |
| 632 | } |
| 633 | let _ = write!( |
| 634 | out, |
| 635 | "<div class=outline{heading} style='left:{x}pt;top:{y}pt;width:{}pt'>\n{html}", |
| 636 | layout.size[0] |
| 637 | ); |
| 638 | // Text carries its own tags; those on tables and pictures lie where the page lays them. |
| 639 | for (paragraph, [tx, ty], tag) in layout.tags() { |
| 640 | if !layout.paragraphs.iter().any(|text| text.id == paragraph) { |
| 641 | out.push_str(&self.assets.tag(tag, [tx + column, ty], self.root)?); |
| 642 | } |
| 643 | } |
| 644 | out.push_str("</div>\n"); |
| 645 | Ok(()) |
| 646 | } |
| 647 | |
| 648 | /// A picture, linked where it links, at `style`. |
| 649 | fn picture(&mut self, image: &Image, style: &str) -> io::Result<String> { |
| 650 | let link = (image.link.as_deref()).and_then(|link| self.links.link(link, self.root)); |
| 651 | let (outer, inner) = match link { |
| 652 | Some(_) => (style, "width:100%;height:100%"), |
| 653 | None => ("", style), |
| 654 | }; |
| 655 | let picture = match self.assets.picture(image)? { |
| 656 | Some(name) => format!( |
| 657 | "<img class=picture src=\"{}{}\" alt=\"{}\" style='{inner}'>", |
| 658 | self.root, |
| 659 | href(&name), |
| 660 | text(image.alt.as_deref().unwrap_or_default()) |
| 661 | ), |
| 662 | None => format!("<span class=missing style='{inner}'>Picture not shown</span>"), |
| 663 | }; |
| 664 | // A link holding only a picture is named by the picture's text, else as a picture. |
| 665 | let label = match image.alt.as_deref().map(str::trim) { |
| 666 | Some("") | None => " aria-label=Picture", |
| 667 | Some(_) => "", |
| 668 | }; |
| 669 | Ok(match link { |
| 670 | Some(link) => format!( |
| 671 | "<a href=\"{}\"{label} style='{outer}'>{picture}</a>", |
| 672 | text(&link) |
| 673 | ), |
| 674 | None => picture, |
| 675 | }) |
| 676 | } |
| 677 | |
| 678 | /// A file as OneNote shows one, its icon over its name, linking to its copy. |
| 679 | fn file(&mut self, file: &Attachment, style: &str) -> io::Result<String> { |
| 680 | let (copy, icon) = self.assets.file(file)?; |
| 681 | let name = std::path::Path::new(&file.filename) |
| 682 | .file_stem() |
| 683 | .map_or(file.filename.clone(), |stem| { |
| 684 | stem.to_string_lossy().into_owned() |
| 685 | }); |
| 686 | let [width, height] = file.size.unwrap_or([24.0, 24.0]); |
| 687 | let inner = format!( |
| 688 | "<img src=\"{}{}\" alt=\"\" style='width:{width}pt;height:{height}pt'><span>{}</span>", |
| 689 | self.root, |
| 690 | href(&icon), |
| 691 | text(&name) |
| 692 | ); |
| 693 | Ok(match copy { |
| 694 | Some(copy) => format!( |
| 695 | "<a class=file href=\"{}{}\" title=\"{}\" style='{style}'>{inner}</a>", |
| 696 | self.root, |
| 697 | href(&copy), |
| 698 | text(&file.filename) |
| 699 | ), |
| 700 | None => format!("<span class=file style='{style}'>{inner}</span>"), |
| 701 | }) |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | /// What a page adds to the blocks Copy writes: text paragraphs' alignment, spacing and tags, |
| 706 | /// and the pictures, files and drawings in an outline. |
| 707 | struct OutlineExtras<'w, 'a, 'b> { |
| 708 | writer: &'w mut PageWriter<'a, 'b>, |
| 709 | paragraphs: BTreeMap<ExGuid, &'w ParagraphLayout>, |
| 710 | column: f32, |
| 711 | /// The first file that couldn't be written. |
| 712 | failed: Option<io::Error>, |
| 713 | } |
| 714 | |
| 715 | impl Extras for OutlineExtras<'_, '_, '_> { |
| 716 | fn style(&mut self, node: &PageParagraph) -> String { |
| 717 | let Some(format) = (node.text()) |
| 718 | .and_then(|text| text.text.spans().first()) |
| 719 | .map(|span| &span.format) |
| 720 | else { |
| 721 | return String::new(); |
| 722 | }; |
| 723 | let mut style = String::new(); |
| 724 | match format.alignment { |
| 725 | Some(1) => style.push_str(";text-align:center"), |
| 726 | Some(2) => style.push_str(";text-align:right"), |
| 727 | _ => {} |
| 728 | } |
| 729 | // Lines as tall as the page's, whose line boxes take Windows' font metrics. |
| 730 | if let Some(paragraph) = self.paragraphs.get(&node.id) { |
| 731 | let mut heights = paragraph.text.lines().map(|(_, line)| line.height); |
| 732 | if let Some(first) = heights.next() |
| 733 | && heights.all(|height| (height - first).abs() < 0.01) |
| 734 | { |
| 735 | let _ = write!(style, ";line-height:{first}pt"); |
| 736 | } |
| 737 | } |
| 738 | if let Some(before) = format.space_before.filter(|space| *space > 0.0) { |
| 739 | let _ = write!(style, ";margin-top:{before}pt"); |
| 740 | } |
| 741 | if let Some(after) = format.space_after.filter(|space| *space > 0.0) { |
| 742 | let _ = write!(style, ";margin-bottom:{after}pt"); |
| 743 | } |
| 744 | style |
| 745 | } |
| 746 | |
| 747 | fn attributes(&mut self, node: &PageParagraph) -> String { |
| 748 | // The number the page shows, which a list's own count would not give. |
| 749 | (self.paragraphs.get(&node.id)) |
| 750 | .and_then(|paragraph| paragraph.number.as_ref()) |
| 751 | .map(|(count, _)| format!(" value={}", count.number)) |
| 752 | .unwrap_or_default() |
| 753 | } |
| 754 | |
| 755 | fn link(&mut self, target: &str) -> Option<String> { |
| 756 | self.writer.links.link(target, self.writer.root) |
| 757 | } |
| 758 | |
| 759 | fn content(&mut self, node: &PageParagraph, out: &mut String) { |
| 760 | let _ = write!(out, "<a id={}></a>", anchor(node.id)); |
| 761 | let written = match &node.content { |
| 762 | ParagraphContent::Text(_) => { |
| 763 | let Some(paragraph) = self.paragraphs.get(&node.id) else { |
| 764 | return; |
| 765 | }; |
| 766 | // List markers as the page numbers and places them, which HTML's lists can't. |
| 767 | let mut tags = String::new(); |
| 768 | let font = (node.text()) |
| 769 | .and_then(|text| text.text.spans().first()) |
| 770 | .map(|span| &span.format); |
| 771 | let lists = node.lists.iter().rev(); |
| 772 | for ((marker, [x, y]), list) in paragraph.markers.iter().zip(lists) { |
| 773 | let size = (self.writer.definitions.get(list)) |
| 774 | .and_then(|list| list.format.font_size) |
| 775 | .or(font.and_then(|font| font.font_size)) |
| 776 | .unwrap_or(crate::layout::DEFAULT_FONT_SIZE); |
| 777 | let family = font_name( |
| 778 | (font.and_then(|font| font.font.as_deref())) |
| 779 | .unwrap_or(crate::layout::DEFAULT_FONT), |
| 780 | ) |
| 781 | .map(|family| format!("font-family:\"{family}\";")) |
| 782 | .unwrap_or_default(); |
| 783 | // The list item tells assistive technology its number or bullet. |
| 784 | let _ = write!( |
| 785 | tags, |
| 786 | "<span class=marker aria-hidden=true style='left:{}pt;top:{y}pt;{family}\ |
| 787 | font-size:{size}pt'>{}</span>", |
| 788 | x - paragraph.origin[0], |
| 789 | text(&marker.text) |
| 790 | ); |
| 791 | } |
| 792 | let mut result = Ok(()); |
| 793 | for tag in &paragraph.tags { |
| 794 | let at = [ |
| 795 | tag.origin[0] + self.column - paragraph.origin[0], |
| 796 | tag.origin[1], |
| 797 | ]; |
| 798 | match self.writer.assets.tag(tag, at, self.writer.root) { |
| 799 | Ok(tag) => tags.push_str(&tag), |
| 800 | Err(error) => result = Err(error), |
| 801 | } |
| 802 | } |
| 803 | result.map(|()| tags) |
| 804 | } |
| 805 | ParagraphContent::Image(image) => match crate::outline::image_size(image) { |
| 806 | Some([width, height]) => self |
| 807 | .writer |
| 808 | .picture(image, &format!("width:{width}pt;height:{height}pt")) |
| 809 | .map(|picture| format!("<div>{picture}</div>\n")), |
| 810 | None => Ok(String::new()), |
| 811 | }, |
| 812 | ParagraphContent::Attachment(file) => self |
| 813 | .writer |
| 814 | .file(file, "") |
| 815 | .map(|file| format!("<div>{file}</div>\n")), |
| 816 | ParagraphContent::Ink(ink) => Ok(format!("<div>{}</div>\n", ink_svg(ink, ""))), |
| 817 | ParagraphContent::Unsupported(_) => { |
| 818 | Ok("<p class=missing>Content not shown</p>\n".to_owned()) |
| 819 | } |
| 820 | ParagraphContent::Table(_) => Ok(String::new()), |
| 821 | }; |
| 822 | match written { |
| 823 | Ok(written) => out.push_str(&written), |
| 824 | Err(error) => { |
| 825 | self.failed.get_or_insert(error); |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | /// `paragraphs` with the colour and highlight their tags give each run, as the page lays |
| 832 | /// them out. |
| 833 | fn repaint(paragraphs: &mut [PageParagraph], layout: &OutlineLayout) { |
| 834 | for paragraph in paragraphs { |
| 835 | match &mut paragraph.content { |
| 836 | ParagraphContent::Text(text) => { |
| 837 | let Some(laid) = (layout.paragraphs.iter()).find(|laid| laid.id == paragraph.id) |
| 838 | else { |
| 839 | continue; |
| 840 | }; |
| 841 | let [color, highlight] = laid.tag_paint; |
| 842 | if color.is_none() && highlight.is_none() { |
| 843 | continue; |
| 844 | } |
| 845 | let mut start = 0; |
| 846 | let runs: Vec<_> = (text.text.spans().iter()) |
| 847 | .map(|span| { |
| 848 | let run = text.text.text()[start..span.end].to_owned(); |
| 849 | start = span.end; |
| 850 | let mut format = span.format.clone(); |
| 851 | format.color = color.or(format.color); |
| 852 | format.highlight = highlight.or(format.highlight); |
| 853 | (run, format) |
| 854 | }) |
| 855 | .collect(); |
| 856 | text.text = onestore::page::text::Paragraph::from_runs(runs); |
| 857 | } |
| 858 | ParagraphContent::Table(table) => { |
| 859 | for cell in table.rows.iter_mut().flat_map(|row| &mut row.cells) { |
| 860 | repaint(&mut cell.paragraphs, layout); |
| 861 | } |
| 862 | } |
| 863 | _ => {} |
| 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | /// The drawing as SVG the size of its strokes, its own `style` first, its strokes as the page |
| 869 | /// paints them. |
| 870 | fn ink_svg(ink: &Ink, style: &str) -> String { |
| 871 | let Some([x0, y0, x1, y1]) = ink_bounds(ink) else { |
| 872 | return String::new(); |
| 873 | }; |
| 874 | let mut primitives = Vec::new(); |
| 875 | append_ink( |
| 876 | ink, |
| 877 | crate::origin(&ink.layout), |
| 878 | [0.0, 0.0, 0.0, 1.0], |
| 879 | &mut primitives, |
| 880 | ); |
| 881 | let (width, height) = (x1 - x0, y1 - y0); |
| 882 | let mut svg = format!( |
| 883 | "<svg class=ink style='{style}width:{width}pt;height:{height}pt' \ |
| 884 | viewBox='{x0} {y0} {width} {height}' role=img aria-label=Drawing>" |
| 885 | ); |
| 886 | let paint = |color: [f32; 4]| { |
| 887 | let [red, green, blue] = draw::srgb_bytes(color); |
| 888 | format!("#{red:02X}{green:02X}{blue:02X}' opacity='{}", color[3]) |
| 889 | }; |
| 890 | let cap = |round: bool| if round { "round" } else { "square" }; |
| 891 | for primitive in primitives { |
| 892 | match primitive { |
| 893 | Primitive::Segment { |
| 894 | from, |
| 895 | to, |
| 896 | width, |
| 897 | round, |
| 898 | color, |
| 899 | } => { |
| 900 | let _ = write!( |
| 901 | svg, |
| 902 | "<line x1='{}' y1='{}' x2='{}' y2='{}' stroke='{}' stroke-width='{width}' \ |
| 903 | stroke-linecap='{}'/>", |
| 904 | from[0], |
| 905 | from[1], |
| 906 | to[0], |
| 907 | to[1], |
| 908 | paint(color), |
| 909 | cap(round) |
| 910 | ); |
| 911 | } |
| 912 | Primitive::Taper { |
| 913 | from, |
| 914 | to, |
| 915 | widths, |
| 916 | round, |
| 917 | color, |
| 918 | } => { |
| 919 | // A quadrilateral from one width to the other, capped at each end. |
| 920 | let [dx, dy] = [to[0] - from[0], to[1] - from[1]]; |
| 921 | let length = dx.hypot(dy).max(f32::EPSILON); |
| 922 | let [nx, ny] = [-dy / length, dx / length]; |
| 923 | let [a, b] = widths.map(|width| width / 2.0); |
| 924 | let _ = write!( |
| 925 | svg, |
| 926 | "<path d='M{} {}L{} {}L{} {}L{} {}Z' fill='{}'/>", |
| 927 | from[0] + nx * a, |
| 928 | from[1] + ny * a, |
| 929 | to[0] + nx * b, |
| 930 | to[1] + ny * b, |
| 931 | to[0] - nx * b, |
| 932 | to[1] - ny * b, |
| 933 | from[0] - nx * a, |
| 934 | from[1] - ny * a, |
| 935 | paint(color) |
| 936 | ); |
| 937 | if round { |
| 938 | for ([x, y], radius) in [(from, a), (to, b)] { |
| 939 | let _ = write!( |
| 940 | svg, |
| 941 | "<circle cx='{x}' cy='{y}' r='{radius}' fill='{}'/>", |
| 942 | paint(color) |
| 943 | ); |
| 944 | } |
| 945 | } |
| 946 | } |
| 947 | Primitive::Highlight { |
| 948 | from, |
| 949 | to, |
| 950 | width, |
| 951 | color, |
| 952 | } => { |
| 953 | let _ = write!( |
| 954 | svg, |
| 955 | "<line x1='{}' y1='{}' x2='{}' y2='{}' stroke='{}' stroke-width='{width}' \ |
| 956 | stroke-linecap='square' style='mix-blend-mode:multiply'/>", |
| 957 | from[0], |
| 958 | from[1], |
| 959 | to[0], |
| 960 | to[1], |
| 961 | paint(color) |
| 962 | ); |
| 963 | } |
| 964 | _ => {} |
| 965 | } |
| 966 | } |
| 967 | svg.push_str("</svg>"); |
| 968 | svg |
| 969 | } |
| 970 | |
| 971 | /// The page's rule lines and grid as backgrounds from its margin origin, as the page draws |
| 972 | /// them. |
| 973 | fn rules(page: &Page) -> String { |
| 974 | use crate::{HALF_INCH, MARGIN_RULE, RULE_WIDTH}; |
| 975 | use onestore::page::VerticalRule; |
| 976 | let Some(lines) = page.rule_lines else { |
| 977 | return String::new(); |
| 978 | }; |
| 979 | let [x, y] = page.margin_origin; |
| 980 | let line = |direction: &str, color: u32| { |
| 981 | format!( |
| 982 | "linear-gradient(to {direction}, {} {RULE_WIDTH}pt, transparent {RULE_WIDTH}pt)", |
| 983 | hex(color) |
| 984 | ) |
| 985 | }; |
| 986 | // Each line is centred on its place, as the page draws it. |
| 987 | let centred = -RULE_WIDTH / 2.0; |
| 988 | let mut images = vec![line("bottom", lines.color)]; |
| 989 | let mut sizes = vec![format!("100% {}pt", lines.spacing * HALF_INCH)]; |
| 990 | let mut positions = vec![format!("0 {centred}pt")]; |
| 991 | let mut repeats = vec!["repeat-y"]; |
| 992 | let mut left = 0.0; |
| 993 | match lines.vertical { |
| 994 | VerticalRule::Margin(color) => { |
| 995 | images.push(line("right", color)); |
| 996 | sizes.push("100% 100%".into()); |
| 997 | positions.push(format!("{}pt 0", x - MARGIN_RULE + centred)); |
| 998 | repeats.push("no-repeat"); |
| 999 | } |
| 1000 | VerticalRule::Grid { spacing, color } => { |
| 1001 | let spacing = spacing * HALF_INCH; |
| 1002 | // The grid repeats from the margin origin both ways. |
| 1003 | left = x - (x / spacing).ceil() * spacing; |
| 1004 | images.push(line("right", color)); |
| 1005 | sizes.push(format!("{spacing}pt 100%")); |
| 1006 | positions.push(format!("{centred}pt 0")); |
| 1007 | repeats.push("repeat-x"); |
| 1008 | } |
| 1009 | } |
| 1010 | format!( |
| 1011 | "<div class=rules style='left:{left}pt;top:{y}pt;background-image:{};\ |
| 1012 | background-size:{};background-position:{};background-repeat:{}'></div>\n", |
| 1013 | images.join(","), |
| 1014 | sizes.join(","), |
| 1015 | positions.join(","), |
| 1016 | repeats.join(",") |
| 1017 | ) |
| 1018 | } |
| 1019 | |
| 1020 | /// A page's title as navigation shows it. |
| 1021 | fn shown_title(title: &str) -> String { |
| 1022 | match title.trim() { |
| 1023 | "" => "Untitled page".to_owned(), |
| 1024 | title => title.to_owned(), |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | /// The section's folder in the site, its groups' and its own names as path components, unlike |
| 1029 | /// any folder `taken` (as `same_name` gives them), which it joins. |
| 1030 | fn folder(section: &Section, taken: &mut BTreeSet<String>) -> String { |
| 1031 | let path = (section.groups.iter().chain([&section.name])) |
| 1032 | .map(|name| component(name)) |
| 1033 | .collect::<Vec<_>>() |
| 1034 | .join("/"); |
| 1035 | let folder = (1..) |
| 1036 | .map(|number| match number { |
| 1037 | 1 => path.clone(), |
| 1038 | number => format!("{path} {number}"), |
| 1039 | }) |
| 1040 | .find(|folder| !taken.contains(&same_name(folder))) |
| 1041 | .expect("some number is free"); |
| 1042 | taken.insert(same_name(&folder)); |
| 1043 | folder |
| 1044 | } |
| 1045 | |
| 1046 | /// `name` as macOS and Windows compare names: case and Unicode composition aside. |
| 1047 | fn same_name(name: &str) -> String { |
| 1048 | icu_normalizer::ComposingNormalizerBorrowed::new_nfc() |
| 1049 | .normalize(name) |
| 1050 | .to_lowercase() |
| 1051 | } |
| 1052 | |
| 1053 | /// `name` as a file or folder name every system takes, cut short where it is long so that |
| 1054 | /// paths nested a few deep stay within Windows' 260 characters. |
| 1055 | fn component(name: &str) -> String { |
| 1056 | let name: String = name |
| 1057 | .chars() |
| 1058 | .map(|c| match c { |
| 1059 | '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' | '#' | '%' => '-', |
| 1060 | c if c.is_control() => ' ', |
| 1061 | c => c, |
| 1062 | }) |
| 1063 | .collect(); |
| 1064 | let mut end = name.len().min(48); |
| 1065 | while !name.is_char_boundary(end) { |
| 1066 | end -= 1; |
| 1067 | } |
| 1068 | let name = name[..end].trim_matches(|c: char| c.is_whitespace() || c == '.'); |
| 1069 | // Windows reserves these names for devices, with or without an extension. |
| 1070 | let stem = name |
| 1071 | .split('.') |
| 1072 | .next() |
| 1073 | .unwrap_or_default() |
| 1074 | .to_ascii_uppercase(); |
| 1075 | let reserved = matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL") |
| 1076 | || (stem.len() == 4 |
| 1077 | && (stem.starts_with("COM") || stem.starts_with("LPT")) |
| 1078 | && stem.as_bytes()[3].is_ascii_digit()); |
| 1079 | match name { |
| 1080 | "" => "Untitled".to_owned(), |
| 1081 | name if reserved => match name.split_once('.') { |
| 1082 | Some((stem, extension)) => format!("{stem}_.{extension}"), |
| 1083 | None => format!("{name}_"), |
| 1084 | }, |
| 1085 | name => name.to_owned(), |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | /// `path` as a URL path: every byte but unreserved characters and `/` percent-encoded. |
| 1090 | fn href(path: &str) -> String { |
| 1091 | let mut encoded = String::with_capacity(path.len()); |
| 1092 | for byte in path.bytes() { |
| 1093 | match byte { |
| 1094 | b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' | b'/' => { |
| 1095 | encoded.push(byte as char) |
| 1096 | } |
| 1097 | byte => { |
| 1098 | let _ = write!(encoded, "%{byte:02X}"); |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | encoded |
| 1103 | } |
| 1104 | |
| 1105 | /// Text as HTML shows it, also inside a quoted attribute. |
| 1106 | fn text(value: &str) -> String { |
| 1107 | escape(value, &mut false) |
| 1108 | } |
| 1109 | |
| 1110 | fn hex(color: u32) -> String { |
| 1111 | let [red, green, blue] = draw::srgb_bytes(colorref(color)); |
| 1112 | format!("#{red:02X}{green:02X}{blue:02X}") |
| 1113 | } |
| 1114 | |
| 1115 | fn digest(bytes: &[u8]) -> String { |
| 1116 | Sha256::digest(bytes)[..8] |
| 1117 | .iter() |
| 1118 | .map(|byte| format!("{byte:02x}")) |
| 1119 | .collect() |
| 1120 | } |
| 1121 | |
| 1122 | #[cfg(test)] |
| 1123 | mod tests { |
| 1124 | use super::*; |
| 1125 | |
| 1126 | /// A section of a corpus file, listed as the app lists it, and its pages. |
| 1127 | fn section(name: &str, groups: &[&str], bytes: &[u8]) -> (Section, Vec<Page>) { |
| 1128 | let arena = onestore::Arena::default(); |
| 1129 | let mut stored = onestore::Section::open(&arena, bytes.to_vec()).unwrap(); |
| 1130 | let (mut listings, mut pages) = (Vec::new(), Vec::new()); |
| 1131 | for (space, title, level) in stored.pages().unwrap() { |
| 1132 | let page = stored.page(space).unwrap(); |
| 1133 | listings.push(Listing { |
| 1134 | title, |
| 1135 | level, |
| 1136 | identity: page.identity, |
| 1137 | }); |
| 1138 | pages.push(page); |
| 1139 | } |
| 1140 | let section = Section { |
| 1141 | groups: groups.iter().map(|group| (*group).to_owned()).collect(), |
| 1142 | name: name.to_owned(), |
| 1143 | color: Some(0x00d6a37c), |
| 1144 | identity: Some(onestore::Header::parse(&bytes[..1024]).unwrap().file_id), |
| 1145 | pages: Some(listings), |
| 1146 | }; |
| 1147 | (section, pages) |
| 1148 | } |
| 1149 | |
| 1150 | /// The site's files, written a section at a time. |
| 1151 | fn written(sections: Vec<(Section, Vec<Page>)>) -> BTreeMap<String, Vec<u8>> { |
| 1152 | let mut files = BTreeMap::new(); |
| 1153 | let mut write = |name: &str, bytes: &[u8]| { |
| 1154 | assert!( |
| 1155 | files.insert(name.to_owned(), bytes.to_vec()).is_none(), |
| 1156 | "{name} written twice" |
| 1157 | ); |
| 1158 | Ok(()) |
| 1159 | }; |
| 1160 | let (listed, pages): (Vec<_>, Vec<_>) = sections.into_iter().unzip(); |
| 1161 | let mut site = Site::new("Notebook", listed); |
| 1162 | let mut engine = TextEngine::default(); |
| 1163 | for (section, pages) in pages.into_iter().enumerate() { |
| 1164 | for (at, page) in pages.into_iter().enumerate() { |
| 1165 | site.page([section, at], page, &mut engine, &mut write) |
| 1166 | .unwrap(); |
| 1167 | } |
| 1168 | } |
| 1169 | site.finish(&mut write).unwrap(); |
| 1170 | files |
| 1171 | } |
| 1172 | |
| 1173 | /// Every `href` and `src` of the site's pages, each with the page holding it. |
| 1174 | fn references(files: &BTreeMap<String, Vec<u8>>) -> Vec<(&str, String)> { |
| 1175 | let mut references = Vec::new(); |
| 1176 | for (name, bytes) in files.iter().filter(|(name, _)| name.ends_with(".html")) { |
| 1177 | let page = std::str::from_utf8(bytes).unwrap(); |
| 1178 | for attribute in ["href=\"", "src=\""] { |
| 1179 | for (at, _) in page.match_indices(attribute) { |
| 1180 | let value = &page[at + attribute.len()..]; |
| 1181 | let value = &value[..value.find('"').unwrap()]; |
| 1182 | references.push((name.as_str(), value.to_owned())); |
| 1183 | } |
| 1184 | } |
| 1185 | } |
| 1186 | references |
| 1187 | } |
| 1188 | |
| 1189 | /// The site's file `link` from page `name` leads to, and the anchor it names. |
| 1190 | fn resolved(name: &str, link: &str) -> (String, Option<String>) { |
| 1191 | let (path, anchor) = match link.split_once('#') { |
| 1192 | Some((path, anchor)) => (path, Some(anchor.to_owned())), |
| 1193 | None => (link, None), |
| 1194 | }; |
| 1195 | let mut file = std::path::Path::new(name).parent().unwrap().to_path_buf(); |
| 1196 | for component in percent_decoded(path).split('/') { |
| 1197 | match component { |
| 1198 | ".." => assert!(file.pop(), "{name} reaches past the site: {link}"), |
| 1199 | component => file.push(component), |
| 1200 | } |
| 1201 | } |
| 1202 | (file.to_str().unwrap().to_owned(), anchor) |
| 1203 | } |
| 1204 | |
| 1205 | /// Every page of a notebook with tagged tables, pictures, files and a recording is written, |
| 1206 | /// and everything a page or the index points at lies in the site: nothing is asked of a |
| 1207 | /// server. |
| 1208 | #[test] |
| 1209 | fn a_site_holds_everything_its_pages_point_at() { |
| 1210 | let sections = vec![ |
| 1211 | section( |
| 1212 | "Tables", |
| 1213 | &[], |
| 1214 | include_bytes!("../../../corpus/table-tags/candidate/Tables.one"), |
| 1215 | ), |
| 1216 | section( |
| 1217 | "Features", |
| 1218 | &["Group", "Inner"], |
| 1219 | include_bytes!("../../../corpus/media-edit/cold/notebook/Features.one"), |
| 1220 | ), |
| 1221 | ]; |
| 1222 | let pages: usize = sections.iter().map(|(_, pages)| pages.len()).sum(); |
| 1223 | let files = written(sections); |
| 1224 | let html = files.keys().filter(|name| name.ends_with(".html")).count(); |
| 1225 | assert_eq!(html, pages + 1); |
| 1226 | assert!(files.contains_key("Group/Inner/Features/10 Files and recording.html")); |
| 1227 | assert!(files.keys().any(|name| name.starts_with("tags/"))); |
| 1228 | assert_eq!( |
| 1229 | files.keys().any(|name| name.starts_with("fonts/")), |
| 1230 | crate::layout::bundled("Calibri").is_some() |
| 1231 | ); |
| 1232 | for (name, link) in references(&files) { |
| 1233 | if link.contains(':') { |
| 1234 | continue; |
| 1235 | } |
| 1236 | let (path, _) = resolved(name, &link); |
| 1237 | assert!(files.contains_key(&path), "{name} points at missing {path}"); |
| 1238 | } |
| 1239 | // Links may lead out of the site; nothing a page shows comes from outside it. |
| 1240 | for (name, bytes) in &files { |
| 1241 | let text = String::from_utf8_lossy(bytes); |
| 1242 | assert!( |
| 1243 | !text.contains("src=\"http") && !text.contains("url(http"), |
| 1244 | "{name}" |
| 1245 | ); |
| 1246 | } |
| 1247 | let index = String::from_utf8_lossy(&files["index.html"]); |
| 1248 | assert!(index.contains("<h2 class=section"), "{index}"); |
| 1249 | assert!(index.contains("<h2>Group</h2>") && index.contains("<h3>Inner</h3>")); |
| 1250 | assert!(index.contains("<h4 class=section"), "{index}"); |
| 1251 | let page = String::from_utf8_lossy(&files["Tables/01 Check a table.html"]); |
| 1252 | assert!(page.contains("<html lang=en>"), "{page}"); |
| 1253 | assert!(page.contains("role=heading aria-level=1"), "{page}"); |
| 1254 | } |
| 1255 | |
| 1256 | /// Links to a page and a section in the export lead to their files; links into the |
| 1257 | /// notebook outside it, or to anything but the web or mail, stay text. |
| 1258 | #[test] |
| 1259 | fn links_into_the_notebook_lead_to_the_exported_pages() { |
| 1260 | let files = written(vec![ |
| 1261 | section( |
| 1262 | "Internal", |
| 1263 | &[], |
| 1264 | include_bytes!("../../../corpus/link-edit/internal/candidate/links.one"), |
| 1265 | ), |
| 1266 | section( |
| 1267 | "Native", |
| 1268 | &["Group"], |
| 1269 | include_bytes!("../../../corpus/link-edit/native-links/notebook/links.one"), |
| 1270 | ), |
| 1271 | ]); |
| 1272 | let mut rewritten = 0; |
| 1273 | for (name, link) in references(&files) { |
| 1274 | assert!(!link.starts_with("onenote:"), "{name} keeps {link}"); |
| 1275 | if link.contains(':') || name == "index.html" { |
| 1276 | continue; |
| 1277 | } |
| 1278 | let (path, _) = resolved(name, &link); |
| 1279 | assert!(files.contains_key(&path), "{name} points at missing {path}"); |
| 1280 | rewritten += 1; |
| 1281 | } |
| 1282 | assert!(rewritten > 0); |
| 1283 | // The page, paragraph and section links of the native page, beside its page list. |
| 1284 | let native = |
| 1285 | String::from_utf8_lossy(&files["Group/Native/01 Read about Rust the Rust site.html"]); |
| 1286 | for (link, count) in [ |
| 1287 | ("02%20Link%20target.html\"", 2), |
| 1288 | ("02%20Link%20target.html#p", 1), |
| 1289 | ("01%20Read%20about%20Rust%20the%20Rust%20site.html\"", 2), |
| 1290 | ] { |
| 1291 | assert_eq!(native.matches(link).count(), count, "{link} in {native}"); |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | /// A link to a paragraph leads to its anchor on the exported page. |
| 1296 | #[test] |
| 1297 | fn a_paragraph_link_leads_to_its_anchor() { |
| 1298 | use onestore::page::link::{LinkTarget, internal_link}; |
| 1299 | let (section, pages) = section( |
| 1300 | "Internal", |
| 1301 | &[], |
| 1302 | include_bytes!("../../../corpus/link-edit/internal/candidate/links.one"), |
| 1303 | ); |
| 1304 | let paragraph = pages[1] |
| 1305 | .objects |
| 1306 | .iter() |
| 1307 | .find_map(|object| match object { |
| 1308 | PageObject::Outline(outline) => outline.paragraphs.first(), |
| 1309 | PageObject::Title(title) => title.outlines.first()?.paragraphs.first(), |
| 1310 | _ => None, |
| 1311 | }) |
| 1312 | .unwrap() |
| 1313 | .id; |
| 1314 | let target = LinkTarget::Object { |
| 1315 | identity: pages[1].identity.unwrap(), |
| 1316 | title: "", |
| 1317 | object: paragraph, |
| 1318 | }; |
| 1319 | let link = internal_link(section.identity.unwrap(), "", target); |
| 1320 | let site = Site::new("Notebook", vec![section]); |
| 1321 | let resolved = site.links.link(&link, "../").unwrap(); |
| 1322 | assert_eq!( |
| 1323 | resolved, |
| 1324 | format!("../Internal/02%20Link%20target.html#{}", anchor(paragraph)) |
| 1325 | ); |
| 1326 | let files = written(vec![self::section( |
| 1327 | "Internal", |
| 1328 | &[], |
| 1329 | include_bytes!("../../../corpus/link-edit/internal/candidate/links.one"), |
| 1330 | )]); |
| 1331 | let target = String::from_utf8_lossy(&files["Internal/02 Link target.html"]); |
| 1332 | assert!(target.contains(&format!("<a id={}>", anchor(paragraph)))); |
| 1333 | } |
| 1334 | |
| 1335 | /// A locked section is listed with its name and colour and a note, and has no pages. |
| 1336 | #[test] |
| 1337 | fn a_locked_section_is_listed_as_locked() { |
| 1338 | let files = written(vec![( |
| 1339 | Section { |
| 1340 | groups: Vec::new(), |
| 1341 | name: "Diary".into(), |
| 1342 | color: Some(0x000000ff), |
| 1343 | identity: None, |
| 1344 | pages: None, |
| 1345 | }, |
| 1346 | Vec::new(), |
| 1347 | )]); |
| 1348 | let index = String::from_utf8(files["index.html"].clone()).unwrap(); |
| 1349 | assert!( |
| 1350 | index.contains("<h2 class=section style='border-color:#FF0000'>Diary</h2>"), |
| 1351 | "{index}" |
| 1352 | ); |
| 1353 | assert!(index.contains("<p class=locked>Locked</p>")); |
| 1354 | assert_eq!( |
| 1355 | files.keys().filter(|name| name.ends_with(".html")).count(), |
| 1356 | 1 |
| 1357 | ); |
| 1358 | } |
| 1359 | |
| 1360 | /// Fonts, link targets, titles and names stored on a page can't put script, handlers or |
| 1361 | /// outside requests into its file. |
| 1362 | #[test] |
| 1363 | fn hostile_strings_in_a_page_stay_inert() { |
| 1364 | let (mut listed, mut pages) = section( |
| 1365 | "Sec'tion\" <b>", |
| 1366 | &["Gro'up"], |
| 1367 | include_bytes!("../../../corpus/media-edit/cold/notebook/Features.one"), |
| 1368 | ); |
| 1369 | let hostile = [ |
| 1370 | "Evil' tabindex=1 autofocus onfocus='alert(1)//", |
| 1371 | "A B\";background-image:url(https://evil.example/px.png);x:\"", |
| 1372 | ]; |
| 1373 | let link = |target: &str| { |
| 1374 | let code = onestore::document::Format { |
| 1375 | hyperlink: Some(true), |
| 1376 | hidden: Some(true), |
| 1377 | ..Default::default() |
| 1378 | }; |
| 1379 | let label = onestore::document::Format { |
| 1380 | hyperlink: Some(true), |
| 1381 | hyperlink_label: Some(true), |
| 1382 | ..Default::default() |
| 1383 | }; |
| 1384 | [ |
| 1385 | (format!("\u{fddf}HYPERLINK \"{target}\""), code), |
| 1386 | ("label".to_owned(), label), |
| 1387 | ] |
| 1388 | }; |
| 1389 | fn poison( |
| 1390 | paragraphs: &mut [PageParagraph], |
| 1391 | font: &str, |
| 1392 | runs: &[(String, onestore::document::Format)], |
| 1393 | ) { |
| 1394 | for paragraph in paragraphs { |
| 1395 | if let ParagraphContent::Text(text) = &mut paragraph.content { |
| 1396 | let mut all: Vec<_> = (text.text.spans().iter()) |
| 1397 | .scan(0, |start, span| { |
| 1398 | let run = text.text.text()[*start..span.end].to_owned(); |
| 1399 | *start = span.end; |
| 1400 | let mut format = span.format.clone(); |
| 1401 | format.font = Some(font.to_owned()); |
| 1402 | Some((run, format)) |
| 1403 | }) |
| 1404 | .collect(); |
| 1405 | all.extend(runs.iter().cloned()); |
| 1406 | text.text = onestore::page::text::Paragraph::from_runs(all); |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | let runs: Vec<_> = [ |
| 1411 | "javascript:alert(document.domain)", |
| 1412 | "data:text/html,x", |
| 1413 | "file:///etc/passwd", |
| 1414 | ] |
| 1415 | .into_iter() |
| 1416 | .flat_map(link) |
| 1417 | .collect(); |
| 1418 | for (at, page) in pages.iter_mut().enumerate() { |
| 1419 | for object in &mut page.objects { |
| 1420 | match object { |
| 1421 | PageObject::Outline(outline) => { |
| 1422 | poison(&mut outline.paragraphs, hostile[at % 2], &runs) |
| 1423 | } |
| 1424 | PageObject::Image(image) => image.link = Some("javascript:alert(2)".into()), |
| 1425 | _ => {} |
| 1426 | } |
| 1427 | } |
| 1428 | } |
| 1429 | for listing in listed.pages.iter_mut().flatten() { |
| 1430 | listing.title = "it's \"<script>alert(3)</script>".into(); |
| 1431 | } |
| 1432 | listed.name = "Sec'tion\" <b>".into(); |
| 1433 | let files = written(vec![(listed, pages)]); |
| 1434 | for (name, bytes) in &files { |
| 1435 | if !name.ends_with(".html") { |
| 1436 | continue; |
| 1437 | } |
| 1438 | let html = String::from_utf8_lossy(bytes); |
| 1439 | for bad in [ |
| 1440 | "onfocus", |
| 1441 | "autofocus", |
| 1442 | "url(", |
| 1443 | "javascript", |
| 1444 | "data:text", |
| 1445 | "file:", |
| 1446 | "<script", |
| 1447 | "<b>", |
| 1448 | "Evil", |
| 1449 | "evil.example", |
| 1450 | ] { |
| 1451 | assert!(!html.contains(bad), "{bad} in {name}"); |
| 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | /// Sections whose names a file system would take for the same folder, or refuse, get |
| 1457 | /// folders of their own, and deep groups stay short. |
| 1458 | #[test] |
| 1459 | fn sections_get_folders_of_their_own() { |
| 1460 | let named = |groups: &[&str], name: &str| Section { |
| 1461 | groups: groups.iter().map(|group| (*group).to_owned()).collect(), |
| 1462 | name: name.to_owned(), |
| 1463 | color: None, |
| 1464 | identity: None, |
| 1465 | pages: Some(vec![Listing { |
| 1466 | title: "Page".into(), |
| 1467 | level: 1, |
| 1468 | identity: None, |
| 1469 | }]), |
| 1470 | }; |
| 1471 | let long = "A rather long group name that goes on for quite a while"; |
| 1472 | let site = Site::new( |
| 1473 | "Notebook", |
| 1474 | vec![ |
| 1475 | named(&[], "Plans #1"), |
| 1476 | named(&[], "plans %1"), |
| 1477 | named(&[], "CON"), |
| 1478 | named(&[], "com1.txt"), |
| 1479 | named(&[], " . . "), |
| 1480 | named(&[], "Cafe\u{301}"), |
| 1481 | named(&[], "Caf\u{e9}"), |
| 1482 | named(&[long, long, long, long], long), |
| 1483 | ], |
| 1484 | ); |
| 1485 | let folders: Vec<&str> = site |
| 1486 | .listed |
| 1487 | .iter() |
| 1488 | .map(|(folder, _)| folder.as_str()) |
| 1489 | .collect(); |
| 1490 | assert_eq!( |
| 1491 | folders[..5], |
| 1492 | ["Plans -1", "plans -1 2", "CON_", "com1_.txt", "Untitled"] |
| 1493 | ); |
| 1494 | assert_eq!(folders[6], "Caf\u{e9} 2"); |
| 1495 | assert!(folders[7].len() <= 5 * 49, "{}", folders[7]); |
| 1496 | } |
| 1497 | |
| 1498 | /// A page alone is the page as `index.html`, with no header or page list, its pictures, |
| 1499 | /// files and fonts beside it. |
| 1500 | #[test] |
| 1501 | fn a_page_alone_is_the_page_and_nothing_else() { |
| 1502 | let (mut section, mut pages) = section( |
| 1503 | "Features", |
| 1504 | &["Group"], |
| 1505 | include_bytes!("../../../corpus/media-edit/cold/notebook/Features.one"), |
| 1506 | ); |
| 1507 | let page = pages.remove(0); |
| 1508 | section.pages.as_mut().unwrap().truncate(1); |
| 1509 | let mut files = BTreeMap::new(); |
| 1510 | let mut write = |name: &str, bytes: &[u8]| { |
| 1511 | files.insert(name.to_owned(), bytes.to_vec()); |
| 1512 | Ok(()) |
| 1513 | }; |
| 1514 | let mut site = Site::page_alone("Notebook", section); |
| 1515 | site.page([0, 0], page, &mut TextEngine::default(), &mut write) |
| 1516 | .unwrap(); |
| 1517 | site.finish(&mut write).unwrap(); |
| 1518 | let html = String::from_utf8_lossy(&files["index.html"]); |
| 1519 | assert!( |
| 1520 | !html.contains("<header>") && !html.contains("<nav"), |
| 1521 | "{html}" |
| 1522 | ); |
| 1523 | assert!(html.contains("<main class=page"), "{html}"); |
| 1524 | assert!(html.contains("href=\"style.css\""), "{html}"); |
| 1525 | assert!(files.keys().any(|name| name.starts_with("files/"))); |
| 1526 | assert!( |
| 1527 | files |
| 1528 | .keys() |
| 1529 | .all(|name| !name.ends_with(".html") || name == "index.html") |
| 1530 | ); |
| 1531 | for (name, link) in references(&files) { |
| 1532 | if !link.contains(':') { |
| 1533 | let (path, _) = resolved(name, &link); |
| 1534 | assert!(files.contains_key(&path), "{name} points at missing {path}"); |
| 1535 | } |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | fn percent_decoded(link: &str) -> String { |
| 1540 | let bytes = link.as_bytes(); |
| 1541 | let mut decoded = Vec::new(); |
| 1542 | let mut at = 0; |
| 1543 | while at < bytes.len() { |
| 1544 | if bytes[at] == b'%' { |
| 1545 | decoded.push(u8::from_str_radix(&link[at + 1..at + 3], 16).unwrap()); |
| 1546 | at += 3; |
| 1547 | } else { |
| 1548 | decoded.push(bytes[at]); |
| 1549 | at += 1; |
| 1550 | } |
| 1551 | } |
| 1552 | String::from_utf8(decoded).unwrap() |
| 1553 | } |
| 1554 | } |