Documentation
¶
Overview ¶
Package gofasthtml extracts HTML, Markdown, plain text and EPUB into the shared go-fast document model, gofastcore.Document. An EPUB is a part for each chapter of its spine, read as XHTML. Markdown is parsed as CommonMark with the GFM tables, strikethrough, task lists and extended autolinks, rendered to HTML and read as a page is, so its raw HTML gets the same checks; blocks are located in the Markdown.
It uses the standard library and go-fast-core only, with an HTML5 tokenizer and tree builder of its own that recover from tag soup the way browsers do. An HTML page, a Markdown document or a plain text file is one part of kind body; an EPUB is one part of kind chapter for each chapter. Script and style text, hidden form values and, in an EPUB, images no chapter shows go to a part of kind stored after them when the whole document is read. Blocks carry their byte range in the source (Loc.Offset, Loc.Length) and the id of their element (Loc.Anchor).
Everything is extracted. Where the page declares, in an attribute or in inline style, that text is not displayed or has no ink, the blocks carry that reason (hidden, no_ink); nothing else about display is judged, and stylesheets and scripts are not evaluated. Script and style text is read as data into the stored part and never run. Nothing the page refers to is fetched. See the README for the mapping, the warnings and the limits.
Index ¶
Constants ¶
const ( // DefaultMaxArchiveMembers bounds the number of files in an EPUB's // zip archive. DefaultMaxArchiveMembers = 100_000 // DefaultMaxParts bounds the chapters read when Limits.MaxParts is // zero. DefaultMaxParts = 100_000 )
Defaults for EPUB.
const ( // WarnNonLinear: the chapter is marked linear="no" in the spine: a // reading system shows it only when it is linked to (notes, answers). // It is extracted in its place in the spine. WarnNonLinear = "epub.non_linear" // WarnEncrypted: resources of the publication are encrypted (DRM) // and were not read. Font obfuscation is not counted. WarnEncrypted = "epub.encrypted" // WarnMimetype: the archive's mimetype member is missing or does not // say application/epub+zip; the archive was read as an EPUB all the // same. WarnMimetype = "epub.mimetype" )
Warning codes raised while an EPUB is read.
const ( // DefaultMaxInputBytes bounds the size of the file. The whole file is // held in memory while it is read. DefaultMaxInputBytes = 256 << 20 // DefaultMaxDecodedBytes bounds the bytes decoded: the text the input // becomes in UTF-8, and the images given inline as data: URIs. DefaultMaxDecodedBytes = 1 << 30 // DefaultMaxDepth is how deeply elements are nested in the tree that // is read; see Extractor.MaxDepth. DefaultMaxDepth = dom.DefaultMaxDepth )
Defaults for the limits of gofastcore.Options.Limits, used where a limit is zero. A negative limit means no limit.
const ( // WarnCharsetUnsupported: the document declares a character encoding // this library does not decode, and was read as windows-1252, so // letters outside ASCII may be wrong; or Extractor.Charset names one, // and it was ignored. WarnCharsetUnsupported = "html.charset_unsupported" // WarnNestingFlattened: elements are nested deeper than the limit. // Below it, elements were placed beside each other instead of inside // each other; their text is all there, and the reasons it is hidden // are kept, but the blocks there may be joined or split differently. WarnNestingFlattened = "html.nesting_flattened" // WarnMetadataTruncated: a metadata value was longer than 64 KiB and // was cut there, or there were more than 256 authors or keyword // declarations and the rest were left out. WarnMetadataTruncated = "html.metadata_truncated" )
Warning codes specific to this library. Each states a fact about the file. The codes shared by all go-fast libraries are in gofastcore; this library raises these of them besides the ones about parts and limits (part.no_text, part.image_only, part.failed, limit.exceeded):
input.active_content the page has scripts, event handler attributes
or javascript: addresses; script text is read
as data into the stored part, and nothing is
run
input.external_reference the page loads resources from elsewhere:
pictures, stylesheets, scripts, frames, a
refresh to another address, a form's target;
nothing is fetched
text.unmapped_glyphs bytes that are not valid in the encoding read
appear as U+FFFD
image.unsupported_encoding image data was asked for and an inline
data: image could not be decoded
input.repaired an EPUB's zip archive has no readable central
directory and was read by its local headers
input.ambiguous a member name occurs twice in an EPUB's
archive; the first is read
child.encrypted an embedded document of an EPUB is encrypted
and was not opened
An embedded document handed to gofastcore.Options.ExtractChild may also carry child.not_extracted, child.unsupported or child.failed, which that function sets. The EPUB codes are in epub.go.
const StoredManifestItem = gofastcore.StoredManifestItem
StoredManifestItem is the Attrs["stored"] value of an image the publication stores and no chapter shows. It is core's gofastcore.StoredManifestItem.
const StoredStylesheet = gofastcore.StoredStylesheet
StoredStylesheet is the Attrs["stored"] value of the text of a style element, kept in the stored part. It is core's gofastcore.StoredStylesheet.
Variables ¶
This section is empty.
Functions ¶
func Detect ¶
Detect decides the format of an input from its name, and failing that from its content: a name ending in .html, .htm, .xhtml or .shtml is HTML; .md, .markdown, .mdown, .mkd, .mkdn or .mdwn Markdown; .txt or .text plain text; .epub EPUB. A zip archive whose first member is "mimetype" saying application/epub+zip is EPUB whatever its name. Without one of those, an input that begins with markup, or has a tag of a common HTML element in its first 4 KiB, is HTML, and any other plain text: Markdown is not guessed from content, since reading plain text as Markdown would take characters out of it.
Types ¶
type Extractor ¶
type Extractor struct {
// Format is the format to read the input as: "html", "md", "txt" or
// "epub". Empty means to decide it with Detect.
Format string
// MaxDepth bounds how deeply elements nest in the tree that is read.
// Zero means DefaultMaxDepth.
MaxDepth int
// MaxArchiveMembers bounds the number of files in an EPUB's archive.
// Zero means DefaultMaxArchiveMembers; negative means no limit.
MaxArchiveMembers int
// Charset is the character encoding the input comes with from outside
// it, as a label such as the charset parameter of a MIME Content-Type
// gives it ("windows-1251", "shift_jis"): the transport-layer encoding
// of the WHATWG encoding sniffing. It applies to HTML, Markdown and
// plain text, not to the chapters of an EPUB. A byte order mark
// overrides it; it overrides a <meta> declaration and detection. A
// label that names no encoding this library decodes is ignored, with
// the warning html.charset_unsupported. Empty means none.
Charset string
}
Extractor extracts HTML, Markdown, plain text and EPUB. The zero value is ready to use and safe for concurrent use.
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 of HTML, Markdown, plain text or EPUB from r, in the format e.Format names or Detect decides from Options.Name and the content, and returns the document. HTML, Markdown and plain text are one part of kind body; an EPUB is a part of kind chapter for each chapter of its spine. With the whole document (Options.Parts unset), script and style text, hidden form values and an EPUB's images no chapter shows follow in a part of kind stored.
An error wraps one of the gofastcore errors: ErrLimitExceeded when the input is larger than Limits.MaxInputBytes, or an EPUB's archive has more members than MaxArchiveMembers; ErrNoPartsSelected when Options.Parts selects no part (part 1 of HTML, Markdown or plain text, a chapter of an EPUB); ErrMalformed when r cannot be read, or an EPUB's archive or package document cannot be read; ErrUnsupported for a Format this library does not have, or an EPUB whose selected chapters are all encrypted. Any input that can be read is a document of its format: tag soup is recovered from the way browsers recover from it.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
gofasthtml
command
Command gofasthtml extracts HTML, Markdown, plain text and EPUB files into the shared document model of the go-fast libraries and prints them as JSON, Markdown or plain text.
|
Command gofasthtml extracts HTML, Markdown, plain text and EPUB files into the shared document model of the go-fast libraries and prints them as JSON, Markdown or plain text. |
|
internal
|
|
|
blocks
Package blocks turns an HTML document tree into the blocks of the shared model: headings, paragraphs, list items, tables, code, quotes, captions, images, comments and form values, with white space collapsed as CSS white-space: normal does, links and styles as spans, and the display reasons the page declares.
|
Package blocks turns an HTML document tree into the blocks of the shared model: headings, paragraphs, list items, tables, code, quotes, captions, images, comments and form values, with white space collapsed as CSS white-space: normal does, links and styles as spans, and the display reasons the page declares. |
|
charset
Package charset decides the character encoding of an HTML document and decodes it to UTF-8, keeping a map from offsets in the decoded text back to offsets in the source bytes.
|
Package charset decides the character encoding of an HTML document and decodes it to UTF-8, keeping a map from offsets in the decoded text back to offsets in the source bytes. |
|
css
Package css reads the declarations of an inline style attribute that say how content is displayed: display, visibility, opacity, the alpha of the text colour, and white-space.
|
Package css reads the declarations of an inline style attribute that say how content is displayed: display, visibility, opacity, the alpha of the text colour, and white-space. |
|
dom
Package dom builds a document tree from HTML tokens, recovering from tag soup the way the WHATWG HTML standard's tree construction does for the cases that change what text belongs where: implied end tags (p, li, dd, dt, option, table cells and rows), misnested formatting elements (a simplified adoption agency algorithm), reopening of formatting elements closed by a block, foster parenting of content misplaced in tables, foreign content (SVG, MathML) and the elements that break out of it.
|
Package dom builds a document tree from HTML tokens, recovering from tag soup the way the WHATWG HTML standard's tree construction does for the cases that change what text belongs where: implied end tags (p, li, dd, dt, option, table cells and rows), misnested formatting elements (a simplified adoption agency algorithm), reopening of formatting elements closed by a block, foster parenting of content misplaced in tables, foreign content (SVG, MathML) and the elements that break out of it. |
|
entity
Package entity resolves HTML character references: the named references of the WHATWG HTML standard (section 13.5, embedded from entities.txt) and the numeric ones with the standard's replacement rules.
|
Package entity resolves HTML character references: the named references of the WHATWG HTML standard (section 13.5, embedded from entities.txt) and the numeric ones with the standard's replacement rules. |
|
epub
Package epub reads the container of an EPUB 2 or 3 publication: the zip archive, META-INF/container.xml, the package document (OPF) with its metadata, manifest and spine, the navigation (EPUB 3 nav document or EPUB 2 NCX) and META-INF/encryption.xml.
|
Package epub reads the container of an EPUB 2 or 3 publication: the zip archive, META-INF/container.xml, the package document (OPF) with its metadata, manifest and spine, the navigation (EPUB 3 nav document or EPUB 2 NCX) and META-INF/encryption.xml. |
|
lexer
Package lexer is an HTML tokenizer following the WHATWG HTML standard, section 13.2.5 "Tokenization", for the states that decide what text a document holds: data, RCDATA, RAWTEXT, script data with its escaped states, PLAINTEXT, tags and attributes, comments, bogus comments, DOCTYPE, CDATA sections in foreign content, and character references.
|
Package lexer is an HTML tokenizer following the WHATWG HTML standard, section 13.2.5 "Tokenization", for the states that decide what text a document holds: data, RCDATA, RAWTEXT, script data with its escaped states, PLAINTEXT, tags and attributes, comments, bogus comments, DOCTYPE, CDATA sections in foreign content, and character references. |
|
md
Package md parses Markdown, following the block and inline structure of CommonMark 0.31 (with the GFM tables, strikethrough, task lists and extended autolinks), and renders it to HTML as a CommonMark renderer does, keeping a map from offsets in the HTML back to offsets in the Markdown.
|
Package md parses Markdown, following the block and inline structure of CommonMark 0.31 (with the GFM tables, strikethrough, task lists and extended autolinks), and renders it to HTML as a CommonMark renderer does, keeping a map from offsets in the HTML back to offsets in the Markdown. |