ooxml: OOXML packages
| (require ooxml) | package: ooxml |
The "ooxml" package works with the container layer of ECMA-376 documents — .docx, .pptx and .xlsx files, which are zip archives of XML parts wired together by relationships. It does not know about WordprocessingML or DrawingML; it gives you the parts, their relationships, content types and a validator, and leaves the XML to you.
Everything is implemented in pure Racket: no FFI, no system dependency.
1 Quick start
> (define parts (hash "[Content_Types].xml" (string->bytes/utf-8 (content-types-xml '(("rels" . "application/vnd.openxmlformats-package.relationships+xml") ("xml" . "application/xml")) '(("word/document.xml" . "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml")))) "_rels/.rels" (string->bytes/utf-8 (rels-xml (list (make-rel "rId1" 'officeDocument "word/document.xml")))) "word/document.xml" #"<w:document/>" "word/media/orphan.png" #"...")) > (package-reachable parts) '("" "_rels/.rels" "word/document.xml")
> (check-package parts) '("no content type for word/media/orphan.png")
Parts are kept in a plain hash so that they stay inspectable; read-package produces one straight from a file.
2 Package operations
A part table is a hash? from part name (string?) to bytes?. Part names are /-separated, have no leading slash, and name the package root "".
procedure
(part-name? v) → boolean?
v : any/c
procedure
(read-package path) → (hash/c string? bytes?)
path : path-string?
procedure
(write-package out-path parts [ #:compress compress]) → void? out-path : path-string? parts : (hash/c string? bytes?)
compress : (or/c 'deflate 'store (-> string? (or/c 'deflate 'store))) = default-compress
3 Relationships
Relationships follow ECMA-376 Part 2, §9. The relationships of a part live in a _rels directory next to it: "word/document.xml" owns "word/_rels/document.xml.rels", and the package root "" owns "_rels/.rels".
struct
id : string? type : (or/c symbol? string?) target : string? mode : (or/c 'internal 'external)
procedure
id : string? type : (or/c symbol? string?) target : string? mode : (or/c 'internal 'external) = 'internal
procedure
(rel-type-uri type) → string?
type : (or/c symbol? string?)
procedure
(owner-of-rels rels-part) → string?
rels-part : string?
procedure
(parse-rels xml) → (listof rel?)
xml : string?
procedure
(resolve-target from-part target) → string?
from-part : string? target : string?
4 Reachability and content types
procedure
(parse-content-types xml)
→ (hash/c symbol? (listof (cons/c string? string?))) xml : string?
procedure
(content-types-xml defaults overrides) → string?
defaults : (listof (cons/c string? string?)) overrides : (listof (cons/c string? string?))
procedure
(content-type-of ct name) → (or/c string? #f)
ct : (hash/c symbol? (listof (cons/c string? string?))) name : string?
procedure
(rebuild-content-types parts) → bytes?
parts : (hash/c string? bytes?)
5 Validation
6 Zip layer
procedure
path : path-string?
procedure
(zip->entries path) → (listof zip-entry?)
path : path-string?
procedure
(zip-write! out-path entries [ #:compress compress]) → void? out-path : path-string? entries : (listof (or/c zip-entry? (list/c string? bytes?)))
compress : (or/c 'deflate 'store (-> string? (or/c 'deflate 'store))) = 'deflate
procedure
(zip-entries->bytes entries [ #:compress compress]) → bytes? entries : (listof (or/c zip-entry? (list/c string? bytes?)))
compress : (or/c 'deflate 'store (-> string? (or/c 'deflate 'store))) = 'deflate
procedure
(crc32 data) → exact-nonnegative-integer?
data : bytes?
7 License
MIT.