plaintext

command
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Command plaintext converts a document to text, keeping the block structure as blank lines and newlines.

It is the shape of program that this library makes easy and gets wrong in three specific ways if written the obvious way. All three are consequences of things measured elsewhere in examples/gip, and each one has a test here that fails without the fix.

Breaks go on start tags, not end tags. An element whose end tag the source leaves out - a list item, a table cell, a paragraph - has no end tag token, so its OnEndTag handler runs against the enclosing element's and content positioned there lands somewhere else. Emitting the break before each block begins needs no end tag at all, and the last break comes from the document end.

References are decoded per text node, not per chunk. A chunk never splits a character, but it splits everything larger: "&" can arrive as "&am" and "p;", and html.UnescapeString on each piece leaves both alone. So the text of a node is accumulated first and decoded once.

Whitespace is collapsed with state that survives a chunk boundary. Two spaces can arrive in two chunks, so "have I just emitted a space" has to live outside the handler, or the output depends on how the input was written.

What is skipped is tracked by depth rather than by selector, because there is no selector for "text that is not inside a script". A script, a style and a template hold content that is not the document's prose, and each is counted in and out - on whatever end tag closes it, not on a matching name, because the counter is asking "has this element ended" rather than "is this position mine". An element that the source never closes explicitly, such as a <head>, cannot be counted this way at all; see the skip list.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL