tikz-gallery-generator
Custum build of stapix for tikz.pablopie.xyz
| Name | Size | Mode |
| .. | ||
| src/main.rs | 16K | -rw-r--r-- |
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
use image::{DynamicImage, ImageReader}; use std::{ cmp, env, fmt::{self, Display}, fs::{self, File}, io::{self, Write}, path::{Path, PathBuf}, process::{ExitCode, Command}, sync::mpsc, time::Instant, }; use gallery_entry::{GalleryEntry, FileFormat, LicenseType}; use threadpool::ThreadPool; use escape::Escaped; #[macro_use] mod log; mod escape; mod gallery_entry; /// A wrapper for displaying the path for the thumbnail of a given path pub struct ThumbPath<'a>(pub &'a GalleryEntry); /// A wrapper for displaying file names with ".html" appended at the end pub struct HtmlFileName<'a>(pub &'a str); const FULL_BUILD_OPT: &str = "-B"; const N_THREADS_OPT: &str = "-j"; const BOTH_OPTS: &str = "-Bj"; const TARGET_PATH: &str = "./site"; const PAGES_PATH: &str = "figures"; const IMAGES_PATH: &str = "assets/images"; const THUMBS_PATH: &str = "assets/thumbs"; const STYLES_PATH: &str = "assets/styles.css"; const PAGE_TITLE: &str = "TikZ gallery"; fn main() -> ExitCode { let mut args = env::args(); let program = args .next() .expect("args always contains at least the input program"); log::version(&program); let config = match args.next() { Some(config) => config, None => { errorln!("Expected 1 command line argument, found none"); log::usage(&program); return ExitCode::FAILURE; } }; let mut full_build = false; let total_cores = num_cpus::get(); let mut num_cores = total_cores - 1; while let Some(arg) = args.next() { let mut is_valid_arg = false; if arg == FULL_BUILD_OPT || arg == BOTH_OPTS { full_build = true; is_valid_arg = true; } if arg == N_THREADS_OPT || arg == BOTH_OPTS { is_valid_arg = true; let val = match args.next() { Some(val) => val, None => { errorln!("Expected one more argument, got none"); log::usage(&program); return ExitCode::FAILURE; } }; match val.parse() { Ok(val) => num_cores = val, Err(_) => { errorln!("Expected a number, got {val:?}"); log::usage(&program); return ExitCode::FAILURE; } } } if !is_valid_arg { if arg.starts_with("-") { errorln!("Unknown option: {arg:?}"); } else { errorln!("Unknown argument: {arg:?}"); } log::usage(&program); return ExitCode::FAILURE; } } let f = File::open(&config); match f.map(serde_yaml::from_reader::<_, Vec<GalleryEntry>>) { Err(err) => { errorln!("Couldn't open {config:?}: {err}"); log::usage(&program); return ExitCode::FAILURE; } Ok(Err(err)) => { errorln!("Couldn't parse {config:?}: {err}"); log::usage_config(); return ExitCode::FAILURE; } Ok(Ok(pics)) => if render_gallery(pics, full_build, num_cores, total_cores).is_err() { return ExitCode::FAILURE; }, } ExitCode::SUCCESS } /// Coordinates the rendering of all the pages and file conversions fn render_gallery( pics: Vec<GalleryEntry>, full_build: bool, num_cores: usize, total_cores: usize, ) -> Result<(), ()> { struct Job { pic_id: usize, image_path: PathBuf, thumb_path: PathBuf, page_path: PathBuf, } let start = Instant::now(); let mut skipped = 0; let mut jobs = Vec::with_capacity(pics.len()); for (pic_id, pic) in pics.iter().enumerate() { let mut image_path = PathBuf::from(TARGET_PATH); image_path.push(IMAGES_PATH); image_path.push(&pic.file_name); let thumb_path: PathBuf = ThumbPath(pic).into(); let mut page_path = PathBuf::from(TARGET_PATH); page_path.push(PAGES_PATH); page_path.push(format!("{}", HtmlFileName(&pic.file_name))); if pic.alt.is_empty() { warnln!( "Empty text alternative was specified for the file {name:?}", name = pic.file_name ); } if full_build || needs_update(pic, &image_path) || needs_update(pic, &thumb_path) || needs_update(pic, &page_path) { jobs.push(Job { pic_id, image_path, thumb_path, page_path, }); } else { skipped += 1; } } // ======================================================================== if !jobs.is_empty() { infoln!("Rendering HTML files..."); } render_index(&pics).map_err(|_| ())?; log::job_finished("index.html"); if jobs.is_empty() { warnln!( "Skipping all {total} entries: no update is required. Use {FULL_BUILD_OPT} to overwrite", total = pics.len(), ); log::finished(start.elapsed()); return Ok(()); } for Job { pic_id, page_path, .. } in &jobs { let pic = &pics[*pic_id]; render_pic_page(pic, page_path).map_err(|_| ())?; log::job_finished(&HtmlFileName(&pic.file_name)); } // ======================================================================== for Job { pic_id, image_path, .. } in &jobs { let pic = &pics[*pic_id]; copy(&pic.path, image_path)?; } infoln!("Copied image files to the target directory"); // ======================================================================== let num_cores = cmp::min(num_cores, jobs.len()); // NOTE: only spawn the threads if necessary if num_cores > 1 { infoln!("Rendering thumbnails... (using {num_cores}/{total_cores} cores)"); let rendering_pool = ThreadPool::with_name( String::from("thumbnails renderer"), num_cores, ); let (sender, reciever) = mpsc::channel(); for Job { pic_id, thumb_path, .. } in &jobs { let pic_id = *pic_id; let thumb_path = thumb_path.clone(); let pic = pics[pic_id].clone(); let sender = sender.clone(); rendering_pool.execute(move || { // NOTE: we need to send the picture id back so that the main thread // knows how to log the fact we finished rendering it let _ = sender.send( render_thumbnail(&pic, &thumb_path).map(|()| pic_id) ); }); } for _ in 0..jobs.len() { let msg = reciever.recv(); // propagate the panic to the main thread: reciever.recv should // only fail if some of the rendering threads panicked if msg.is_err() { panic!("rendering thread panicked!"); } let pic_id = msg.unwrap()?; let pic = &pics[pic_id]; log::job_finished(&pic.file_name); } } else { infoln!("Rendering thumbnails... (using 1/{total_cores} core)"); for Job { pic_id, thumb_path, .. } in &jobs { let pic = &pics[*pic_id]; render_thumbnail(pic, thumb_path)?; log::job_finished(&pic.file_name); } } // ========================================================================== if skipped > 1 { warnln!("Skipped {skipped}/{total} entries. Use {FULL_BUILD_OPT} to overwrite", total = pics.len()); } log::finished(start.elapsed()); Ok(()) } fn render_index(pics: &Vec<GalleryEntry>) -> io::Result<()> { let mut path = PathBuf::from(TARGET_PATH); path.push("index.html"); let mut f = create_file(&path)?; writeln!(f, "<!DOCTYPE html>")?; write_license(&mut f)?; writeln!(f, "<html lang=\"en\">")?; writeln!(f, "<head>")?; writeln!(f, "<title>{PAGE_TITLE}</title>")?; write_head(&mut f, false)?; // preload the first 2 pictures in the gallery for pic in pics.iter().take(2) { writeln!( f, "<link rel=\"preload\" as=\"image\" href=\"./{path}\">", path = ThumbPath(pic), )?; } writeln!(f, "</head>")?; writeln!(f, "<body>")?; writeln!(f, "<main>")?; const INTRO_MSG: &str = include_str!("intro.html"); writeln!(f, "{}", INTRO_MSG)?; writeln!(f, "<div id=\"gallery\" role=\"feed\">")?; for pic in pics { writeln!(f, "<article class=\"picture-container\">")?; writeln!( f, "<a aria-label=\"{name}\" href=\"./{PAGES_PATH}/{name}.html\">", name = Escaped(&pic.file_name) )?; writeln!( f, "<img alt=\"{alt}\" src=\"./{path}\">", alt = Escaped(&pic.alt), path = ThumbPath(pic), )?; writeln!(f, "</a>\n</article>")?; } writeln!(f, "</div>")?; const OUTRO_MSG: &str = include_str!("outro.html"); writeln!(f, "{}", OUTRO_MSG)?; writeln!(f, "</main>")?; writeln!(f, "</body>")?; writeln!(f, "</html>") } fn render_pic_page(pic: &GalleryEntry, path: &Path) -> io::Result<()> { let mut f = create_file(path)?; writeln!(&mut f, "<!DOCTYPE html>")?; write_license(&mut f)?; writeln!(&mut f, "<html lang=\"en\">")?; writeln!(&mut f, "<head>")?; writeln!( &mut f, "<title>{PAGE_TITLE} ‐ {name}</title>", name = Escaped(&pic.file_name) )?; write_head(&mut f, true)?; writeln!( &mut f, "<link rel=\"preload\" as=\"image\" href=\"../{path}\">", path = ThumbPath(pic), )?; writeln!(&mut f, "</head>")?; writeln!(&mut f, "<body>")?; writeln!(&mut f, "<main>")?; writeln!( &mut f, "<h1 class=\"picture-title\">{name}</h1>", name = Escaped(&pic.file_name) )?; if pic.caption.is_some() { writeln!(&mut f, "<figure>")?; } else { writeln!(&mut f, "<figure aria-label=\"File {name}\">", name = Escaped(&pic.file_name))?; } writeln!(&mut f, "<div id=\"picture\">")?; writeln!(&mut f, "<div>")?; writeln!(&mut f, "<div class=\"picture-container\">")?; writeln!( &mut f, "<img alt=\"{alt}\" src=\"../{path}\">", alt = Escaped(&pic.alt), path = ThumbPath(pic), )?; writeln!(&mut f, "</div>")?; writeln!(&mut f, "<nav id=\"picture-nav\">")?; writeln!(&mut f, "<ul>")?; writeln!( &mut f, "<li><a href=\"../{IMAGES_PATH}/{name}\">download</a></li>", name = Escaped(&pic.file_name), )?; if let Some(src) = &pic.source { writeln!(&mut f, "<li><a href=\"{src}\">original source</a></li>")?; } writeln!(&mut f, "</ul>")?; writeln!(&mut f, "</nav>")?; writeln!(&mut f, "</div>")?; writeln!(&mut f, "</div>")?; if let Some(caption) = &pic.caption { writeln!(&mut f, "<figcaption>")?; writeln!(&mut f, "{}", Escaped(caption))?; writeln!(&mut f, "</figcaption>")?; } writeln!(&mut f, "</figure>")?; writeln!(&mut f, "</main>")?; writeln!(&mut f, "<footer>")?; write!(&mut f, "original work by ")?; if let Some(url) = &pic.author_url { writeln!(&mut f, "<a role=\"author\" href=\"{url}\">{author}</a>", author = Escaped(&pic.author))?; } else { writeln!(&mut f, "{}", Escaped(&pic.author))?; } writeln!(&mut f, "<br>")?; match &pic.license { LicenseType::Cc(license) => { writeln!( &mut f, "licensed under <a role=\"license\" href=\"{url}\">{license}</a>", url = license.url() )?; } LicenseType::PublicDomain => writeln!(&mut f, "this is public domain")?, LicenseType::Proprietary => { writeln!( &mut f, "this is distributed under a proprietary license" )?; } } writeln!(&mut f, "</footer>")?; writeln!(&mut f, "</body>")?; writeln!(&mut f, "</html>") } /// Prints the common head elements to a given file fn write_head(f: &mut File, pic_page: bool) -> io::Result<()> { const AUTHOR: &str = "Thiago Brevidelli"; const LICENSE: &str = "GPLv3"; writeln!(f, "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">")?; writeln!(f, "<meta name=\"author\" content=\"{AUTHOR}\">")?; writeln!(f, "<meta name=\"copyright\" content=\"{LICENSE}\">")?; writeln!(f, "<meta content=\"text/html; charset=utf-8\" http-equiv=\"content-type\">")?; if pic_page { writeln!(f, "<link rel=\"stylesheet\" href=\"../{STYLES_PATH}\">")?; } else { writeln!(f, "<link rel=\"stylesheet\" href=\"./{STYLES_PATH}\">")?; } Ok(()) } /// Prints a HTML comment with GPL licensing info fn write_license(f: &mut File) -> io::Result<()> { const LICENSE_COMMENT: &str = include_str!("license.html"); writeln!(f, "{}", LICENSE_COMMENT) } fn render_thumbnail(pic: &GalleryEntry, thumb_path: &Path) -> Result<(), ()> { match pic.file_format { FileFormat::TeX => { // tikztosvg -o thumb_path // -p relsize // -p xfrac // -l matrix // -l patterns // -l shapes.geometric // -l arrows // -q // pic.path let mut tikztosvg_cmd = Command::new("tikztosvg"); tikztosvg_cmd.arg("-o") .arg(thumb_path) .args([ "-p", "relsize", "-p", "xfrac", "-l", "matrix", "-l", "patterns", "-l", "shapes.geometric", "-l", "arrows", "-q", ]) .arg(&pic.path); let exit_code = tikztosvg_cmd .status() .map_err(|e| errorln!("Failed to run tikztosvg: {e}"))?; if !exit_code.success() { errorln!( "Failed to run tikztosvg: {tikztosvg_cmd:?} returned exit code {exit_code}" ); return Err(()); } }, FileFormat::Svg => { let mut src_path = PathBuf::from(TARGET_PATH); src_path.push(IMAGES_PATH); src_path.push(&pic.file_name); copy(&src_path, thumb_path)?; }, FileFormat::Jpeg | FileFormat::Png => { /// Target height of the thumbnails const THUMB_HEIGHT: u32 = 500; const WEBP_IMAGE_QUALITY: f32 = 90.0; let mut thumb_file = create_file(thumb_path).map_err(|_| ())?; let img_reader = ImageReader::open(&pic.path) .map_err(|e| { errorln!( "Couldn't open file {path:?} to render thumbnail: {e}", path = pic.file_name, ); })?; let img = img_reader .decode() .map_err(|e| { errorln!( "Faileded to decode image file {name:?}: {e}", name = pic.file_name, ); })?; let h = THUMB_HEIGHT; let w = (h * img.width()) / img.height(); // we should make sure that the image is in the RGBA8 format so that // the webp crate can encode it let img = DynamicImage::from(img.thumbnail(w, h).into_rgba8()); let mem = webp::Encoder::from_image(&img) .expect("image should be in the RGBA8 format") .encode(WEBP_IMAGE_QUALITY); if let Err(e) = thumb_file.write_all(&mem) { errorln!( "Couldn't write thumnail to file {thumb_path:?}: {e}"); return Err(()); } } } Ok(()) } fn needs_update(pic: &GalleryEntry, dst: &Path) -> bool { let dst_meta = fs::metadata(dst); if let (Ok(dst_meta), Some(pic_meta)) = (&dst_meta, &pic.metadata) { if dst_meta.modified().unwrap() > pic_meta.modified().unwrap() { return false; } } true } fn create_file(path: &Path) -> io::Result<File> { File::create(path) .map_err(|e| { errorln!("Could not open file {path:?}: {e}"); e }) } fn copy(from: &Path, to: &Path) -> Result<(), ()> { fs::copy(from, to) .map(|_| ()) .map_err(|e| errorln!("Failed to copy {from:?} to {to:?}: {e}")) } impl Display for ThumbPath<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { write!(f, "{THUMBS_PATH}/{name}", name = Escaped(&self.0.file_name))?; match self.0.file_format { FileFormat::TeX => write!(f, ".svg")?, FileFormat::Svg => {} FileFormat::Jpeg | FileFormat::Png => write!(f, ".webp")?, } Ok(()) } } impl From<ThumbPath<'_>> for PathBuf { fn from(thumb_path: ThumbPath<'_>) -> Self { let pic = thumb_path.0; let mut result = PathBuf::from(TARGET_PATH); result.push(THUMBS_PATH); match pic.file_format { FileFormat::TeX => { result.push(pic.file_name.clone() + ".svg"); } FileFormat::Svg => { result.push(&pic.file_name); } FileFormat::Jpeg | FileFormat::Png => { result.push(pic.file_name.clone() + ".webp"); } } result } } impl Display for HtmlFileName<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { write!(f, "{}.html", self.0) } }