mirror of
https://github.com/storytold/indd-utils.git
synced 2026-11-22 10:10:47 +01:00
feat: validate output against the IDML schemas
Add tools/validate.sh, which checks every XML part of an IDML package against InDesign's generated RelaxNG schemas with Jing, and a --schemas option in compare.py. Output for all 75 corpus pairs validates. Do not write BindingLocation on master spreads, which the schema does not allow.
This commit is contained in:
1 parent
f6fb4e7ae4
commit
602065453e
4 files changed
+67
-5
No files matched your search
@@ -41,4 +41,13 @@ the INDD version were probably re-saved after export; trust them less.
|
||||
## Commands
|
||||
|
||||
- `cargo test` (all tests), `cargo clippy --all-targets`, `cargo fmt`.
|
||||
- `cargo run -q -- info <file>`.
|
||||
- `cargo run -q -- info <file>`, `indd convert in.indd out.idml`,
|
||||
`indd objects <file>`, `indd dump <file> <uid>...`.
|
||||
- `python3 -I tools/compare.py [--detail TAG --show N]`: convert every
|
||||
corpus pair and compare with the reference IDML. This is the main
|
||||
measure of progress; run it after every change.
|
||||
- Schema validation: `tools/validate.sh out.idml <schemas> <jing>`, or
|
||||
`compare.py --schemas <dir> --jing <dir>`. The IDML RelaxNG schemas
|
||||
(generated by InDesign, published in the metanorma/idml repo) and Jing
|
||||
(Maven Central) are kept outside the repo, for example in the session
|
||||
scratchpad; do not commit them.
|
||||
+5
-3
@@ -705,9 +705,11 @@ impl Writer<'_> {
|
||||
self.package_root(&mut x, kind);
|
||||
x.start(kind)
|
||||
.attr("Self", uref(Some(s.uid)))
|
||||
.attr("PageCount", s.pages.len().to_string())
|
||||
.attr("BindingLocation", s.binding_location.to_string())
|
||||
.attr("ItemTransform", matrix(&s.transform));
|
||||
.attr("PageCount", s.pages.len().to_string());
|
||||
if !master {
|
||||
x.attr("BindingLocation", s.binding_location.to_string());
|
||||
}
|
||||
x.attr("ItemTransform", matrix(&s.transform));
|
||||
let (prefix, base) = s
|
||||
.master_name
|
||||
.clone()
|
||||
|
||||
+16
-1
@@ -8,7 +8,9 @@ we produce (matched by Self), and per attribute how often our value
|
||||
matches. Also reports story text agreement.
|
||||
|
||||
Usage: python3 -I tools/compare.py [--limit N] [--detail TAG] [--file SUBSTR]
|
||||
Run from the repository root after `cargo build --release`.
|
||||
[--schemas DIR --jing DIR]
|
||||
Run from the repository root after `cargo build --release`. With --schemas
|
||||
and --jing, also validates every output with tools/validate.sh.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
@@ -127,6 +129,8 @@ def main():
|
||||
ap.add_argument("--detail", help="print attribute table for this tag")
|
||||
ap.add_argument("--file", help="only pairs whose path contains this")
|
||||
ap.add_argument("--show", type=int, default=0, help="show N mismatches per attribute")
|
||||
ap.add_argument("--schemas", help="IDML RelaxNG schema directory (validate output)")
|
||||
ap.add_argument("--jing", help="directory with jing.jar, isorelax.jar, saxon.jar")
|
||||
args = ap.parse_args()
|
||||
|
||||
found = Counter()
|
||||
@@ -135,6 +139,7 @@ def main():
|
||||
examples = defaultdict(list)
|
||||
story_ok = Counter()
|
||||
failures = []
|
||||
invalid = []
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
out = Path(tmp) / "out.idml"
|
||||
for indd, idml in pairs(args.limit, args.file):
|
||||
@@ -142,6 +147,12 @@ def main():
|
||||
if r.returncode != 0:
|
||||
failures.append((indd.name, r.stderr.strip()))
|
||||
continue
|
||||
if args.schemas and args.jing:
|
||||
v = subprocess.run(
|
||||
[ROOT / "tools" / "validate.sh", out, args.schemas, args.jing],
|
||||
capture_output=True, text=True)
|
||||
if v.returncode != 0:
|
||||
invalid.append((indd.name, v.stdout.strip().splitlines()[:3]))
|
||||
ref_el, ref_st, ref_rg = load(idml)
|
||||
our_el, our_st, our_rg = load(out)
|
||||
for (tag, s), el in ref_el.items():
|
||||
@@ -194,6 +205,10 @@ def main():
|
||||
print(f"conversion failures: {len(failures)}")
|
||||
for name, err in failures[:10]:
|
||||
print(f" {name}: {err}")
|
||||
if args.schemas:
|
||||
print(f"schema validation failures: {len(invalid)}")
|
||||
for name, errs in invalid[:10]:
|
||||
print(f" {name}: {errs}")
|
||||
st = sum(story_ok.values())
|
||||
print(f"story text: {story_ok['ok']}/{st} exact, {story_ok['wrong']} differ, {story_ok['missing']} missing")
|
||||
print("\nelements (produced / in reference):")
|
||||
|
||||
Executable
+36
@@ -0,0 +1,36 @@
|
||||
#!/bin/sh
|
||||
# Validate the XML parts of an IDML package against IDML RelaxNG compact
|
||||
# schemas (the package schemas InDesign generates with generateIDMLSchema).
|
||||
#
|
||||
# Usage: tools/validate.sh <file.idml> <schema-dir> <jing-dir>
|
||||
# schema-dir: directory with designmap.rnc, Spreads/Spread.rnc, ...
|
||||
# jing-dir: directory with jing.jar, isorelax.jar, saxon.jar
|
||||
# The schemas and Jing are not part of this repository.
|
||||
set -eu
|
||||
idml=$1; schemas=$2; jing=$3
|
||||
tmp=$(mktemp -d)
|
||||
trap 'rm -rf "$tmp"' EXIT
|
||||
unzip -qq "$idml" -d "$tmp"
|
||||
# The generated schemas pin DOMVersion to the generating InDesign's version;
|
||||
# validate structure regardless of the version written.
|
||||
dom=$(grep -o 'DOMVersion="[^"]*"' "$schemas/designmap.rnc" | head -1)
|
||||
[ -z "$dom" ] && dom=$(grep -o '"[0-9][0-9]*\.[0-9]"' "$schemas/designmap.rnc" | head -1 | sed 's/^/DOMVersion=/')
|
||||
find "$tmp" -name '*.xml' -exec sed -i "s/DOMVersion=\"[^\"]*\"/$dom/" {} +
|
||||
cp="$jing/jing.jar:$jing/isorelax.jar:$jing/saxon.jar"
|
||||
status=0
|
||||
check() { # file schema
|
||||
[ -f "$tmp/$1" ] || return 0
|
||||
if ! out=$(java -cp "$cp" com.thaiopensource.relaxng.util.Driver -c "$schemas/$2" "$tmp/$1" 2>&1); then
|
||||
status=1
|
||||
fi
|
||||
[ -n "$out" ] && printf '%s\n' "$out" | sed "s|$tmp/||"
|
||||
return 0
|
||||
}
|
||||
check designmap.xml designmap.rnc
|
||||
for f in "$tmp"/Spreads/*.xml; do [ -e "$f" ] && check "Spreads/$(basename "$f")" Spreads/Spread.rnc; done
|
||||
for f in "$tmp"/MasterSpreads/*.xml; do [ -e "$f" ] && check "MasterSpreads/$(basename "$f")" MasterSpreads/MasterSpread.rnc; done
|
||||
for f in "$tmp"/Stories/*.xml; do [ -e "$f" ] && check "Stories/$(basename "$f")" Stories/Story.rnc; done
|
||||
for p in Fonts Graphic Preferences Styles; do check "Resources/$p.xml" "Resources/$p.rnc"; done
|
||||
check XML/BackingStory.xml XML/BackingStory.rnc
|
||||
check XML/Tags.xml XML/Tags.rnc
|
||||
exit $status
|
||||
Reference in new issue
Block a user