1use std::collections::BTreeMap;
2
3use crate::manifest::{Manifest, ManifestOutput};
4use crate::quality::manifest_quality_warnings;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct ManifestReviewOptions {
8 pub asset_path_prefix: String,
9 pub top_limit: usize,
10}
11
12impl Default for ManifestReviewOptions {
13 fn default() -> Self {
14 Self {
15 asset_path_prefix: String::new(),
16 top_limit: 8,
17 }
18 }
19}
20
21pub fn render_manifest_review(manifest: &Manifest, options: &ManifestReviewOptions) -> String {
22 let groups = grouped_outputs(manifest);
23 let warnings = review_warnings(manifest);
24 let source_count = groups.len();
25 let top_limit = options.top_limit.max(1);
26
27 let mut out = String::new();
28 out.push_str("<!doctype html>\n");
29 out.push_str("<html lang=\"en\">\n");
30 out.push_str("<head>\n");
31 out.push_str(" <meta charset=\"utf-8\">\n");
32 out.push_str(" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n");
33 out.push_str(" <title>DevImg Review</title>\n");
34 out.push_str(" <style>\n");
35 out.push_str(STYLES);
36 out.push_str(" </style>\n");
37 out.push_str("</head>\n");
38 out.push_str("<body>\n");
39 out.push_str(" <main class=\"page\">\n");
40 out.push_str(" <header class=\"hero\">\n");
41 out.push_str(" <p class=\"eyebrow\">DevImg visual review</p>\n");
42 out.push_str(" <h1>Generated image variants</h1>\n");
43 out.push_str(" <dl class=\"meta-grid\">\n");
44 push_stat(&mut out, "Sources", &source_count.to_string());
45 push_stat(&mut out, "Variants", &manifest.outputs.len().to_string());
46 push_stat(
47 &mut out,
48 "Source bytes",
49 &format_bytes(manifest.source_bytes_total()),
50 );
51 push_stat(
52 &mut out,
53 "Output bytes",
54 &format_bytes(manifest.output_bytes_total()),
55 );
56 push_stat(&mut out, "Budget status", "not evaluated from manifest");
57 push_stat(&mut out, "Config hash", &short_hash(&manifest.config_hash));
58 out.push_str(" </dl>\n");
59 out.push_str(" </header>\n");
60
61 out.push_str(" <section class=\"panel\">\n");
62 out.push_str(" <div class=\"section-heading\">\n");
63 out.push_str(" <h2>Manifest</h2>\n");
64 out.push_str(" </div>\n");
65 out.push_str(" <div class=\"manifest-grid\">\n");
66 push_key_value(&mut out, "Generated at", &manifest.generated_at);
67 push_key_value(&mut out, "Config path", &manifest.config_path);
68 push_key_value(&mut out, "Config hash", &manifest.config_hash);
69 push_key_value(
70 &mut out,
71 "Budget status",
72 "not evaluated; run devimg check --config <path>",
73 );
74 out.push_str(" </div>\n");
75 out.push_str(" </section>\n");
76
77 out.push_str(" <section class=\"panel\">\n");
78 out.push_str(" <div class=\"section-heading\">\n");
79 out.push_str(" <h2>Review signals</h2>\n");
80 out.push_str(" </div>\n");
81 if warnings.is_empty() {
82 out.push_str(" <p class=\"empty-state\">No manifest-only warnings.</p>\n");
83 } else {
84 out.push_str(" <ul class=\"signal-list\">\n");
85 for warning in &warnings {
86 out.push_str(" <li>");
87 push_escaped(&mut out, warning);
88 out.push_str("</li>\n");
89 }
90 out.push_str(" </ul>\n");
91 }
92 out.push_str(" </section>\n");
93
94 out.push_str(" <section class=\"panel split-panel\">\n");
95 push_top_sources(&mut out, &groups, top_limit);
96 push_top_outputs(&mut out, &manifest.outputs, top_limit);
97 out.push_str(" </section>\n");
98
99 for (source_path, outputs) in &groups {
100 push_source_group(&mut out, source_path, outputs, options);
101 }
102
103 out.push_str(" </main>\n");
104 out.push_str("</body>\n");
105 out.push_str("</html>\n");
106 out
107}
108
109fn grouped_outputs(manifest: &Manifest) -> BTreeMap<&str, Vec<&ManifestOutput>> {
110 let mut groups = BTreeMap::<&str, Vec<&ManifestOutput>>::new();
111 for output in &manifest.outputs {
112 groups
113 .entry(output.source_path.as_str())
114 .or_default()
115 .push(output);
116 }
117 for outputs in groups.values_mut() {
118 outputs.sort_by(|left, right| {
119 left.preset
120 .cmp(&right.preset)
121 .then(left.width.cmp(&right.width))
122 .then(left.height.cmp(&right.height))
123 .then(left.format.cmp(&right.format))
124 .then(left.output_path.cmp(&right.output_path))
125 });
126 }
127 groups
128}
129
130fn review_warnings(manifest: &Manifest) -> Vec<String> {
131 if manifest.outputs.is_empty() {
132 return vec!["Manifest has no generated variants.".to_string()];
133 }
134 manifest_quality_warnings(manifest)
135}
136
137fn push_source_group(
138 out: &mut String,
139 source_path: &str,
140 outputs: &[&ManifestOutput],
141 options: &ManifestReviewOptions,
142) {
143 let first = outputs[0];
144 let source_href = asset_href(source_path, &options.asset_path_prefix);
145 let variant_total: u64 = outputs.iter().map(|output| output.bytes).sum();
146
147 out.push_str(" <section class=\"source-group\">\n");
148 out.push_str(" <div class=\"source-intro\">\n");
149 out.push_str(" <div>\n");
150 out.push_str(" <p class=\"eyebrow\">Source</p>\n");
151 out.push_str(" <h2>");
152 push_escaped(out, source_path);
153 out.push_str("</h2>\n");
154 out.push_str(" </div>\n");
155 out.push_str(" <dl class=\"source-stats\">\n");
156 push_stat(
157 out,
158 "Dimensions",
159 &format!("{}x{}", first.source_width, first.source_height),
160 );
161 push_stat(out, "Source bytes", &format_bytes(first.source_bytes));
162 push_stat(out, "Variant bytes", &format_bytes(variant_total));
163 push_stat(out, "Variants", &outputs.len().to_string());
164 out.push_str(" </dl>\n");
165 out.push_str(" </div>\n");
166
167 out.push_str(" <div class=\"source-preview\">\n");
168 push_image(
169 out,
170 &source_href,
171 &format!("Source image: {source_path}"),
172 first.source_width,
173 first.source_height,
174 );
175 out.push_str(" </div>\n");
176
177 out.push_str(" <div class=\"variant-grid\">\n");
178 for output in outputs {
179 push_variant_card(out, output, options);
180 }
181 out.push_str(" </div>\n");
182 out.push_str(" </section>\n");
183}
184
185fn push_variant_card(out: &mut String, output: &ManifestOutput, options: &ManifestReviewOptions) {
186 let output_href = asset_href(&output.output_path, &options.asset_path_prefix);
187 out.push_str(" <article class=\"variant-card\">\n");
188 out.push_str(" <div class=\"variant-image\">\n");
189 push_image(
190 out,
191 &output_href,
192 &format!(
193 "{} {} {}x{}",
194 output.preset, output.format, output.width, output.height
195 ),
196 output.width,
197 output.height,
198 );
199 out.push_str(" </div>\n");
200 out.push_str(" <div class=\"variant-body\">\n");
201 out.push_str(" <div class=\"variant-title\">\n");
202 out.push_str(" <strong>");
203 push_escaped(out, &output.preset);
204 out.push_str("</strong>\n");
205 out.push_str(" <span>");
206 push_escaped(out, &output.format);
207 out.push_str("</span>\n");
208 out.push_str(" </div>\n");
209 out.push_str(" <dl class=\"variant-meta\">\n");
210 push_key_value(out, "Size", &format!("{}x{}", output.width, output.height));
211 push_key_value(out, "Fit", &output.fit);
212 push_key_value(out, "Bytes", &format_bytes(output.bytes));
213 push_key_value(out, "Hash", &short_hash(&output.hash));
214 out.push_str(" </dl>\n");
215 out.push_str(" <a class=\"path-link\" href=\"");
216 push_attr_escaped(out, &output_href);
217 out.push_str("\">");
218 push_escaped(out, &output.output_path);
219 out.push_str("</a>\n");
220 out.push_str(" </div>\n");
221 out.push_str(" </article>\n");
222}
223
224fn push_top_sources(
225 out: &mut String,
226 groups: &BTreeMap<&str, Vec<&ManifestOutput>>,
227 top_limit: usize,
228) {
229 let mut sources: Vec<(&str, u64)> = groups
230 .iter()
231 .map(|(source, outputs)| (*source, outputs[0].source_bytes))
232 .collect();
233 sources.sort_by(|left, right| right.1.cmp(&left.1).then(left.0.cmp(right.0)));
234
235 out.push_str(" <div>\n");
236 out.push_str(" <h2>Largest sources</h2>\n");
237 push_ranked_list(out, sources.into_iter().take(top_limit));
238 out.push_str(" </div>\n");
239}
240
241fn push_top_outputs(out: &mut String, outputs: &[ManifestOutput], top_limit: usize) {
242 let mut ranked: Vec<(&str, u64)> = outputs
243 .iter()
244 .map(|output| (output.output_path.as_str(), output.bytes))
245 .collect();
246 ranked.sort_by(|left, right| right.1.cmp(&left.1).then(left.0.cmp(right.0)));
247
248 out.push_str(" <div>\n");
249 out.push_str(" <h2>Largest outputs</h2>\n");
250 push_ranked_list(out, ranked.into_iter().take(top_limit));
251 out.push_str(" </div>\n");
252}
253
254fn push_ranked_list<'a>(out: &mut String, items: impl Iterator<Item = (&'a str, u64)>) {
255 let mut count = 0;
256 out.push_str(" <ol class=\"ranked-list\">\n");
257 for (path, bytes) in items {
258 count += 1;
259 out.push_str(" <li><span>");
260 push_escaped(out, path);
261 out.push_str("</span><strong>");
262 push_escaped(out, &format_bytes(bytes));
263 out.push_str("</strong></li>\n");
264 }
265 if count == 0 {
266 out.push_str(" <li><span>No entries.</span><strong>0 B</strong></li>\n");
267 }
268 out.push_str(" </ol>\n");
269}
270
271fn push_stat(out: &mut String, label: &str, value: &str) {
272 out.push_str(" <div>\n");
273 out.push_str(" <dt>");
274 push_escaped(out, label);
275 out.push_str("</dt>\n");
276 out.push_str(" <dd>");
277 push_escaped(out, value);
278 out.push_str("</dd>\n");
279 out.push_str(" </div>\n");
280}
281
282fn push_key_value(out: &mut String, label: &str, value: &str) {
283 out.push_str(" <div><dt>");
284 push_escaped(out, label);
285 out.push_str("</dt><dd>");
286 push_escaped(out, value);
287 out.push_str("</dd></div>\n");
288}
289
290fn push_image(out: &mut String, href: &str, alt: &str, width: u32, height: u32) {
291 out.push_str(" <a href=\"");
292 push_attr_escaped(out, href);
293 out.push_str("\"><img src=\"");
294 push_attr_escaped(out, href);
295 out.push_str("\" alt=\"");
296 push_attr_escaped(out, alt);
297 out.push_str("\" width=\"");
298 out.push_str(&width.to_string());
299 out.push_str("\" height=\"");
300 out.push_str(&height.to_string());
301 out.push_str("\" loading=\"lazy\" decoding=\"async\"></a>\n");
302}
303
304fn asset_href(path: &str, prefix: &str) -> String {
305 let path = path.replace('\\', "/");
306 let prefix = prefix.replace('\\', "/");
307 let prefix = prefix.trim_end_matches('/');
308 if prefix.is_empty() {
309 path
310 } else if path.is_empty() {
311 prefix.to_string()
312 } else {
313 format!("{prefix}/{}", path.trim_start_matches('/'))
314 }
315}
316
317fn short_hash(hash: &str) -> String {
318 hash.split_once(':')
319 .map(|(_, value)| value)
320 .unwrap_or(hash)
321 .chars()
322 .take(12)
323 .collect()
324}
325
326fn format_bytes(bytes: u64) -> String {
327 const KB: f64 = 1024.0;
328 const MB: f64 = KB * 1024.0;
329 let bytes = bytes as f64;
330 if bytes >= MB {
331 format!("{:.2} MB", bytes / MB)
332 } else if bytes >= KB {
333 format!("{:.1} KB", bytes / KB)
334 } else {
335 format!("{} B", bytes as u64)
336 }
337}
338
339fn push_escaped(out: &mut String, value: &str) {
340 for ch in value.chars() {
341 match ch {
342 '&' => out.push_str("&"),
343 '<' => out.push_str("<"),
344 '>' => out.push_str(">"),
345 '"' => out.push_str("""),
346 '\'' => out.push_str("'"),
347 _ => out.push(ch),
348 }
349 }
350}
351
352fn push_attr_escaped(out: &mut String, value: &str) {
353 push_escaped(out, value);
354}
355
356const STYLES: &str = r#":root {
357 color-scheme: light;
358 --paper: #f7f4ee;
359 --ink: #1c211f;
360 --muted: #646b67;
361 --line: #d8d1c5;
362 --panel: #fffdf8;
363 --accent: #0e6f68;
364 --accent-ink: #06443f;
365 --warn: #9a5a00;
366 --danger: #9d2f2f;
367 --shadow: 0 16px 38px rgba(44, 38, 28, 0.08);
368 }
369
370 * {
371 box-sizing: border-box;
372 }
373
374 body {
375 margin: 0;
376 background:
377 linear-gradient(90deg, rgba(28, 33, 31, 0.035) 1px, transparent 1px),
378 linear-gradient(180deg, rgba(28, 33, 31, 0.035) 1px, transparent 1px),
379 var(--paper);
380 background-size: 36px 36px;
381 color: var(--ink);
382 font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
383 }
384
385 .page {
386 width: min(1440px, calc(100% - 40px));
387 margin: 0 auto;
388 padding: 40px 0 56px;
389 }
390
391 .hero,
392 .panel,
393 .source-group {
394 background: rgba(255, 253, 248, 0.94);
395 border: 1px solid var(--line);
396 box-shadow: var(--shadow);
397 }
398
399 .hero {
400 padding: 32px;
401 border-radius: 6px;
402 }
403
404 .eyebrow {
405 margin: 0 0 10px;
406 color: var(--accent-ink);
407 font-size: 12px;
408 font-weight: 800;
409 letter-spacing: 0;
410 text-transform: uppercase;
411 }
412
413 h1,
414 h2 {
415 margin: 0;
416 letter-spacing: 0;
417 }
418
419 h1 {
420 max-width: 920px;
421 font-size: 72px;
422 line-height: 0.95;
423 }
424
425 h2 {
426 font-size: 20px;
427 }
428
429 .meta-grid,
430 .source-stats {
431 display: grid;
432 grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
433 gap: 12px;
434 margin: 28px 0 0;
435 }
436
437 .meta-grid div,
438 .source-stats div {
439 min-width: 0;
440 border-top: 3px solid var(--accent);
441 padding-top: 10px;
442 }
443
444 dt {
445 color: var(--muted);
446 font-size: 12px;
447 font-weight: 700;
448 text-transform: uppercase;
449 }
450
451 dd {
452 margin: 5px 0 0;
453 min-width: 0;
454 overflow-wrap: anywhere;
455 font-weight: 750;
456 }
457
458 .panel,
459 .source-group {
460 margin-top: 18px;
461 border-radius: 6px;
462 padding: 24px;
463 }
464
465 .section-heading {
466 display: flex;
467 align-items: end;
468 justify-content: space-between;
469 gap: 18px;
470 margin-bottom: 18px;
471 }
472
473 .manifest-grid,
474 .variant-meta {
475 display: grid;
476 gap: 10px;
477 }
478
479 .manifest-grid {
480 grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
481 }
482
483 .manifest-grid div,
484 .variant-meta div {
485 min-width: 0;
486 }
487
488 .signal-list,
489 .ranked-list {
490 margin: 0;
491 padding-left: 20px;
492 }
493
494 .signal-list li {
495 margin: 8px 0;
496 color: var(--warn);
497 font-weight: 700;
498 overflow-wrap: anywhere;
499 }
500
501 .empty-state {
502 margin: 0;
503 color: var(--muted);
504 }
505
506 .split-panel {
507 display: grid;
508 grid-template-columns: repeat(2, minmax(0, 1fr));
509 gap: 28px;
510 }
511
512 .ranked-list {
513 display: grid;
514 gap: 8px;
515 margin-top: 14px;
516 }
517
518 .ranked-list li {
519 display: flex;
520 justify-content: space-between;
521 gap: 18px;
522 border-bottom: 1px solid var(--line);
523 padding-bottom: 8px;
524 }
525
526 .ranked-list span {
527 min-width: 0;
528 overflow-wrap: anywhere;
529 }
530
531 .ranked-list strong {
532 flex: 0 0 auto;
533 }
534
535 .source-group {
536 display: grid;
537 grid-template-columns: minmax(280px, 380px) minmax(0, 1fr);
538 gap: 22px;
539 }
540
541 .source-intro {
542 grid-column: 1 / -1;
543 display: grid;
544 grid-template-columns: minmax(0, 1fr) minmax(260px, 0.8fr);
545 gap: 24px;
546 }
547
548 .source-intro h2 {
549 overflow-wrap: anywhere;
550 }
551
552 .source-preview,
553 .variant-image {
554 display: grid;
555 place-items: center;
556 min-height: 180px;
557 overflow: hidden;
558 border: 1px solid var(--line);
559 background: #ece7dc;
560 }
561
562 .source-preview {
563 align-self: start;
564 }
565
566 img {
567 display: block;
568 width: 100%;
569 height: auto;
570 max-height: 520px;
571 object-fit: contain;
572 }
573
574 .source-preview img {
575 max-height: 420px;
576 }
577
578 .variant-grid {
579 display: grid;
580 grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
581 gap: 14px;
582 }
583
584 .variant-card {
585 min-width: 0;
586 overflow: hidden;
587 border: 1px solid var(--line);
588 border-radius: 6px;
589 background: var(--panel);
590 }
591
592 .variant-body {
593 display: grid;
594 gap: 12px;
595 padding: 14px;
596 }
597
598 .variant-title {
599 display: flex;
600 align-items: center;
601 justify-content: space-between;
602 gap: 10px;
603 }
604
605 .variant-title span {
606 border: 1px solid var(--line);
607 border-radius: 999px;
608 padding: 3px 8px;
609 color: var(--accent-ink);
610 font-size: 12px;
611 font-weight: 800;
612 text-transform: uppercase;
613 }
614
615 .path-link {
616 color: var(--accent-ink);
617 overflow-wrap: anywhere;
618 font-size: 12px;
619 font-weight: 700;
620 text-decoration-thickness: 2px;
621 text-underline-offset: 3px;
622 }
623
624 @media (max-width: 860px) {
625 .page {
626 width: min(100% - 24px, 720px);
627 padding-top: 20px;
628 }
629
630 .hero,
631 .panel,
632 .source-group {
633 padding: 18px;
634 }
635
636 h1 {
637 font-size: 42px;
638 }
639
640 .split-panel,
641 .source-group,
642 .source-intro {
643 grid-template-columns: 1fr;
644 }
645 }
646"#;
647
648#[cfg(test)]
649mod tests {
650 use super::{render_manifest_review, ManifestReviewOptions};
651 use crate::manifest::{Manifest, ManifestOutput};
652
653 #[test]
654 fn review_html_escapes_manifest_text() {
655 let manifest = Manifest {
656 version: 1,
657 generated_at: "unix:1".to_string(),
658 config_path: "dev\"img.toml".to_string(),
659 config_hash: "blake3:<config>&\"".to_string(),
660 outputs: vec![ManifestOutput {
661 source_path: "assets/<card>&\".png".to_string(),
662 source_hash: "blake3:source".to_string(),
663 source_width: 800,
664 source_height: 450,
665 source_bytes: 123,
666 output_path: "public/images/<card>.webp".to_string(),
667 preset: "project-card<script>".to_string(),
668 fit: "cover".to_string(),
669 width: 640,
670 height: 360,
671 format: "webp".to_string(),
672 bytes: 45,
673 hash: "blake3:output".to_string(),
674 operation_hash: "blake3:operation".to_string(),
675 }],
676 };
677
678 let html = render_manifest_review(&manifest, &ManifestReviewOptions::default());
679
680 assert!(html.contains("assets/<card>&".png"));
681 assert!(html.contains("project-card<script>"));
682 assert!(html.contains("public/images/<card>.webp"));
683 assert!(!html.contains("project-card<script>"));
684 }
685
686 #[test]
687 fn review_html_groups_sources_and_links_assets() {
688 let manifest = Manifest {
689 version: 1,
690 generated_at: "unix:1".to_string(),
691 config_path: "devimg.toml".to_string(),
692 config_hash: "blake3:config".to_string(),
693 outputs: vec![
694 output(
695 "assets/card.png",
696 "public/images/card.960.webp",
697 960,
698 540,
699 200,
700 ),
701 output(
702 "assets/card.png",
703 "public/images/card.640.webp",
704 640,
705 360,
706 100,
707 ),
708 output(
709 "assets/logo.png",
710 "public/images/logo.256.webp",
711 256,
712 256,
713 50,
714 ),
715 ],
716 };
717 let options = ManifestReviewOptions {
718 asset_path_prefix: "..".to_string(),
719 top_limit: 2,
720 };
721
722 let html = render_manifest_review(&manifest, &options);
723
724 assert_eq!(html.matches("class=\"source-group\"").count(), 2);
725 assert!(html.contains("Sources</dt>\n <dd>2</dd>"));
726 assert!(html.contains("Variants</dt>\n <dd>3</dd>"));
727 assert!(html.contains("src=\"../public/images/card.640.webp\""));
728 assert!(html.contains("href=\"../assets/card.png\""));
729 assert!(html.contains("Largest outputs"));
730 }
731
732 fn output(source: &str, path: &str, width: u32, height: u32, bytes: u64) -> ManifestOutput {
733 ManifestOutput {
734 source_path: source.to_string(),
735 source_hash: format!("blake3:{source}"),
736 source_width: 800,
737 source_height: 450,
738 source_bytes: 500,
739 output_path: path.to_string(),
740 preset: "project-card".to_string(),
741 fit: "cover".to_string(),
742 width,
743 height,
744 format: "webp".to_string(),
745 bytes,
746 hash: format!("blake3:{path}"),
747 operation_hash: format!("blake3:{path}:operation"),
748 }
749 }
750}