oxedyne/fe2o3/fe2o3_file/dev/docx_oracle.sh
1.9 KiB, 1 run, executable
created by r1870400018:22572, 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 | #!/usr/bin/env bash |
| 2 | # Puts a .docx this crate wrote to an external reader, and says what came back. |
| 3 | # |
| 4 | # A test that reads back what this crate wrote proves that this crate agrees with itself, which is |
| 5 | # worth very little for a format whose whole purpose is to be opened by somebody else's program. |
| 6 | # LibreOffice is that somebody else. It is not Microsoft Word -- where the two disagree, the right |
| 7 | # answer is unknown and the right response is to preserve rather than to choose -- but it is a second |
| 8 | # implementation's opinion, which is the only kind of evidence there is here. |
| 9 | # |
| 10 | # fe2o3_file/dev/docx_oracle.sh <markdown file> |
| 11 | # |
| 12 | # Needs `soffice` on the path. |
| 13 | |
| 14 | set -eu |
| 15 | |
| 16 | md="${1:?usage: docx_oracle.sh <markdown file>}" |
| 17 | # Not /tmp. LibreOffice writes a whole user profile into HOME, and on a machine where /tmp is a tmpfs |
| 18 | # that profile is held in RAM and charged to whoever ran this. |
| 19 | work="$(mktemp -d "${XDG_CACHE_HOME:-$HOME/.cache}/docx-oracle.XXXXXX")" |
| 20 | trap 'rm -rf "$work"' EXIT |
| 21 | |
| 22 | root="$(cd "$(dirname "$0")/../.." && pwd)" |
| 23 | target="${CARGO_TARGET_DIR:-$HOME/.cache/cargo-targets/${RC_SLOT:-solo}/fe2o3}" |
| 24 | |
| 25 | CARGO_TARGET_DIR="$target" cargo run -q -p oxedyne_fe2o3_file --example make_docx \ |
| 26 | --manifest-path "$root/Cargo.toml" -- "$md" "$work/made.docx" |
| 27 | |
| 28 | echo "-- the archive --" |
| 29 | unzip -t "$work/made.docx" |
| 30 | |
| 31 | echo "-- what LibreOffice read back --" |
| 32 | HOME="$work" soffice --headless --convert-to txt:Text --outdir "$work" "$work/made.docx" >/dev/null |
| 33 | cat "$work/made.txt" |
| 34 | |
| 35 | echo "-- the structure it recovered --" |
| 36 | HOME="$work" soffice --headless --convert-to fodt --outdir "$work" "$work/made.docx" >/dev/null |
| 37 | echo -n "heading levels: "; grep -o 'text:outline-level="[0-9]"' "$work/made.fodt" | sort -u | tr '\n' ' '; echo |
| 38 | echo -n "links: "; grep -o 'xlink:href="[^"]*"' "$work/made.fodt" | tr '\n' ' '; echo |
| 39 | echo -n "alignments: "; grep -o 'fo:text-align="[^"]*"' "$work/made.fodt" | sort | uniq -c | tr '\n' ' '; echo |