From ac03b001f0edf9088a091f228ba1672f82cf2752 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Fri, 17 May 2024 13:08:06 +0100 Subject: [PATCH] Write HTML and copy font files to output directory --- src/bin/diffenator3.rs | 81 +++++++++++++++++++++++---------- src/html.rs | 15 ++++-- templates/WordDiff.partial.html | 14 +++--- templates/diffenator.html | 8 ++-- 4 files changed, 78 insertions(+), 40 deletions(-) diff --git a/src/bin/diffenator3.rs b/src/bin/diffenator3.rs index abf6ade..dd19d42 100644 --- a/src/bin/diffenator3.rs +++ b/src/bin/diffenator3.rs @@ -7,7 +7,10 @@ use diffenator3::{ ttj::{jsondiff::Substantial, table_diff}, }; use serde_json::Map; -use std::{error::Error, path::PathBuf}; +use std::{ + error::Error, + path::{Path, PathBuf}, +}; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] @@ -51,6 +54,10 @@ struct Cli { #[clap(long = "html")] html: bool, + /// Output directory for HTML + #[clap(long = "output", default_value = "out", requires = "html")] + output: String, + /// Location in design space, in the form axis=123,other=456 #[clap(long = "location")] location: Option, @@ -117,12 +124,12 @@ fn main() { let mut font_a = DFont::new(&font_binary_a); let mut font_b = DFont::new(&font_binary_b); - if let Some(loc) = cli.location { - let _hack = font_a.set_location(&loc); - let _hack = font_b.set_location(&loc); - } else if let Some(inst) = cli.instance { - font_a.set_instance(&inst).expect("Couldn't find instance"); - font_b.set_instance(&inst).expect("Couldn't find instance"); + if let Some(ref loc) = cli.location { + let _hack = font_a.set_location(loc); + let _hack = font_b.set_location(loc); + } else if let Some(ref inst) = cli.instance { + font_a.set_instance(inst).expect("Couldn't find instance"); + font_b.set_instance(inst).expect("Couldn't find instance"); } let mut diff = Map::new(); if cli.tables { @@ -142,23 +149,7 @@ fn main() { diff.insert("words".into(), word_diff); } if cli.html { - let font_face_old = CSSFontFace::new(cli.font1.to_str().unwrap(), "old", &font_a); - let font_face_new = CSSFontFace::new(cli.font2.to_str().unwrap(), "new", &font_b); - let font_style_old = CSSFontStyle::new(&font_a, Some("old")); - let font_style_new = CSSFontStyle::new(&font_b, Some("new")); - let value = serde_json::to_value(&diff).unwrap_or_else(|e| { - die("serializing diff", e); - }); - let html = render_output( - &value, - font_face_old, - font_face_new, - font_style_old, - font_style_new, - ) - .unwrap_or_else(|err| die("rendering HTML", err)); - println!("{}", html); - std::process::exit(0); + do_html(&cli, &font_a, &font_b, diff); } if cli.json { println!("{}", serde_json::to_string_pretty(&diff).expect("foo")); @@ -220,3 +211,45 @@ fn main() { } } } + +fn do_html(cli: &Cli, font_a: &DFont, font_b: &DFont, diff: Map) -> ! { + // Make output directory + let output_dir = Path::new(&cli.output); + if !output_dir.exists() { + std::fs::create_dir(output_dir).expect("Couldn't create output directory"); + } + + // Copy old font to output/old- + let old_font = output_dir.join(format!( + "old-{}", + cli.font1.file_name().unwrap().to_str().unwrap() + )); + std::fs::copy(&cli.font1, &old_font).expect("Couldn't copy old font"); + let new_font = output_dir.join(format!( + "new-{}", + cli.font2.file_name().unwrap().to_str().unwrap() + )); + std::fs::copy(&cli.font2, &new_font).expect("Couldn't copy new font"); + + let font_face_old = CSSFontFace::new(&old_font, "old", font_a); + let font_face_new = CSSFontFace::new(&new_font, "new", font_b); + let font_style_old = CSSFontStyle::new(font_a, Some("old")); + let font_style_new = CSSFontStyle::new(font_b, Some("new")); + let value = serde_json::to_value(&diff).unwrap_or_else(|e| { + die("serializing diff", e); + }); + let html = render_output( + &value, + font_face_old, + font_face_new, + font_style_old, + font_style_new, + ) + .unwrap_or_else(|err| die("rendering HTML", err)); + + // Write output + let output_file = output_dir.join("diffenator.html"); + println!("Writing output to {}", output_file.to_str().unwrap()); + std::fs::write(output_file, html).expect("Couldn't write output file"); + std::process::exit(0); +} diff --git a/src/html.rs b/src/html.rs index 00299f6..d976d8a 100644 --- a/src/html.rs +++ b/src/html.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::{collections::HashMap, path::Path}; use lazy_static::lazy_static; use serde::Serialize; @@ -20,7 +20,7 @@ pub struct CSSFontFace { } impl CSSFontFace { - pub fn new(filename: &str, suffix: &str, dfont: &DFont) -> Self { + pub fn new(filename: &Path, suffix: &str, dfont: &DFont) -> Self { let familyname = suffix.to_string() + " " + &dfont.family_name(); let cssfamilyname = familyname.clone(); let class_name = cssfamilyname.replace(' ', "-"); @@ -30,7 +30,12 @@ impl CSSFontFace { CSSFontFace { suffix: suffix.to_string(), - filename: filename.to_string(), + filename: filename + .file_name() + .unwrap() + .to_str() + .unwrap_or_default() + .to_string(), familyname, cssfamilyname, class_name, @@ -93,7 +98,7 @@ lazy_static! { match Tera::new("templates/*") { Ok(t) => t, Err(e) => { - println!("Parsing error(s): {}", e); + eprintln!("Parsing error(s): {}", e); ::std::process::exit(1); } } @@ -121,7 +126,7 @@ pub fn render_output( "font_styles_old": [font_style_old], "font_styles_new": [font_style_new], "font_styles": [], - "pt_size": 20, + "pt_size": 40, "include_ui": true, }))?, ) diff --git a/templates/WordDiff.partial.html b/templates/WordDiff.partial.html index abc1823..2cf49fd 100644 --- a/templates/WordDiff.partial.html +++ b/templates/WordDiff.partial.html @@ -1,19 +1,19 @@ -
+
- {{ string }}
+ {{ diff.word }}
Before:
- {{ hb_a }} - feats={{ ot_features }} + {{ diff.buffer_a }} + feats={{ diff.ot_features }}
After:
- {{ hb_b }} - feats={{ ot_features }} + {{ diff.buffer_b }} + feats={{ diff.ot_features }}
- changed pixels: {{ changed_pixels }}% + changed pixels: {{ diff.percent }}%
diff --git a/templates/diffenator.html b/templates/diffenator.html index 92f4b83..fd6127e 100644 --- a/templates/diffenator.html +++ b/templates/diffenator.html @@ -88,8 +88,8 @@
Misshapen {{ script }} words
- {% for word in words %} - {{ word }} + {% for diff in words %} + {% include "WordDiff.partial.html" %} {% endfor %}
@@ -100,8 +100,8 @@
Misshapen user strings
- {% for word in diff.strings %} - {{ word }} + {% for diff in diff.strings %} + {% include "WordDiff.partial.html" %} {% endfor %}