Documentation
¶
Overview ¶
Package gofastodf extracts OpenDocument files into the shared document model of go-fast-core (github.com/AOShei/go-fast-core). It uses the standard library and go-fast-core only.
What is read ¶
Text documents (odt, and the template ott, master document odm, master template otm and web page oth), spreadsheets (ods, ots), presentations (odp, otp), and the flat XML forms fodt, fods and fodp. The format is decided from the file (its mimetype member or manifest, a flat file's office:mimetype, or the body element of its content), never from a file name. Templates and master documents are reported as odt, ods or odp; flat files as fodt, fods or fodp.
Encrypted packages are decrypted with Options.Password: AES-CBC with a PBKDF2 key (ODF 1.2 and 1.3), the encrypted-package member of ODF 1.4 (AES-GCM with an Argon2id key), and Blowfish CFB (ODF 1.0 and 1.1). Without a password an encrypted file gives ErrPasswordRequired.
Drawings (odg, otg, fodg), charts (odc), formulas (odf), images (odi) and databases (odb) as documents in their own right, and the OpenOffice.org 1 formats (sxw, sxc, sxi, sxd, sxm, sxg and the templates stw, stc, sti, std), give ErrUnsupported, as does a file whose media type is not OpenDocument's and whose content holds no OpenDocument body.
Use ¶
doc, err := gofastodf.ExtractFile(ctx, "report.odt", nil)
if err != nil {
return err
}
fmt.Println(doc.Markdown())
Extract reads from an io.ReaderAt of a known size, ExtractFile opens a file, and ExtractBytes reads a file held in memory. Each uses a zero Extractor; an Extractor's own fields set the limits only this library has (archive members, table cells). Every other setting and limit is in gofastcore.Options. Errors wrap the errors of gofastcore.
Parts ¶
A text document gives one part of kind body, numbered 1. A spreadsheet gives a part of kind sheet for each sheet, a presentation a part of kind slide for each slide, numbered from 1. When the whole document is read (Options.Parts unset) and it keeps values it never shows (user field declarations, named ranges, form controls placed nowhere), a last part of kind stored holds them. Embedded documents are handed to Options.Child and listed in Document.Children.
Display ¶
Everything is extracted. The Display of a block, span, table region or part records only what the file itself declares: hidden (text:display "none" in a style or field, hidden sections and paragraphs, collapsed or filtered rows and columns, hidden sheets and slides, draw:display "none" on a frame, shape or layer, a header or footer switched off) and no_ink (an opacity of 0 on a definite font colour). Colour, size, position and overlap are not judged, and conditions are never evaluated.
Index ¶
- Constants
- func Extract(ctx context.Context, r io.ReaderAt, size int64, opts *gofastcore.Options) (*gofastcore.Document, error)
- func ExtractBytes(ctx context.Context, data []byte, opts *gofastcore.Options) (*gofastcore.Document, error)
- func ExtractFile(ctx context.Context, path string, opts *gofastcore.Options) (*gofastcore.Document, error)
- type Extractor
Constants ¶
const ( // DefaultMaxInputBytes bounds the size of the file. DefaultMaxInputBytes = 1 << 30 // DefaultMaxDecodedBytes bounds the decompressed bytes read from the // archive, over all its members. It is what stops a decompression // bomb. DefaultMaxDecodedBytes = 2 << 30 // DefaultMaxArchiveMembers bounds the number of files in the archive. DefaultMaxArchiveMembers = 100_000 // DefaultMaxCells bounds the table cells kept from a document. Each // cell kept costs memory whatever the size of the file; repetition // attributes let a few bytes ask for millions. DefaultMaxCells = 20_000_000 )
Defaults for the limits of gofastcore.Options.Limits, used where a limit is zero. A negative limit means no limit.
const ( // WarnUnreadContent was odf.unread_content, raised when the document holds content of a kind this version does not read. // // Deprecated: the event is go-fast-core's shared code input.unread_content // (gofastcore.WarnInputUnreadContent) since v0.2.0; this name has that value. WarnUnreadContent = gofastcore.WarnInputUnreadContent // WarnRegionSplit was odf.region_split, raised when a sparse area of a sheet was written as one-row tables. // // Deprecated: the event is go-fast-core's shared code table.region_split // (gofastcore.WarnTableRegionSplit) since v0.2.0; this name has that value. WarnRegionSplit = gofastcore.WarnTableRegionSplit // WarnFormulaNoValue was odf.formula_no_value, raised when formula cells have no stored result. // // Deprecated: the event is go-fast-core's shared code table.formula_no_value // (gofastcore.WarnTableFormulaNoValue) since v0.2.0; this name has that value. WarnFormulaNoValue = gofastcore.WarnTableFormulaNoValue // WarnPartDamaged was odf.part_damaged, raised when a supporting stream (manifest, styles, metadata) could not be read to its end. // // Deprecated: the event is go-fast-core's shared code input.stream_damaged // (gofastcore.WarnInputStreamDamaged) since v0.2.0; this name has that value. WarnPartDamaged = gofastcore.WarnInputStreamDamaged // WarnPropertyTruncated was raised by v0.1.1 and earlier when a // metadata value was cut at 64 KiB or keywords or user-defined // properties past 256 were left out. // // Deprecated: these cuts are reported as gofastcore.WarnLimitExceeded, // as every bound that cuts output is; this code is no longer raised. WarnPropertyTruncated = "odf.property_truncated" // WarnControlCharsRemoved was odf.control_characters_removed, raised when control characters with no meaning as text were removed. // // Deprecated: the event is go-fast-core's shared code text.control_characters_removed // (gofastcore.WarnTextControlCharactersRemoved) since v0.2.0; this name has that value. WarnControlCharsRemoved = gofastcore.WarnTextControlCharactersRemoved )
Warning codes specific to this library. Each states a fact about the file. Of the codes shared by all go-fast libraries this library raises, besides those about parts and limits:
input.active_content the package holds macros (Basic or other
scripts), event handlers, a link that runs
a macro, or an applet
input.external_reference a linked picture, a section linked to
another file, a plug-in or floating frame,
an instruction to reload from an address
input.repaired the archive's directory could not be read;
the members were found by their own headers
input.ambiguous the archive names a member twice, in two
ways, or has two members with one name
text.tracked_changes the document has tracked changes
child.failed an embedded file could not be taken out
of the package to be handed on
structure.heading_level_clamped
What the file declares about display is on the blocks, spans, table regions and parts (gofastcore.Display), not in warnings.
Variables ¶
This section is empty.
Functions ¶
func Extract ¶
func Extract(ctx context.Context, r io.ReaderAt, size int64, opts *gofastcore.Options) (*gofastcore.Document, error)
Extract reads an OpenDocument file with default settings. It is (&Extractor{}).Extract.
Types ¶
type Extractor ¶
type Extractor struct {
// MaxArchiveMembers bounds the number of files in the zip archive.
// Zero means DefaultMaxArchiveMembers; negative means no limit.
MaxArchiveMembers int
// MaxCells bounds the table cells kept from a document. A table that
// reaches it is cut off there, with a limit.exceeded warning. Zero
// means DefaultMaxCells; negative means no limit.
MaxCells int64
}
Extractor extracts OpenDocument files. The zero value is ready to use and safe for concurrent use. Its fields are the settings only this library has; everything else is in gofastcore.Options.
func (*Extractor) Extract ¶
func (e *Extractor) Extract(ctx context.Context, r io.ReaderAt, size int64, opts *gofastcore.Options) (*gofastcore.Document, error)
Extract reads size bytes from r and returns the document.
The input is a package (a zip archive) or a flat file (one XML document). The format is decided from the file itself (its mimetype member, its manifest, the office:mimetype of a flat file, or the body of its content), never from a file name. An error wraps one of the gofastcore errors:
- ErrMalformed: neither a zip archive nor XML, or a package too damaged to read.
- ErrUnsupported: a zip archive or XML document that is not OpenDocument; an OpenDocument file of a kind this version does not read (drawings, charts, formulas, images, databases, the OpenOffice.org 1 formats sxw, sxc, sxi, sxd, sxm, sxg and their templates); a file whose media type is not OpenDocument's and whose content.xml has no OpenDocument body; an encryption this version does not decrypt (an algorithm, key derivation or checksum other than AES-CBC, AES-GCM, Blowfish CFB, PBKDF2, Argon2id, SHA-1 and SHA-256).
- ErrPasswordRequired: the file is encrypted and Options.Password is empty.
- ErrWrongPassword: the file is encrypted and its checksum or authentication tag does not match Options.Password.
- ErrLimitExceeded: the input is larger than Limits.MaxInputBytes, or a limit was reached before anything could be read.
- ErrNoPartsSelected: Options.Parts selects none of the parts.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
gofastodf
command
Command gofastodf extracts OpenDocument files (odt, ods, odp and the flat fodt, fods, fodp) into the shared document model of the go-fast libraries and prints them as JSON, Markdown or plain text.
|
Command gofastodf extracts OpenDocument files (odt, ods, odp and the flat fodt, fods, fodp) into the shared document model of the go-fast libraries and prints them as JSON, Markdown or plain text. |
|
internal
|
|
|
argon2
Package argon2 implements Argon2id (RFC 9106, version 0x13) and the BLAKE2b it is built on (RFC 7693), for deriving the keys of encrypted OpenDocument packages.
|
Package argon2 implements Argon2id (RFC 9106, version 0x13) and the BLAKE2b it is built on (RFC 7693), for deriving the keys of encrypted OpenDocument packages. |
|
blowfish
Package blowfish implements the Blowfish block cipher (Schneier, 1993) and its 64-bit CFB mode, for the encrypted packages of ODF 1.0 and 1.1.
|
Package blowfish implements the Blowfish block cipher (Schneier, 1993) and its 64-bit CFB mode, for the encrypted packages of ODF 1.0 and 1.1. |
|
clean
Package clean turns bytes read from a file into strings that obey the text conventions of the shared document model: valid UTF-8, no control characters other than line feed and tab, and for prose no surrounding white space and no presentation-form ligatures.
|
Package clean turns bytes read from a file into strings that obey the text conventions of the shared document model: valid UTF-8, no control characters other than line feed and tab, and for prose no surrounding white space and no presentation-form ligatures. |
|
display
Package display settles the display reasons a file declares for stretches of a paragraph (text:display="none", no ink) onto the paragraph's block.
|
Package display settles the display reasons a file declares for stretches of a paragraph (text:display="none", no ink) onto the paragraph's block. |
|
meta
Package meta reads an OpenDocument meta.xml stream into the shared metadata.
|
Package meta reads an OpenDocument meta.xml stream into the shared metadata. |
|
ns
Package ns numbers the XML namespaces of OpenDocument for xmlscan.
|
Package ns numbers the XML namespaces of OpenDocument for xmlscan. |
|
odpkg
Package odpkg reads the container of an OpenDocument file: a zip archive holding a mimetype member, a manifest (META-INF/manifest.xml) that lists the members with their media types and, for an encrypted file, how each is encrypted, and the XML streams themselves.
|
Package odpkg reads the container of an OpenDocument file: a zip archive holding a mimetype member, a manifest (META-INF/manifest.xml) that lists the members with their media types and, for an encrypted file, how each is encrypted, and the XML streams themselves. |
|
odt
Package odt reads the body of an OpenDocument document into parts and blocks: a text document (office:text) as one body part of headings, paragraphs, lists, tables, frames and text boxes in reading order, images, notes, comments, tracked deletions, and the headers and footers of its master pages; a spreadsheet (office:spreadsheet) as one part per sheet, its used regions as tables; a presentation (office:presentation) as one part per slide, with the text of its master page and its speaker notes.
|
Package odt reads the body of an OpenDocument document into parts and blocks: a text document (office:text) as one body part of headings, paragraphs, lists, tables, frames and text boxes in reading order, images, notes, comments, tracked deletions, and the headers and footers of its master pages; a spreadsheet (office:spreadsheet) as one part per sheet, its used regions as tables; a presentation (office:presentation) as one part per slide, with the text of its master page and its speaker notes. |
|
style
Package style reads the styles of an OpenDocument file (styles.xml and the automatic styles of each stream) as far as the extractor needs them: outline levels, list styles, the "Horizontal Line" style, what the file declares about display (text:display="none", a font colour with no opacity, hidden sheets, slides and layers), and emphasis (weight, slant, lines, position).
|
Package style reads the styles of an OpenDocument file (styles.xml and the automatic styles of each stream) as far as the extractor needs them: outline levels, list styles, the "Horizontal Line" style, what the file declares about display (text:display="none", a font colour with no opacity, hidden sheets, slides and layers), and emphasis (weight, slant, lines, position). |
|
xmlscan
Package xmlscan is a pull tokenizer for XML, built for reading OpenDocument parts quickly and safely.
|
Package xmlscan is a pull tokenizer for XML, built for reading OpenDocument parts quickly and safely. |