diff --git a/crates/rundown-converter/src/export.rs b/crates/rundown-converter/src/export.rs index 991a78a..79d6d99 100644 --- a/crates/rundown-converter/src/export.rs +++ b/crates/rundown-converter/src/export.rs @@ -1,15 +1,190 @@ use crate::{ - model::{Analysis, Rundown}, + model::{Analysis, Entry, Rundown, column_index, column_name}, pipeline::validate, + time::display, }; use anyhow::{Result, bail, ensure}; use serde_json::{Value, json}; use std::collections::{BTreeMap, BTreeSet}; use std::{ - io::Write, + fmt::Write as _, + io::{Cursor, Write}, path::Path, sync::atomic::{AtomicBool, Ordering}, }; +use zip::{ZipWriter, write::SimpleFileOptions}; + +fn entry_values(entry: &Entry, rundown: &Rundown, usage: &str, column: Option<&str>) -> String { + entry + .source_rows + .iter() + .flat_map(|row| &row.cells) + .filter(|(letter, cell)| { + !cell.text.trim().is_empty() + && rundown + .mapping + .column_usage + .get(*letter) + .map(String::as_str) + == Some(usage) + && column.is_none_or(|expected| *letter == expected) + }) + .map(|(_, cell)| cell.text.clone()) + .collect::>() + .join("\n") +} + +fn metadata_columns(rundown: &Rundown) -> Vec<(String, String)> { + let mut columns = rundown + .mapping + .column_usage + .iter() + .filter(|(_, usage)| usage.as_str() == "metadata") + .map(|(column, _)| { + let label = rundown + .mapping + .column_labels + .get(column) + .cloned() + .unwrap_or_else(|| column.clone()); + (column.clone(), label) + }) + .collect::>(); + columns.sort_by_key(|(column, _)| column_index(column).unwrap_or(usize::MAX)); + let mut used = [ + "start", + "end", + "duration", + "cue", + "title", + "note", + "timer type", + ] + .into_iter() + .map(str::to_string) + .collect::>(); + for (column, label) in &mut columns { + let base = if label.trim().is_empty() { + format!("Metadata {column}") + } else { + label.trim().to_string() + }; + let mut unique = base.clone(); + if !used.insert(unique.to_lowercase()) { + unique = format!("{base} ({column})"); + used.insert(unique.to_lowercase()); + } + *label = unique; + } + columns +} + +fn xml_text(value: &str) -> String { + let mut escaped = String::with_capacity(value.len()); + for character in value.chars().filter(|character| { + matches!(*character, '\t' | '\n' | '\r') + || ('\u{20}'..='\u{d7ff}').contains(character) + || ('\u{e000}'..='\u{fffd}').contains(character) + || ('\u{10000}'..='\u{10ffff}').contains(character) + }) { + escaped.push_str(match character { + '&' => "&", + '<' => "<", + '>' => ">", + '"' => """, + '\'' => "'", + _ => { + escaped.push(character); + continue; + } + }); + } + escaped +} + +fn worksheet(rundown: &Rundown) -> String { + let metadata = metadata_columns(rundown); + let mut rows = vec![vec![ + "Start".into(), + "End".into(), + "Duration".into(), + "Cue".into(), + "Title".into(), + "Note".into(), + "Timer type".into(), + ]]; + rows[0].extend(metadata.iter().map(|(_, label)| label.clone())); + for entry in rundown.entries.iter().filter(|entry| entry.kind == "event") { + let mut row = vec![ + display(entry.start_ms), + display(entry.end_ms), + display(entry.duration_ms), + entry.source_rows[0].row.to_string(), + entry.title.clone(), + entry_values(entry, rundown, "notes", None), + "count-down".into(), + ]; + row.extend( + metadata + .iter() + .map(|(column, _)| entry_values(entry, rundown, "metadata", Some(column))), + ); + rows.push(row); + } + let mut xml = String::from( + r#""#, + ); + for (row_index, row) in rows.iter().enumerate() { + let _ = write!(xml, "", row_index + 1); + for (column, value) in row.iter().enumerate() { + let _ = write!( + xml, + "{}", + column_name(column), + row_index + 1, + xml_text(value) + ); + } + xml.push_str(""); + } + xml.push_str("", rows.len()); + xml +} + +pub fn xlsx(rundown: &Rundown) -> Result> { + ensure!( + validate(rundown).is_empty(), + "Cannot export an inconsistent rundown" + ); + let mut zip = ZipWriter::new(Cursor::new(Vec::new())); + let options = SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated); + let sheet = worksheet(rundown); + for (name, contents) in [ + ( + "[Content_Types].xml", + r#""#, + ), + ( + "_rels/.rels", + r#""#, + ), + ( + "xl/workbook.xml", + r#""#, + ), + ( + "xl/_rels/workbook.xml.rels", + r#""#, + ), + ("xl/worksheets/sheet1.xml", sheet.as_str()), + ] { + zip.start_file(name, options)?; + zip.write_all(contents.as_bytes())?; + } + Ok(zip.finish()?.into_inner()) +} pub fn ontime(r: &Rundown) -> Result { ensure!( @@ -123,18 +298,35 @@ pub fn write( !output.exists() || overwrite, "Output exists; use --overwrite or choose another path" ); - let value = if neutral { - serde_json::to_value(r)? - } else { - ontime(r)? - }; + let extension = output + .extension() + .and_then(|extension| extension.to_str()) + .unwrap_or_default() + .to_ascii_lowercase(); + ensure!( + matches!(extension.as_str(), "json" | "xlsx"), + "Output must use .json or .xlsx" + ); + ensure!( + !(neutral && extension == "xlsx"), + "Neutral IR export requires a .json output" + ); let parent = output .parent() .filter(|p| !p.as_os_str().is_empty()) .unwrap_or(Path::new(".")); let mut temp = tempfile::NamedTempFile::new_in(parent)?; - serde_json::to_writer_pretty(&mut temp, &value)?; - writeln!(temp)?; + if extension == "xlsx" { + temp.write_all(&xlsx(r)?)?; + } else { + let value = if neutral { + serde_json::to_value(r)? + } else { + ontime(r)? + }; + serde_json::to_writer_pretty(&mut temp, &value)?; + writeln!(temp)?; + } temp.as_file().sync_all()?; if cancel.load(Ordering::Relaxed) { bail!("Cancelled; no output published"); diff --git a/crates/rundown-converter/src/gui.rs b/crates/rundown-converter/src/gui.rs index 4c366bb..7fc9349 100644 --- a/crates/rundown-converter/src/gui.rs +++ b/crates/rundown-converter/src/gui.rs @@ -32,6 +32,36 @@ enum Tab { Results, Details, } +#[derive(Clone, Copy, Default, PartialEq)] +enum ExportChoice { + #[default] + OntimeJson, + OntimeXlsx, + NeutralJson, +} +impl ExportChoice { + fn label(self) -> &'static str { + match self { + Self::OntimeJson => "Ontime project (.json)", + Self::OntimeXlsx => "Ontime spreadsheet (.xlsx)", + Self::NeutralJson => "Neutral intermediate representation (.json)", + } + } + fn extension(self) -> &'static str { + match self { + Self::OntimeJson => "ontime.json", + Self::OntimeXlsx => "ontime.xlsx", + Self::NeutralJson => "neutral.json", + } + } + fn file_extension(self) -> &'static str { + if self == Self::OntimeXlsx { + "xlsx" + } else { + "json" + } + } +} struct App { input: String, output: String, @@ -46,7 +76,7 @@ struct App { rx: Option>, cancel: Arc, overwrite: bool, - neutral: bool, + export_choice: ExportChoice, completed: Option, settings: bool, advanced_settings: bool, @@ -105,7 +135,7 @@ impl Default for App { rx: None, cancel: Arc::new(AtomicBool::new(false)), overwrite: false, - neutral: false, + export_choice: ExportChoice::default(), completed: None, settings: false, advanced_settings: false, @@ -149,7 +179,11 @@ impl App { fn set_input(&mut self, path: PathBuf) { self.tab = Tab::Setup; self.input = path.to_string_lossy().into(); - self.output = path.with_extension("ontime.json").to_string_lossy().into(); + self.export_choice = ExportChoice::OntimeJson; + self.output = path + .with_extension(self.export_choice.extension()) + .to_string_lossy() + .into(); self.profile = "{}".into(); self.profile_path = None; self.sheet.clear(); @@ -157,6 +191,18 @@ impl App { self.tables.clear(); self.invalidate(); } + fn change_export_choice(&mut self, choice: ExportChoice) { + if self.export_choice == choice { + return; + } + self.export_choice = choice; + if !self.input.is_empty() { + self.output = PathBuf::from(&self.input) + .with_extension(choice.extension()) + .to_string_lossy() + .into(); + } + } fn begin(&mut self) -> mpsc::Sender { let (tx, rx) = mpsc::channel(); self.rx = Some(rx); @@ -219,7 +265,21 @@ impl App { }; let input = PathBuf::from(&self.input); let output = PathBuf::from(&self.output); - let neutral = self.neutral; + if !output + .extension() + .and_then(|extension| extension.to_str()) + .is_some_and(|extension| { + extension.eq_ignore_ascii_case(self.export_choice.file_extension()) + }) + { + self.error = Some(format!( + "{} requires a .{} filename", + self.export_choice.label(), + self.export_choice.file_extension() + )); + return; + } + let neutral = self.export_choice == ExportChoice::NeutralJson; let overwrite = self.overwrite; let tx = self.begin(); let cancel = self.cancel.clone(); @@ -665,14 +725,37 @@ impl App { ui.label("Save to"); ui.add(egui::TextEdit::singleline(&mut self.output).desired_width(f32::INFINITY)); ui.horizontal(|ui| { + let mut choice = self.export_choice; + egui::ComboBox::from_id_salt("export_format") + .selected_text(self.export_choice.label()) + .show_ui(ui, |ui| { + for option in [ + ExportChoice::OntimeJson, + ExportChoice::OntimeXlsx, + ExportChoice::NeutralJson, + ] { + ui.selectable_value(&mut choice, option, option.label()); + } + }); + self.change_export_choice(choice); if ui.button("Change…").clicked() - && let Some(p) = rfd::FileDialog::new() - .set_file_name("rundown.ontime.json") - .save_file() + && let Some(p) = { + let mut dialog = rfd::FileDialog::new().set_file_name( + PathBuf::from(&self.output) + .file_name() + .unwrap_or_default() + .to_string_lossy(), + ); + dialog = if self.export_choice == ExportChoice::OntimeXlsx { + dialog.add_filter("Excel workbook", &["xlsx"]) + } else { + dialog.add_filter("JSON", &["json"]) + }; + dialog.save_file() + } { self.output = p.to_string_lossy().into(); } - ui.checkbox(&mut self.neutral, "Neutral JSON instead of Ontime"); }); ui.checkbox(&mut self.overwrite, "Replace existing output"); ui.separator(); @@ -736,7 +819,11 @@ impl App { ui.ctx().copy_text(p.display().to_string()); } }); - ui.label("Import the .ontime.json project in Ontime. Review the schedule before running the show."); + ui.label(if p.extension().and_then(|extension| extension.to_str()) == Some("xlsx") { + "Import the .xlsx file with Ontime's spreadsheet importer and map the displayed columns. Review the schedule before running the show." + } else { + "Import the .ontime.json project in Ontime. Review the schedule before running the show." + }); } else { ui.label("No export published yet."); } @@ -986,6 +1073,31 @@ mod tests { wait(&mut app); } + #[test] + fn export_choice_updates_default_output_extension() { + let mut app = App::default(); + app.set_input(PathBuf::from("show.csv")); + assert!(app.output.ends_with("show.ontime.json")); + app.change_export_choice(ExportChoice::OntimeXlsx); + assert!(app.output.ends_with("show.ontime.xlsx")); + assert_eq!(app.export_choice.label(), "Ontime spreadsheet (.xlsx)"); + app.change_export_choice(ExportChoice::NeutralJson); + assert!(app.output.ends_with("show.neutral.json")); + app.output = "show.xlsx".into(); + app.analysis = Some(pipeline::analyze( + &reader::read(&fixture()).unwrap()[0], + &Profile::default(), + &AtomicBool::new(false), + |_, _| {}, + )); + app.export(); + assert_eq!( + app.error.as_deref(), + Some("Neutral intermediate representation (.json) requires a .json filename") + ); + assert!(app.rx.is_none()); + } + #[test] fn multiple_sheets_focus_the_picker_and_explain_blocked_analysis() { let mut app = App::default(); diff --git a/crates/rundown-converter/src/main.rs b/crates/rundown-converter/src/main.rs index 7075f7b..02c1825 100644 --- a/crates/rundown-converter/src/main.rs +++ b/crates/rundown-converter/src/main.rs @@ -12,7 +12,7 @@ enum Action { #[derive(Parser)] #[command( version, - about = "Infer spreadsheet rundown columns, preserve schedule gaps, export Ontime JSON" + about = "Infer spreadsheet rundown columns, preserve schedule gaps, export Ontime JSON or XLSX" )] struct Args { #[arg(value_enum)] @@ -24,6 +24,7 @@ struct Args { /// JSON profile: explicit columns, units, row classifications and row corrections. #[arg(long)] profile: Option, + /// Destination .json or Ontime-importable .xlsx file. #[arg(short, long)] output: Option, /// Export the neutral IR instead of an Ontime project. diff --git a/crates/rundown-converter/tests/cli.rs b/crates/rundown-converter/tests/cli.rs index 1406d25..b8ae588 100644 --- a/crates/rundown-converter/tests/cli.rs +++ b/crates/rundown-converter/tests/cli.rs @@ -31,6 +31,16 @@ fn inspect_validate_convert_and_exit_codes() { .output() .unwrap(); assert!(result.status.success(), "{:?}", result); + let spreadsheet = dir.path().join("show.xlsx"); + let result = cli() + .arg("convert") + .arg(sample()) + .arg("--output") + .arg(&spreadsheet) + .output() + .unwrap(); + assert!(result.status.success(), "{:?}", result); + assert!(std::fs::read(spreadsheet).unwrap().starts_with(b"PK")); let result = cli() .arg("convert") .arg(sample()) diff --git a/crates/rundown-converter/tests/conversion.rs b/crates/rundown-converter/tests/conversion.rs index 13c58c3..2280e5c 100644 --- a/crates/rundown-converter/tests/conversion.rs +++ b/crates/rundown-converter/tests/conversion.rs @@ -409,6 +409,60 @@ fn atomic_publication_preserves_source_and_existing_output() { 1 ); } + +#[test] +fn xlsx_export_has_ontime_columns_notes_metadata_and_clock_strings() { + let source = table(&[ + &["Start", "Duration", "Title", "Notes", "Video"], + &["09:00", "5 min", "Opening", "Ready & ", "Slides"], + &["09:10", "2 min", "Closing", "", "Camera"], + ]); + let profile = Profile { + column_usage: BTreeMap::from([ + ("D".into(), "notes".into()), + ("E".into(), "metadata".into()), + ]), + ..Profile::default() + }; + let analysis = analyze(&source, &profile); + assert!( + analysis.diagnostics.is_empty(), + "{:?}", + analysis.diagnostics + ); + let bytes = export::xlsx(analysis.rundown.as_ref().unwrap()).unwrap(); + assert!(bytes.starts_with(b"PK")); + + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("ontime.xlsx"); + std::fs::write(&path, bytes).unwrap(); + let tables = reader::read(&path).unwrap(); + assert_eq!(tables.len(), 1); + assert_eq!(tables[0].name, "Ontime"); + assert_eq!( + tables[0].rows[0] + .iter() + .map(|cell| cell.text.as_str()) + .collect::>(), + [ + "Start", + "End", + "Duration", + "Cue", + "Title", + "Note", + "Timer type", + "Video", + ] + ); + assert_eq!(tables[0].rows[1][0].text, "09:00:00"); + assert_eq!(tables[0].rows[1][1].text, "09:05:00"); + assert_eq!(tables[0].rows[1][2].text, "00:05:00"); + assert_eq!(tables[0].rows[1][5].text, "Ready & "); + assert_eq!(tables[0].rows[1][6].text, "count-down"); + assert_eq!(tables[0].rows[1][7].text, "Slides"); + assert_eq!(tables[0].rows[2][0].text, "09:10:00"); +} #[test] fn invalid_or_cancelled_analysis_never_exports() { let a = pipeline::analyze( diff --git a/docs/rundown-converter-plan.md b/docs/rundown-converter-plan.md index 28e295b..05c9863 100644 --- a/docs/rundown-converter-plan.md +++ b/docs/rundown-converter-plan.md @@ -178,12 +178,10 @@ published. Keep local source paths out of exported project notes by default. ### Additional exports Generate a Typst report (editable source and rendered PDF) from the validated IR. -Re-export XLS/ODS spreadsheets with explicit, normalized columns suitable for -Ontime import. Verify accepted file types and exact header/unit requirements -against the target Ontime release before claiming direct import support; if a -requested format needs conversion, document that limitation explicitly. Include -round-trip import tests for timing and gaps. Keep untimed gaps free of timer rows -in every output. These exports do not become intermediate steps for JSON export. +XLSX re-export provides explicit, normalized columns suitable for Ontime import; +ODS remains unavailable because Ontime's current file importer does not expose +ODS upload. Keep untimed gaps free of timer rows in every output. These exports +do not become intermediate steps for JSON export. ### Optional buffer adjustment @@ -301,8 +299,9 @@ errors must remain visible with direct corrective actions. production GUI coverage and reporter behavior. Record unavailable manual checks. - [ ] Extend manual packaging/release preparation for both binaries and supported platforms, including only dependencies this tool needs. CI must not publish releases. -- [ ] Stretch: add Typst source/PDF reports and XLS/ODS re-export with verified - Ontime-compatible columns and documented format support. +- [x] Stretch: add XLSX re-export with verified Ontime-compatible columns and + documented format support. +- [ ] Stretch: add Typst source/PDF reports. - [ ] Stretch: add explicit buffer adjustment, before/after review, feasibility checks and revalidation, with the cases listed above. - [ ] Commit each completed implementation task with its validation summary. diff --git a/docs/rundown-converter.md b/docs/rundown-converter.md index f2b3efb..b65adde 100644 --- a/docs/rundown-converter.md +++ b/docs/rundown-converter.md @@ -3,7 +3,7 @@ Status: first implementation, available from source. A Rust core powers both the CLI and egui desktop app, styled like the [PPTX reporter](rust-reporter.md). It reads XLSX and UTF-8 CSV/TSV, infers columns, preserves untimed gaps and exports -Ontime JSON. Ambiguous documents stop for clarification. See the +Ontime JSON or normalized XLSX. Ambiguous documents stop for clarification. See the [development plan](rundown-converter-plan.md) and [sample findings](rundown-corpus.md). ## Quick start @@ -15,6 +15,7 @@ cargo run --locked -p rundown-converter --bin rundown-converter-gui cargo run --locked -p rundown-converter --bin rundown-converter -- inspect show.xlsx cargo run --locked -p rundown-converter --bin rundown-converter -- validate show.xlsx --sheet "Main" cargo run --locked -p rundown-converter --bin rundown-converter -- convert show.xlsx --sheet "Main" --output show.ontime.json +cargo run --locked -p rundown-converter --bin rundown-converter -- convert show.xlsx --sheet "Main" --output show.ontime.xlsx ``` Try the included synthetic lunch example: @@ -26,8 +27,9 @@ cargo run --locked -p rundown-converter --bin rundown-converter -- convert crate Select a sheet explicitly when a workbook has several. `inspect --json` exposes original cells, formulas, formats relevant to timing, and merged ranges. `validate --json` gives structured decisions, diagnostics and the IR when available. -`convert --neutral` exports that neutral IR instead of Ontime JSON. Existing output -requires `--overwrite`; the source itself cannot be the output. Parent output +The output extension selects native `.json` or spreadsheet `.xlsx` export. +`convert --neutral` exports the neutral IR to JSON instead of an Ontime project. +Existing output requires `--overwrite`; the source itself cannot be the output. Parent output directories must already exist. Exit codes: 0 success, 1 read/profile/write failure, 2 unresolved rundown diagnostics (also used by clap for invalid command syntax). CLI logs are plain text on stderr; JSON results go to stdout. @@ -169,8 +171,9 @@ Open or drop a document and select its sheet/table, then follow five stages: and inferred values. Ask for clarification when required facts remain unknown. 4. Check the complete schedule for inconsistencies, including backward times, negative durations, overlaps and contradictory start/end/duration values. -5. Generate native Ontime JSON from the validated IR, with optional neutral JSON - for inspection or reuse. Open the result or its output folder. +5. Generate native Ontime JSON or an Ontime-ready XLSX from the validated IR, + with optional neutral JSON for inspection or reuse. Open the result or its + output folder. Review and correction controls expose each stage's decisions and errors. CLI and GUI follow this same sequence; export cannot bypass unresolved errors. @@ -225,13 +228,15 @@ operator will start an event on time. Native Ontime JSON is the primary target: it lets the exporter control time relationships and project structure without a second column-mapping step. -Ontime supports project files and spreadsheet interchange; see its -[import/export documentation](https://docs.getontime.no/quick-tips/import-export/). -Stretch goals are a Typst report (editable source and PDF) and XLS/ODS re-export -with normalized columns directly suitable for Ontime import. Actual file-format -and column compatibility must be verified against the target release; any required -format conversion will be documented. These outputs share the validated IR and -are not intermediate steps for native JSON export. +XLSX export writes one `Ontime` worksheet with `Start`, `End`, `Duration`, `Cue`, +`Title`, `Note` and `Timer type` columns, followed by selected metadata columns. +Times use `HH:MM:SS` strings accepted by Ontime. Import the file with Ontime's +[spreadsheet importer](https://docs.getontime.no/features/import-spreadsheet/) +and map the displayed columns. Schedule gaps remain visible through absolute +event starts; the exporter does not invent timer rows for them. Ontime's current +file importer accepts XLSX and does not expose ODS upload, so ODS is not generated. +These outputs share the validated IR and are not intermediate steps for native +JSON export. A Typst source/PDF report remains a stretch goal. The adapter targets Ontime 4 native project JSON. Its events and schedule metadata were checked with the Ontime 4.15.1 native parser; see the