go-fast-html
Extracts HTML, Markdown, plain text, EPUB, comma- and tab-separated values (CSV, TSV), Jupyter notebooks, and JSON and YAML as text into the shared go-fast document model, gofastcore.Document (github.com/AOShei/go-fast-core). Standard library and go-fast-core only.
import "github.com/AOShei/go-fast-html" // package gofasthtml
doc, err := gofasthtml.ExtractFile(ctx, "page.html", nil)
if err != nil { ... }
fmt.Print(doc.Markdown())
Source.Format is html, md, txt, epub, csv, tsv, ipynb, json or yaml. Extractor.Format sets the format; when it is empty, Detect decides it from Options.Name (.html, .htm, .xhtml, .shtml; .md, .markdown, .mdown, .mkd, .mkdn, .mdwn; .txt, .text; .epub; .csv; .tsv, .tab; .ipynb; .json, .jsonl, .ndjson, .geojson; .yaml, .yml) and, failing that, from the content: a zip archive whose mimetype member comes first and says application/epub+zip is EPUB, input that begins with markup, or has a tag of a common HTML element in its first 4 KiB, is HTML, and anything else plain text. Markdown, CSV, notebooks, JSON and YAML are never guessed from content, since reading plain text as Markdown takes characters out of it, a text with commas is not a table because of them, and a text is JSON, YAML or a notebook only by its name.
The command cmd/gofasthtml prints JSON, Markdown or text for files, directories and standard input (-); -as html|md|txt|epub|csv|tsv|ipynb|json|yaml sets the format, and -delimiter, -header and -no-quotes set Extractor.Delimiter, Extractor.Header and Extractor.NoQuotes. It offers the render flags every go-fast command shares; they affect Markdown and text only, for the document and an EPUB's documents alike, and JSON always has everything:
| Flag |
Effect in Markdown and text |
-furniture |
include running headers and footers |
-stored[=kinds] |
include stored data (data the file keeps but shows nowhere): all of it (-stored, =true, =all), or with =kinds (comma or +) only those kinds, wherever they are; =false or = turn it off. The kinds are core's gofastcore.StoredKindNames() (such as markup_comment, macro, stylesheet, manifest_item); any other name is an error listing them (exit status 2) |
-part-markers |
put a line before each part saying which part it is |
-children (default true) |
include embedded documents (an EPUB's documents beside its chapters), each after a line naming it ([child 1: name] in text); -children=false leaves them out |
-exclude-display[=reasons] |
leave out what the file declares not displayed: all of it (alone, =true or =all), or only the reasons named (=hidden, =no_ink, =hidden,no_ink); =false or = is off, and any other value is an error |
This library produces no running headers and footers itself; -furniture is there for the documents other extractors may return as children. Its stored part holds scripts, styles, <input type=hidden> values and EPUB images no chapter shows; markup comments are stored data too, kept where they stand. -stored=markup_comment shows the comments alone (Outlook's conditional comments, for example), -stored=macro the scripts, -stored=stylesheet the styles.
Speed and memory. On 9,835 pages of a web crawl the library is compared with nine Python and Rust extractors in bench/. For a batch, memory is reused from one document to the next: the input buffer and the tree's nodes and attributes go back to pools when a page is done, and nothing in a document refers to them (TestInputReused). Memory can be traded for speed with Go's garbage collector target, GOGC in the environment (or debug.SetGCPercent in a program); the command and the library use Go's default, 100, which uses the least memory. On the 9,835 pages (1.45 GiB), each read from disk as a pipeline does (wall time, peak memory):
GOGC |
One worker |
Peak |
24 workers |
Peak |
| 100 (default) |
5.29 s |
65 MB |
0.79 s |
308 MB |
| 200 |
4.89 s |
104 MB |
0.65 s |
562 MB |
| 400 |
4.71 s |
191 MB |
0.59 s |
1,032 MB |
Status: v0.4.1 (2026-11-24). The changes are in CHANGELOG.md; the development history and the items still open are in IMPLEMENTATION_PLAN.md.
What is read
The library has its own HTML5 parser under internal/, since the standard library has none:
- Encoding (
internal/charset), as the WHATWG encoding sniffing decides it: a byte order mark, then Extractor.Charset (command: -charset), then a <meta charset> or <meta http-equiv="Content-Type"> found by the prescan of the first 1024 bytes (of the first MiB when the input is not valid UTF-8: the standard's parser changes to an encoding a later <meta> declares, and a declaration is taken over a guess), then UTF-8 if the whole input is valid UTF-8 (and has no zero byte, see below); otherwise the encoding is recognised from the bytes when one is certain, and the input is refused when none is (Text that declares no encoding). Decoded are UTF-8, UTF-16LE, UTF-16BE, windows-1252 (following the Encoding standard, the ISO-8859-1 and US-ASCII labels are read as windows-1252), windows-874 (also iso-8859-11, tis-620), windows-1250 to windows-1258 (also cp1251, x-cp1251 and the like; iso-8859-9 and latin5 are windows-1254), macintosh, shift_jis (also sjis, ms932, windows-31j), gbk (also gb2312 and its labels), euc-kr (also ks_c_5601-1987, windows-949), big5, gb18030, euc-jp, ibm866, iso-8859-2 to iso-8859-8 (and iso-8859-8-i), iso-8859-10, iso-8859-13 to iso-8859-16, koi8-r, koi8-u and x-mac-cyrillic, each with the labels the Encoding standard gives it. A document that declares any other encoding (iso-2022-jp, utf-7, ...) is read as windows-1252, with the warning html.charset_unsupported.
Extractor.Charset is the encoding the input comes with from outside it, the transport-layer encoding of the sniffing algorithm, given as a label the way a MIME Content-Type charset parameter gives it (quotes are allowed): go-fast-email passes the charset of an HTML or text body this way. It applies to HTML, Markdown, plain text, CSV, TSV, JSON and YAML, not to the chapters of an EPUB, which declare their own. A byte order mark overrides it; it overrides <meta> and detection, so it is the way to read text whose encoding is not recognised, or is recognised wrongly. A label that names no encoding decoded here is ignored, with the warning html.charset_unsupported, and the encoding is decided as if it had not been given.
- Text that declares no encoding: see the section of that name below.
- Where the legacy encodings differ from the Encoding standard. The tables are Microsoft's code pages as the Unicode Consortium publishes them (copied from go-fast-rtf, see
NOTICE), not the standard's indexes. Big5 is code page 950, then, for the pairs 950 leaves undefined, Big5 with HKSCS as the standard's index has it (big5-hkscs.txt, from Python's big5hkscs codec; many of its characters are above U+FFFF), and the four pairs 0x8862, 0x8864, 0x88A3 and 0x88A5 decode to a letter and a combining mark as in the standard; where 950 defines a pair it agrees with the standard's index. GBK is code page 936; gb18030 is decoded as the standard's GB18030 decoder does, with its four-byte sequences (cjk.go, from go-fast-email's tables). EUC-JP is the standard's, from the JIS X 0208 and JIS X 0212 tables of go-fast-email. The other single-byte encodings (singlebyte.go) are generated from the standard's index files (gen/gen_single.py); its KOI8-U is KOI8-RU, with ў and Ў at 0xAE and 0xBE where Python's koi8_u has box drawing. ISO-2022-JP, whose escape sequences keep a state from one to the next, is not decoded. Shift_JIS and EUC-KR are code pages 932 and 949, which agree with the standard on nearly all characters; private-use and user-defined areas the standard maps are U+FFFD here. In the single-byte encodings, a byte from 0x80 to 0x9F the code page leaves undefined is the C1 control of the same value, as in the standard's indexes; any other undefined byte is U+FFFD. Bytes that are not a character are U+FFFD and counted in text.unmapped_glyphs; a lead byte followed by an ASCII byte that does not make a character gives U+FFFD and the ASCII byte is read again. Offsets in block locations point into the source bytes in every encoding.
- Tokenizer (
internal/lexer), following the WHATWG tokenization rules for data, RCDATA (title, textarea), RAWTEXT (style, xmp, iframe, noembed, noframes), script data with its escaped and double-escaped states, PLAINTEXT, tags and attributes, comments, bogus comments, DOCTYPE, CDATA in SVG and MathML, and character references: the full table of named references (internal/entity) with the rule for legacy names without a semicolon, and numeric references with the standard's replacements.
- Tree (
internal/dom), built the way browsers recover from tag soup: implied end tags (p, li, dd, dt, option, table rows and cells), headings that close headings, misnested formatting elements (a simplified adoption agency algorithm), formatting reopened after a block closes it, content misplaced in a table moved in front of it (foster parenting), SVG and MathML with the HTML elements that break out of them. noscript is read with scripting off, as its content.
Text that declares no encoding
Input with no byte order mark, no Extractor.Charset, no <meta> declaration (HTML) and that is not valid UTF-8 says nothing of its encoding. The Encoding standard reads it as windows-1252, which turns Japanese, Chinese, Korean or Russian text into garbage; Japanese and Chinese open data in Shift_JIS and GBK is common. Here the encoding is recognised from the bytes only where one is certain, and otherwise the input is refused. Nothing is guessed: each test is one that the text of its language passes by a wide margin and the same bytes read in another encoding fail by a wide margin.
| Read as |
When |
| UTF-8 |
there are at least four valid multi-byte sequences and at most one byte that is not valid for every four of them (UTF-8 with damaged bytes, each of which is U+FFFD with text.unmapped_glyphs) |
| UTF-16LE, UTF-16BE |
the input has a zero byte in its first 32 KiB, which UTF-8 text never has, and the bytes at every other offset are nearly all one of four values (0x00 for ASCII, 0x04 for Cyrillic), with no unpaired surrogate or control character. UTF-16 of ASCII and Cyrillic is valid UTF-8, which is why this test comes first. Han text in UTF-16 without a byte order mark is not recognised |
| Shift_JIS |
full-width kana are a fifth of the characters past ASCII or more: Japanese writes its grammar in kana, and no other encoding's bytes read as Shift_JIS give them |
| EUC-KR |
Hangul syllables of KS X 1001 are six in ten or more, Hanja and lone jamo one in ten and one in twenty or fewer |
| GBK, Big5 |
Han characters are six in ten or more, and half of them or more are among the 1,000 most frequent Chinese characters (with their Traditional forms; from the Unicode Consortium's Unihan database, internal/charset/common.go); the bytes of one read as the other give scattered rare characters |
| windows-1251 |
о е а и н т с р are 45 in a hundred of the letters or more, and there are capitals (Russian, Ukrainian, Bulgarian, Serbian; Hebrew in windows-1255, which shares the frequent bytes, has no capitals) |
| windows-1252 |
the bytes past ASCII stand alone, beside ASCII letters or spaces, as the accented letters of the languages of western Europe do, and nothing says they are another Latin code page (below) |
A double-byte code page is allowed one byte sequence it does not have in a hundred characters. It must be the only encoding that passes: two that pass are not decided. What says text is not windows-1252: a byte windows-1252 leaves undefined; a symbol between two ASCII letters (Polish ł ą ż are ³ ¹ ¿ there); the Icelandic letters ð þ ý, one in twenty bytes or more (Turkish ğ ş ı, Czech ý); õ without ã or ç, or with ö and ü (Hungarian ő); pairs of letters no language of windows-1252 writes in one text (ø beside ì, Czech ř beside ě; ã beside î, Romanian); two valid UTF-8 sequences; a zero byte.
Refused. When no encoding is certain, Extract returns ErrEncodingUnknown, which wraps gofastcore.ErrUnsupported, with a message that says to give the encoding with Extractor.Charset (command: -charset). A program reading a batch can test for it with errors.Is and read the file again with the encoding it knows. The chapters of an EPUB, which must be UTF-8 or UTF-16, are not refused: one whose encoding is not certain is read as windows-1252.
Reported. A document read in an encoding recognised from its bytes, windows-1252 included, has the warning text.encoding_detected (a code shared by the go-fast libraries) naming it (for an EPUB, one warning with the encodings and the number of chapters of each). Input that is valid UTF-8 or declares its encoding is never judged and has no such warning.
Measured. On Wikipedia articles in 30 languages, each encoded in the code pages of its language, 57 files (TestDetectEval; internal/charset/gen/fetch_detect_eval.py fetches them), read whole and in 40 windows each of 4 KiB, 1 KiB and 200 bytes:
- No file, and no window of 1 KiB or more, was read in a wrong encoding.
- Of the 2,280 windows of 200 bytes (a sentence or two), 30 were: Czech, Polish, Hungarian, Slovak, Romanian or Vietnamese read as windows-1252 (28) when the letters they hold that windows-1252 has elsewhere are too few to show, a Russian one as windows-1252, and a Japanese one in EUC-JP as EUC-KR. The warning names the encoding read.
- Shift_JIS, GBK, Big5, EUC-KR, windows-1251 and windows-1252 text was recognised in every window of 1 KiB or more; UTF-16 of Russian, Greek and French in every one; damaged UTF-8 in every one.
- Not decided, as they should not be: KOI8-R, KOI8-U, CP866, ISO-8859-5, EUC-JP, ISO-8859-2, ISO-8859-7, and the Windows code pages of Central Europe (but where windows-1252 gives the same text), Greek, Turkish, Hebrew, Arabic, Vietnamese and Thai. Han text in UTF-16 without a byte order mark is not decided either, nor GB18030 text whose four-byte sequences are more than one in a hundred characters.
Cost. Only input that is neither declared nor UTF-8 is tested, from just before its first byte past ASCII and for at most 32 KiB (4,096 characters for each double-byte code page), so the time is bounded whatever the file's size: about 2 µs for a 1 KiB windows-1252 text, 14 µs for 1 KiB of Japanese, Chinese or Korean, and at most about 0.25 ms (BenchmarkDetect). In a whole extraction that is 12 µs on a 4 KiB Japanese text (read in 22 µs, against 10 µs with -charset shift_jis) and 0.23 ms on 64 KiB (0.83 ms against 0.60 ms).
Markdown
Markdown (internal/md) is parsed with the block and inline structure of CommonMark 0.31: ATX and setext headings, paragraphs, block quotes, bullet and ordered lists with nesting and tight/loose lists, fenced and indented code, thematic breaks, HTML blocks, link reference definitions; emphasis by the delimiter algorithm, code spans, inline and reference links and images, autolinks, raw inline HTML, entity and numeric references, backslash escapes, hard breaks.
Conformance: all 652 examples of the CommonMark 0.31.2 spec pass, compared after the normalization of the reference test runner (normalize.py). The test (commonmark_test.go) reads the spec's JSON from outside the repository ($GOFASTHTML_COMMONMARK_SPEC, by default ../.testdata/commonmark-0.31.2-spec.json; the spec is CC-BY-SA 4.0) and is skipped without it; examples that fail are listed in it with the reason, and the list is empty. The spec is checked with link addresses percent-encoded as the reference renderer encodes them; in extraction a link's address is kept as the source writes it.
GitHub Flavored Markdown extensions read: tables; strikethrough (~~a~~ or ~a~, a strike span); task-list items (- [ ], - [x]: the marker is taken out of the text and the item has Attrs["checked"] "false" or "true"; an HTML list item that begins with a checkbox gets the same); extended autolinks (bare www., http:// and https:// addresses, www. ones linked as http://). Not read: footnotes, extended email autolinks (a bare address such as user@example.com stays text; <user@example.com> is a CommonMark autolink and is read) and the disallowed-raw-HTML filter (raw HTML is read as a page's is). Front matter is not recognized.
The document is rendered to HTML as a CommonMark renderer does and read by the HTML path above, so the blocks come out as in the table below (emphasis markers are gone; a link is a link span; code is declared code, verbatim, with the fence's language). Raw HTML in Markdown is read exactly as in a page: its declarations give the same display reasons, and an HTML comment is a note marked Attrs["stored"] markup_comment. Loc.Offset and Loc.Length are into the Markdown source: the lines of the Markdown block a block came from, and for raw HTML the exact bytes of its element.
Encoding: a byte order mark, then Extractor.Charset, else UTF-8 if the whole input is valid UTF-8, else as Text that declares no encoding says. Markdown has no <meta> prescan.
Plain text
Plain text is decoded as Markdown is, and split into paragraph blocks at blank lines and form feeds. Line breaks inside a paragraph are kept as \n: a text file does not say whether its lines are wrapped prose or lines that mean something (an address, a verse, a list), and keeping them loses nothing. Trailing white space on each line and control characters other than tab are dropped. Loc gives each paragraph's byte range. Plain text declares nothing about display, so no block has a display reason.
CSV and TSV
Comma-separated values (csv, MIME text/csv) and tab-separated values (tsv, MIME text/tab-separated-values) are decoded as plain text is (a byte order mark, then Extractor.Charset, then UTF-8 if valid, else as Text that declares no encoding says), and are one part of kind sheet, number 1, named by the file without its directory and extension. Its rows are table blocks.
- The delimiter, in this order: a first line
sep=X (Excel's declaration; sep= in any case, then one character, then the line end), which is read as the declaration and not as a row; Extractor.Delimiter; tab for tsv; for csv, the one of , ; tab | that splits the most of the first 100 rows (in the first 64 KiB) into the same number of fields, more than one, the earlier in that list on a tie, else ,; a first row that runs past the 64 KiB is skipped, and the rows after it decide. Reading the syntax is not judging content: Metadata.Extra["delimiter"] holds the delimiter (written U+XXXX when it is a control character other than tab, or a ligature U+FB00 to U+FB06, which would otherwise be written as its letters) and Metadata.Extra["delimiter_source"] how it was chosen: declared, option, format or detected. A quote or a line end cannot be a delimiter: Extract returns ErrUnsupported for one given as Extractor.Delimiter.
- Fields are read as RFC 4180 has them, and leniently: quoted fields with doubled quotes, line breaks inside quotes (each of CRLF, CR and LF there is a
\n), rows ending in CRLF, LF or CR. A quote inside an unquoted field is text; text after a closing quote is joined to the field ("ab"c is abc, as Python's csv reads it); a quoted field not closed runs to the end of the input. Each of these is counted, and the part has one input.repaired with the number of rows output. Input is never refused for its syntax.
- Quoting in TSV. By default TSV is read with the same quoting as CSV, as Excel's tab-delimited export, pandas and Python's
excel-tab dialect write and read it. The official definition of text/tab-separated-values (IANA) has no quoting: a field cannot hold a tab or a line end, and a quote is an ordinary character. Under the default, a quote that is not balanced therefore merges the rows after it into one field, up to the end of the input, reported as input.repaired. Extractor.NoQuotes (-no-quotes) reads as IANA has it, for CSV and TSV alike: fields end only at the delimiter and rows only at line ends, and quotes are kept as characters.
- The grid. Rows are padded with empty cells to the widest, so every table is a rectangle. Rows with no text at the end are left out, a row whose fields hold only control characters (which are dropped) having none; blank lines and empty cells before them are kept. Cell text follows core's text conventions (control characters other than tab and line feed are dropped, ligatures written as their letters) and is otherwise as written: nothing is trimmed. A cell is text, never a number, date or formula: a value beginning with
= is text and is not evaluated.
- No header row is inferred: CSV does not declare one, so
HeaderRows is 0. Extractor.Header is the declaration RFC 4180's MIME parameter header=present makes, and gives the first table HeaderRows 1.
- Splitting. A table has at most 1,000 rows and at most 2^20 cells; a longer one is split by rows, and the later pieces are
Continued. Each piece has Loc.Range in A1 notation (A1:F1000, A1001:F2000), counting rows of the table (records, after a sep= line), not lines of the file.
- Bounds. Fields past column 16,384 (
MaxCSVColumns) are not read, and a field is read up to 1 MiB (MaxCSVField) and cut there at a character boundary; each gives the part one limit.exceeded with the count. The text budget counts BlockCost for each table block and, for each row, CellCost for each cell, empty or not, and its text; where it runs out the sheet is cut off, with the number of rows not read. The text is read twice, once to count the rows and find the widest and once to build the tables, and besides the output only one row is held.
- No stored part, no display reasons, no inferred structure.
Jupyter notebooks
A Jupyter notebook (ipynb, MIME application/x-ipynb+json), format version 4 (4.0 to 4.5) or 3, is one part of kind body: its cells in order, each read on its own, so that the HTML of one cell or output cannot run into the next. Version 1 and 2 notebooks give ErrUnsupported; input that is not a JSON object with cells, ErrMalformed. A value of a JSON type the format does not allow where it stands (a cell that is not an object, a source that is a number) is not read, and the part has one input.repaired with the count; nothing else is refused.
- Markdown cells are read as Markdown is (above). An image a cell names as
attachment:name is the cell's attachment, with Image.ID cell-N/attachment:name (with a name that has control characters or is longer than 1,024 bytes, sha256: and the start of its hash in its place) and, with ImagesData, its bytes; it is not a resource loaded from elsewhere.
- Code cells are a
code block with the code, and Attrs["language"] the notebook's language (language_info.name, else kernelspec.language; in version 3, the cell's language). Then come the blocks of their outputs, each with Attrs["output"] (AttrOutput) saying which output it is: stdout or stderr for text the code printed (consecutive outputs of one stream are joined, as a notebook shows them), execute_result for the value of the cell, display_data for what it displayed, error for the traceback of the error it raised. Printed text, tracebacks and text/plain values are read as JupyterLab shows them: colour and cursor escape sequences are left out, a backspace takes out the character before it, and text after a carriage return writes over its line (a progress bar is its last state).
- Of an output's representations, the one JupyterLab prefers is read:
text/html (a pandas DataFrame is its HTML table), text/markdown, text/latex (a formula block, notation latex), an image (SVG, BMP, PNG, JPEG, GIF, WebP, as an image block), then text/plain; the others are alternatives of it and are not read. An output with none of these (only JSON, JavaScript or a widget's state) has no block. HTML is read as a page is: scripts go to the stored part, with input.active_content.
- Raw cells, and cells of a kind not known, are a
code block with their text and no language; version 3's heading cells are headings.
- What the notebook declares. A cell whose input is collapsed (
metadata.jupyter.source_hidden) has its blocks hidden, whole, whatever the HTML in it; so do the outputs of a cell whose outputs are collapsed (metadata.jupyter.outputs_hidden, or metadata.collapsed, and version 3's collapsed).
- Location. A cell's
id (version 4.5) is Loc.Anchor of its blocks. There are no byte offsets.
- Metadata:
Title and Authors from the notebook's metadata, and in Metadata.Extra kernel (the kernel's display name), language, language_version and nbformat (4.5).
- Bounds. The text budget and the decode budget (which counts the HTML each cell becomes, and the decoded images) apply as for a page; where one runs out the body is cut off, with the number of cells not read. The JSON is held in memory as it is parsed, and the cells are read one at a time.
JSON and YAML
JSON (json, MIME application/json: .json, and .jsonl, .ndjson, .geojson) and YAML (yaml, MIME application/yaml: .yaml, .yml) are read as text, nothing parsed: data is text here as a person reading the file sees it. They are decoded as plain text is, and are one part of kind body of code blocks with Attrs["language"] json or yaml.
- Text as written. Lines keep their indentation and white space (in YAML it is structure), and ligatures, as code does. Control characters other than tab are dropped, and line ends (LF, CR LF or a lone CR) are
\n.
- Blocks. A YAML file of several documents has a block for each: a document starts at a
--- line (--- then the line end, a space or a tab), and after a ... line at the next line with text, its %YAML or %TAG directives first. Blank lines at the start and end of a document are left out. A long document is split at line ends into pieces of about 1,000 lines and 256 KiB, the later pieces Continued; the pieces joined with line feeds give the document back, blank lines and white space included. A line is never cut: a longer one is a piece of its own. Each block has Loc.Offset and Loc.Length in the file.
- Bounds. The text budget counts each block; where it runs out the body is cut off, with the number of lines not read (every line after the last block made and the blank lines that end its document). The decode budget cutting the input gives the number of its bytes not read. A file of only blank lines (white space and control characters) has
part.no_text.
- Invalid JSON or YAML is read like any other: its text is what it is.
EPUB
EPUB 2 and 3 publications (Source.Format epub, .epub or a zip whose first member says application/epub+zip) are read here rather than in a library of their own, since their content is XHTML and extractors do not import each other.
- Container. The zip archive is read with a limit on members (
Extractor.MaxArchiveMembers, default 100,000; more is ErrLimitExceeded) and every member decompressed within Limits.MaxDecodedBytes, shared with embedded documents. An archive whose central directory is missing (a file cut short) is read by the members' local headers, with input.repaired; a name that occurs twice gives input.ambiguous and the first is read. A missing or wrong mimetype member gives epub.mimetype (info) and the archive is read anyway. META-INF/container.xml leads to the package document (OPF); without one the input is ErrMalformed. container.xml, the package document, the navigation document, the NCX and encryption.xml are read up to 16 MiB each (epub.MaxXML): a larger container.xml or package document is ErrLimitExceeded.
- Metadata.
dc:title (the first) to Title; each dc:creator to Authors; dc:language to Language; each dc:subject to Keywords; dc:date to Created when it parses as ISO 8601, and as written to Extra["date"]; meta property="dcterms:modified" to Modified; dc:description, dc:publisher, dc:contributor, dc:rights, dc:source, dc:type, dc:format, dc:relation, dc:coverage to Extra under their names; the identifiers to Extra["identifier"], one a line, the package's unique identifier first; the package version to Extra["epub_version"]. The text of one metadata element is read up to 64 KiB, up to 4096 Dublin Core elements, and up to 2^20 manifest items and spine entries; past any of these, the document carries limit.exceeded with the count.
- Chapters. One
chapter part per spine item, numbered by its place in the spine, read by the HTML path in XHTML mode (a self-closing tag is an empty element; the XML declaration is not a comment). Loc offsets are into that chapter's file. Part.Name is the chapter's first entry in the table of contents, else its <title>. An item with linear="no" is extracted in its place and carries epub.non_linear (info). Images a chapter shows are resolved in the archive: Image.ID is the member's path, MIME the manifest's media type, and with ImagesData the bytes. References to members of the archive are not counted as external. Options.Parts selects chapters; Limits.MaxParts (default 100,000) bounds them. When Limits.MaxParts or the text budget stops the chapters, the rest of the manifest (below) is not read either, and the document's limit.exceeded names what was not read, as the publication has them: the later chapters, the stored part (the scripts, styles and form values of the chapters read), the images of the manifest that no chapter read shows (some may be ones a later chapter would have shown) and the embedded documents.
- Navigation. The EPUB 3 navigation document (
nav with epub:type="toc"), else the EPUB 2 NCX, becomes Outline: title, nesting level and the number of the chapter it points to (0 when it points outside the spine). Titles count against the text budget before the first chapter. The table of contents is read up to 100,000 entries, lists nested up to 64 levels and titles up to 4 KiB (cut at the same byte however the title's text is split); past any of these, and when the navigation document or NCX is larger than 16 MiB or the decode budget and is not read, the document carries limit.exceeded.
- The rest of the manifest, with the whole document only (
Options.Parts unset): images no chapter shows go to the stored part (after the chapters' scripts and styles), with no display reason, as image blocks with Attrs["stored"] manifest_item and Attrs["name"] the member path (a cover image has the text cover); stylesheets are skipped; every other item (documents outside the spine, fonts, audio, scripts) is handed to Options.ExtractChild and listed in Children. Scripts count as active content.
- Encryption. Resources
META-INF/encryption.xml lists, font obfuscation (IDPF and Adobe) aside, are not read: an encrypted chapter is a part with part.failed and epub.encrypted, an encrypted image an image block without data (its chapter, or the stored part, carries epub.encrypted with the count), an encrypted embedded file a child with child.encrypted, and the document carries epub.encrypted with the count. When every selected chapter is encrypted (DRM), nothing usable is left and the result is ErrUnsupported. An encryption.xml that cannot be read in full (larger than 16 MiB or the decode budget, listing more than 2^20 members, damaged, or XML that cannot be read to its end) leaves which members are encrypted unknown, and none is read as text: every member but mimetype, META-INF/ and the package document is treated as possibly encrypted (a chapter gets part.failed and epub.encrypted saying so, an embedded file child.encrypted, and the navigation document and NCX are not read). Since that leaves no chapter to read, the result is ErrLimitExceeded (too large, too many members) or ErrMalformed (damaged).
- Display. Each chapter's declarations give display reasons as in a page. That includes the navigation document when it is in the spine: the
hidden attribute EPUB 3 puts on its nav and ol elements (landmarks, page list, deeper levels of the table of contents) is a declaration, and those blocks get hidden.
How HTML maps
One part of kind body, numbered 1, and, when the page has script or style text or <input type=hidden> values and Options.Parts is unset, a part of kind stored, numbered 2, that holds them. Every block has Loc.Offset and Loc.Length, its byte range in the source file (also when the file is UTF-16 or windows-1252), and Loc.Anchor from the id of its element, or of an element in it (<a name> counts).
| HTML |
Block |
h1–h6 |
heading, level 1–6 |
p, div and other block elements, text between blocks |
paragraph |
li in ul, ol, menu, dir |
list_item, Level the list depth, Ordered, Marker: 3., c., iv. per start, value, type, reversed; a bullet (•, ◦, ▪ by depth, or per type) for unordered lists. Block elements inside an item are line breaks in its text; text after a nested list is a paragraph |
table |
table; rowspan/colspan as Merges (clamped as the standard clamps them; rowspan=0 runs to the end); leading thead rows, or rows of th only, as HeaderRows; with Options.Spans, a cell's first link in Table.Cells. A table inside a cell is written into the cell's text, a line a row |
caption, figcaption, legend |
caption |
pre, listing, xmp, plaintext |
code, verbatim; Attrs["language"] from a language-x or lang-x class on it or its code |
blockquote |
quote, Level the nesting depth |
img |
image with the alt text; Image.ID the src (a sha256: hash for a data: URI or a very long address), Width/Height from the attributes; with ImagesData, the bytes of data: URIs |
a href |
a link span, with Options.Spans |
b, strong, i, em, u, s, del, code, kbd, sub, sup, ... |
style spans, with Options.Spans |
br |
\n in the text |
hr (and a Markdown thematic break, ---, ***, ___) |
thematic_break, no text, with the display reasons of the element; inside a table cell only a line break in the cell's text |
select |
note of type choices, the options a line each, Attrs["field"] its name |
textarea, input with a value |
paragraph with Attrs["field"] the control's name; the value of <input type=hidden> goes to the stored part with Attrs["stored"] form_field |
<!-- comment --> |
note with Attrs["stored"] markup_comment and no note type, after the block it stands in; no display reason; left out of rendered text unless RenderOptions.Stored or StoredKinds asks for it (also in raw HTML in Markdown and in EPUB chapters) |
script, style |
in the stored part as code (language javascript, json or css), verbatim, read as data and never run, with Attrs["stored"] macro (core's StoredMacro) for a script and stylesheet for a style element; no display reason |
template |
read as text where it stands, with the reason hidden (its content is inert by the element's definition) |
noscript, noembed, noframes, iframe fallback, closed details/dialog, rp |
read as text where they stand; no display reason |
iframe srcdoc |
the document in the attribute is parsed as HTML and its blocks come where the iframe stands, before any fallback text; Loc is the (outermost) iframe element in the page; what the iframe declares (hidden, inline display:none, ...) is added to all of it, what the inner document declares applies inside; its title, meta and lang are not the page's metadata and are not read. Its elements count from the iframe's depth against Extractor.MaxDepth: a srcdoc document whose iframe is at the limit is not read (limit.exceeded). Its bytes count against the decode budget (cut with limit.exceeded) and its text against the text budget. |
iframe src, frame, embed, object, audio, video |
the address is not fetched and counts toward input.external_reference (a data: or javascript: address does not); the element's own content, such as an iframe's fallback text, is read where it stands |
<title> |
Metadata.Title |
<meta name=author> |
Metadata.Authors, one entry each, not split |
<meta name=keywords> |
Metadata.Keywords, one entry, not split |
<meta name=description> |
Metadata.Extra["description"] |
<meta name=generator> |
Metadata.Application |
<html lang> |
Metadata.Language |
Outside pre (and white-space: pre in inline style), white space is collapsed as CSS white-space: normal does. Ligatures U+FB00–U+FB06 are written as their letters everywhere but in code.
What the file declares
Everything is extracted; nothing is dropped. Where the page itself declares that text is not displayed, or is drawn with no ink, the block (or the span, or the table cells) carries that reason in Display, following the page's own inheritance as CSS does. Only the element's attributes and its style attribute are read: stylesheets (<style> and linked ones) and scripts are not evaluated.
hidden is reported for:
- the
hidden attribute, including hidden="until-found" (unless inline style gives another display);
- inline
display: none;
- inline
visibility: hidden or collapse, inherited, and undone by visibility: visible inside;
- the SVG presentation attribute
display="none";
- the SVG presentation attribute
visibility="hidden" (or collapse), undone by visible inside;
- the content of
template, which is inert by the element's definition.
no_ink is reported for:
- inline
opacity: 0 exactly;
- inline
color: transparent, or a colour whose alpha is exactly 0 (undone by another colour inside).
The same rules apply to raw HTML in Markdown and to every EPUB chapter.
The boundary. Colour, size, position and overlap are not judged. Nothing compares two values or applies a threshold: a text colour equal to its background, a font size of 0 or 1 pixel, a large negative offset, clipping, opacity: 0.01 or a colour with alpha 0.01 give no reason. A rule in a stylesheet that hides a class gives no reason either, since stylesheets are not evaluated. A block with no Display value says nothing about whether a reader can see it.
Not reported, no reason: noscript (shown under a condition), closed details and dialog (shown on interaction), rp, and aria-hidden (which concerns assistive technology; it neither adds a reason nor removes one).
Kept where they are, with no reason: comments and attribute text are note blocks; a comment is stored data (Attrs["stored"] markup_comment), as are script and style text and <input type=hidden> values in the stored part, all left out by the renderers unless RenderOptions.Stored is set or RenderOptions.StoredKinds names their kind. A comment or attribute inside a hidden element still has no reason.
Characters are kept. Zero-width characters, Unicode tag characters, bidirectional controls and variation selectors stay in the text as written, with no reason and no warning.
Attribute text. Text a page keeps in attributes without drawing it in the flow of the page is extracted: aria-label, aria-description, aria-roledescription, aria-placeholder, title and placeholder on any element, and alt on an image map's area. Each becomes a note block with Attrs type: attribute and attribute: <name>, at the element's place (before the block the element is in, when that block has no text yet), with Loc the byte range of the attribute's value. A value that only repeats the element's own text, or an image's alt text, after white space is collapsed, is left out, since that text is extracted already. aria-labelledby and aria-describedby are not followed: the elements they name are read where they stand, with the reasons their own declarations give.
Warnings
| Code |
Severity |
When |
input.active_content |
info |
scripts, on... event attributes, javascript: addresses |
input.external_reference |
warning |
resources loaded from elsewhere: pictures, scripts, stylesheets, frames, embedded objects, media, a refresh to another address, a form's target; nothing is fetched |
text.unmapped_glyphs |
warning |
bytes not valid in the encoding read, shown as U+FFFD |
text.control_characters_removed |
info |
control characters with no meaning as text (NUL, ESC, vertical tab, the C1 controls) were removed from the text: of the body, a chapter, a sheet's fields, a notebook, a JSON or YAML file (on that part), or of the metadata and the table of contents (on the document), with the count. Not counted: tab, line ends and form feed, which are white space or a break; and what is removed from addresses and names (links, image addresses, anchors, languages, field names), which identify something and are not text. Text the text budget leaves out is not counted |
image.unsupported_encoding |
warning |
with ImagesData, a data: image that could not be decoded, or an EPUB image that could not be read (not counting images left without data by the decode budget or encryption, which have their own warnings: limit.exceeded, epub.encrypted) |
part.no_text, part.image_only |
warning |
the body, a CSV or TSV sheet, a notebook, a JSON or YAML file, or an EPUB chapter has no text (part.no_text also when an EPUB has no chapters) |
limit.exceeded |
error |
a limit cut off the body, an EPUB chapter (saying so too when the chapter's name was left out), the table of contents or the stored part; the text budget left out an EPUB chapter's name; the text budget stopped the reading of an EPUB's chapters (on the document, naming the chapter and what was not read) or of the rest of its manifest (on the document, with the number of images and embedded documents not read); an EPUB has more chapters than Limits.MaxParts (on the document, naming what was not read); the decode budget left data: images undecoded, without ID or data (on their part, in every images mode), EPUB images without data (on their chapter or the stored part) or embedded documents unread (on the document), with the count; an EPUB's table of contents passed a bound of its own (above) or its navigation document or NCX was not read for its size; an EPUB's encryption.xml was too large to read in a document that is returned; an iframe srcdoc document is at the depth limit; a bound of the tree builder lost spans or a declared display: more than 64 formatting elements open at once, the bound on formatting elements reopened, a style attribute longer than 16 KiB (one warning each per part); a bound cut a string of the output (each once, with a count): a metadata value over 64 KiB, more than 256 author or keywords declarations, an <html lang> over 64 bytes (left out), on the document; an id (or an a's name) over 1,024 bytes not used as Loc.Anchor, a code block's language over 64 bytes (left out), on the part; an EPUB chapter's <title> over 64 KiB cut as its name, on the chapter; an EPUB's package metadata text over 64 KiB, Dublin Core elements past 4,096, manifest items or spine entries past 2^20, on the document; in CSV and TSV, fields past column 16,384 and fields over 1 MiB, on the sheet, and the text budget cutting the sheet off, with the number of rows not read; in a notebook, the text or decode budget cutting the body off, with the number of cells not read, and attached images without data; in JSON and YAML, the text budget cutting the body off, with the number of lines not read |
text.encoding_detected |
warning |
the input declares no encoding and is not UTF-8; it was read in the encoding its bytes were recognised as, which the message names (Text that declares no encoding). In an EPUB, one warning for the document with the encodings and the number of chapters of each |
html.charset_unsupported |
warning |
a declared encoding that is not decoded, read as windows-1252; or an Extractor.Charset label that is not decoded, ignored. In an EPUB, one warning for the document with the number of chapters and the labels (the first 8, with each one's count) |
html.nesting_flattened |
warning |
elements nested deeper than Extractor.MaxDepth (default 512), or more than 4096 open at once; below that their text is read side by side, with their display reasons kept |
input.repaired, input.ambiguous |
warning |
EPUB: the archive's directory was unreadable, or a member name occurs twice. CSV and TSV (input.repaired only): rows with a quote outside RFC 4180's rules, with their number. Notebooks (input.repaired only): values of a JSON type the format does not allow where they stand, not read, with their number |
epub.non_linear |
info |
the chapter is linear="no" in the spine |
epub.encrypted |
error |
resources are encrypted (DRM) and were not read; or encryption.xml could not be read in full and a chapter or image may be encrypted; on a chapter or the stored part, the count of encrypted images left without data |
epub.mimetype |
info |
the mimetype member is missing or wrong |
part.failed |
error |
EPUB: a chapter could not be read (missing from the manifest or archive, encrypted) |
child.encrypted |
error |
EPUB, on a child: an embedded document META-INF/encryption.xml lists; it was not opened |
child.not_extracted |
info, warning |
EPUB, on a child: not extracted, since Options.Child is not set (info), or the depth limit was reached or its bytes could not be read (warning); set by core's Options.ExtractChild |
child.unsupported |
warning |
EPUB, on a child: Options.Child returned ErrUnsupported (the command's does for formats other than HTML, Markdown, text and EPUB) |
child.failed |
error |
EPUB, on a child: Options.Child returned any other error |
Limits
| Limit |
Default |
|
Limits.MaxInputBytes |
256 MiB |
the file is held in memory; larger is ErrLimitExceeded |
Limits.MaxDecodedBytes |
1 GiB |
the text decoded to UTF-8, the HTML a Markdown document is rendered to, and the bytes of data: images |
Limits.MaxTextBytes |
256 MiB |
core's text budget: block text, cells, markers, link targets, image IDs, BlockCost a block and CellCost a cell of a table (so a small table declaring a huge grid is cut by rows) |
Extractor.MaxDepth |
512 |
tree depth |
MaxCSVColumns, MaxCSVField |
16,384, 1 MiB |
the fields of a CSV or TSV row read, and the bytes of a field |
Every fixed bound that cuts a string of the output (text, names, metadata values, anchors, languages) reports it with limit.exceeded; bounds that lose nothing are not reported: an image address over 2,048 bytes is replaced by sha256: and its hash as Image.ID (the address is in the source at Loc), a width or height outside 1 to 2^24 - 1 is an invalid value and is not given, a number attribute (start, value, colspan, rowspan) of more than 18 digits is an invalid integer and is ignored (shorter ones are read in full; colspan and rowspan are clamped as the HTML standard says), a declared encoding label is read up to 64 bytes (only to look it up and to quote it in html.charset_unsupported), and link labels and ordered-list numbers in Markdown are bounded as CommonMark says. Block quotes and lists in Markdown nest at most 100 deep (md.MaxNest); a marker past that is text, kept. Scans in Markdown that find no end (code spans, comments) are not repeated. Every tag costs bounded work: end tags look at most 512 elements down the stack, at most 64 formatting elements are kept to reopen and the number reopened is bounded, and inline styles are read up to 16 KiB; when a page reaches one of the last three, its part carries limit.exceeded. Extraction stops when the context is cancelled.
Not read
Stylesheets and scripts (not evaluated); aria-labelledby/aria-describedby references (not resolved); srcset; the title and meta of an iframe srcdoc document; CSS generated content; <base href> (addresses are as written); layout tables are tables. In Markdown: GFM footnotes, extended email autolinks, the disallowed-raw-HTML filter and front matter, as above.
Licence
MIT. The named character reference table is from the WHATWG HTML Living Standard; see NOTICE.