oxedyne/fe2o3/fe2o3_file/examples/make_docx.rs
1.5 KiB, 5 runs
created by r1870400018:22575, 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 a `.docx` from a Markdown file, so the result can be put to an external reader. |
| 2 | //! |
| 3 | //! The point of this is not convenience. A test that reads back what this crate wrote proves that |
| 4 | //! this crate agrees with itself, which is worth very little for a format whose whole purpose is to |
| 5 | //! be opened by somebody else's program. So the file this makes is meant to be handed to LibreOffice |
| 6 | //! or to Word: |
| 7 | //! |
| 8 | //! ```text |
| 9 | //! cargo run -p oxedyne_fe2o3_file --example make_docx -- in.md out.docx |
| 10 | //! soffice --headless --convert-to txt out.docx |
| 11 | //! diff <(sed ... in.md) out.txt |
| 12 | //! ``` |
| 13 | //! |
| 14 | //! `dev/docx_oracle.sh` does exactly that and reports the difference. |
| 15 | //! |
| 16 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 17 | //! Anthropic Claude |
| 18 | |
| 19 | use oxedyne_fe2o3_core::prelude::*; |
| 20 | use oxedyne_fe2o3_file::office::docx; |
| 21 | use oxedyne_fe2o3_text::doc::markdown; |
| 22 | |
| 23 | fn main() -> Outcome<()> { |
| 24 | let args: Vec<String> = std::env::args().collect(); |
| 25 | if args.len() != 3 { |
| 26 | return Err(err!( |
| 27 | "Usage: make_docx <markdown in> <docx out>"; Invalid, Input)); |
| 28 | } |
| 29 | let src = res!(std::fs::read_to_string(&args[1]), IO, File); |
| 30 | let doc = res!(markdown::parse(&src)); |
| 31 | let (bytes, left) = res!(docx::write(&doc)); |
| 32 | res!(std::fs::write(&args[2], &bytes), IO, File); |
| 33 | println!("{}: {} bytes, {} blocks", args[2], bytes.len(), doc.blocks.len()); |
| 34 | // What did not fit is said rather than dropped in silence. |
| 35 | for src in &left.images { |
| 36 | println!("not drawn: the image at {}", src); |
| 37 | } |
| 38 | Ok(()) |
| 39 | } |