Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_file/examples/make_odf.rs

2.5 KiB, 5 runs

created by r1870400018:22901, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! Writes the three OpenDocument formats from one Markdown file, so an external reader can be asked
2//! what it makes of each.
3//!
4//! ```text
5//! cargo run -p oxedyne_fe2o3_file --example make_odf -- in.md out_prefix
6//! soffice --headless --convert-to txt out_prefix.odt
7//! ```
8//!
9//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
10//! Anthropic Claude
11
12use oxedyne_fe2o3_core::prelude::*;
13use oxedyne_fe2o3_file::office::deck::Deck;
14use oxedyne_fe2o3_file::office::odf;
15use oxedyne_fe2o3_file::office::sheet::{Book, Cell, Sheet, Value};
16use oxedyne_fe2o3_text::doc::markdown;
17
18fn main() -> Outcome<()> {
19 let args: Vec<String> = std::env::args().collect();
20 if args.len() != 3 {
21 return Err(err!("Usage: make_odf <markdown in> <output prefix>"; Invalid, Input));
22 }
23 let src = res!(std::fs::read_to_string(&args[1]), IO, File);
24 let doc = res!(markdown::parse(&src));
25
26 let (odt, left) = res!(odf::text::write(&doc));
27 res!(std::fs::write(fmt!("{}.odt", args[2]), &odt), IO, File);
28 println!("{}.odt: {} bytes", args[2], odt.len());
29 for s in &left.images {
30 println!(" not carried: the image at {}", s);
31 }
32
33 let (odp, left) = res!(odf::slides::write(&Deck::from_doc(&doc)));
34 res!(std::fs::write(fmt!("{}.odp", args[2]), &odp), IO, File);
35 println!("{}.odp: {} bytes", args[2], odp.len());
36 if left.notes > 0 {
37 println!(" not carried: speaker's notes on {} slide(s)", left.notes);
38 }
39
40 // A spreadsheet has no Markdown to come from, so it is built here: one of everything that
41 // separates the format from a table.
42 let mut s = Sheet::new("Sales");
43 s.rows.push(vec![Cell::text("Region"), Cell::text("Units"), Cell::text("Price"),
44 Cell::text("Total"), Cell::text("Booked"), Cell::text("Active")]);
45 s.rows.push(vec![Cell::text("North"), Cell::number(120.0), Cell::number(3.4),
46 Cell::formula("B2*C2", Value::Number(408.0)),
47 Cell { value: Value::Date("2026-03-14".to_string()), formula: None },
48 Cell::bool(true)]);
49 s.rows.push(vec![Cell::text("South"), Cell::number(85.0), Cell::number(11.0),
50 Cell::formula("B3*C3", Value::Number(935.0)),
51 Cell { value: Value::Date("2026-04-01".to_string()), formula: None },
52 Cell::bool(false)]);
53 s.rows.push(Vec::new());
54 s.rows.push(vec![Cell::text("Total"), Cell::empty(), Cell::empty(),
55 Cell::formula("SUM(D2:D3)", Value::Number(1343.0))]);
56 let ods = res!(odf::sheet::write(&Book { sheets: vec![s] }));
57 res!(std::fs::write(fmt!("{}.ods", args[2]), &ods), IO, File);
58 println!("{}.ods: {} bytes", args[2], ods.len());
59 Ok(())
60}