internal/webfetchhelp/text/html.txt

RESPONSE TRANSFORM REFERENCE

USAGE
  ez-agent-webfetch --normalize [HTTP OPTIONS] http://URL
  ez-agent-webfetch --strip-and-index [HTTP OPTIONS] https://URL

MODES
      --normalize
      Tokenize HTML5 source and emit readable normalized UTF-8 HTML with
      soft-width lines. Formatting is intentionally lossy: quoting, entities,
      tag case, recoverable malformed markup, and collapsible whitespace may
      change. It is not a safe round-trip formatter and does not construct
      rendered layout.

      --strip-and-index
      Emit a compact indexed representation for agent use. For HTTP requests
      with no explicit Accept header, prefer Markdown, then HTML/XHTML at q=0.9.
      Any explicit Accept value, including an empty value that suppresses the
      default, is preserved. Redirect handling follows normal net/http header
      semantics. Normalize-only and raw requests do not receive this preference.

      A final text/markdown response is decoded and passed through without HTML
      parsing: CRLF/CR becomes LF, redundant trailing whitespace/newlines are
      trimmed, and one final LF is ensured before an index is appended. Other
      Markdown content is preserved. A final HTML/XHTML response is parsed as an
      HTML5 tree with scripting disabled, filtered, serialized as UTF-8 HTML,
      and followed by an index. Supplying --normalize as well is valid;
      strip-and-index determines the representation.

      HTML filtering selects the first main in document order, otherwise the
      first article, otherwise body, with whole-document fallback. Content
      outside the selected root is omitted. The main or article root wrapper is
      emitted; body, html, and document wrappers are not. There are no site,
      class, or ID heuristics.

      Filtering removes comments, doctypes, non-HTML namespaces including SVG
      and MathML, and these element subtrees: head, script, style, template, nav,
      footer, form, button, input, select, option, optgroup, textarea, label,
      datalist, output, progress, meter, iframe, noembed, and noframes. Elements
      with hidden, aria-hidden=true, or role=navigation are also removed, using
      trimmed ASCII-case-insensitive comparisons where values apply. Noscript
      fallback children remain, but the noscript wrapper is removed.

      Only href, src, alt, title, id, colspan, and rowspan attributes remain.
      After filtering, empty non-void elements are removed bottom-up when their
      remaining subtree has neither non-whitespace text nor an img. Empty td and
      th cells are retained directly for alignment, but a tr or table with no
      visible content anywhere is removed as a unit. Void elements such as br,
      hr, and img are never removed directly by the empty-element rule; img also
      keeps its ancestors alive. A div left with no retained attributes and,
      ignoring whitespace, exactly one element child is unwrapped as a redundant
      wrapper; the collapse runs bottom-up, so nested wrapper chains reduce to
      the innermost meaningful element.

      Block elements begin on their own line. Inline content flows and wraps at
      --html-width; tags and their retained attributes stay on one physical
      line. Text is escaped as HTML. Preformatted text preserves whitespace with
      CR and CRLF normalized to LF. Void elements emit only a start tag; other
      emitted elements have explicit end tags. The result is filtered HTML, not
      a sanitizer, rendered-page emulation, or Markdown conversion.

      --html-width N
      Soft target 80 through 160; default 120. It wraps ordinary filtered or
      normalized HTML text without splitting a Unicode code point or unbroken
      word. Tags/attributes and preformatted text may exceed it. Markdown
      passthrough is not reflowed. This option requires a response transform.

TYPE, MEMORY, AND SIZE
  Normalize requires a final text/html or application/xhtml+xml response.
  Strip-and-index accepts those types plus text/markdown and rejects every other
  media type before output. Matching is case-insensitive and parameters are
  allowed. Missing, malformed, or another type fails clearly. Exactly one
  Content-Type field value is required; duplicate or ambiguous values fail
  without being reflected in diagnostics.

  Transforms run entirely in memory and create no cache or spill file. Their
  omitted --max-bytes default is 10 MiB (10,485,760 bytes). A positive explicit
  value replaces that default and may be lower or higher. Explicit
  --max-bytes 0 is a usage error with a transform. The effective limit is
  checked separately on response-body bytes exposed by net/http (the combined
  network/content-encoded stage), decompressed bytes, decoded UTF-8, and final
  output including the appended index. Transfer framing and TLS records are
  transport-owned and are not separate body-byte stages. A transform, type,
  size, parsing, or tokenization failure emits no body prefix. A requested
  direct file was already
  opened and remains empty; --force may already have truncated prior data.

ENCODING AND CONTENT ENCODING
  For HTML/XHTML, a recognized UTF-8/UTF-16 BOM has highest priority. Without a
  BOM, a supported HTTP charset is honored, then the first 1024 bytes are
  inspected for HTML meta declarations, valid UTF-8, or the HTML windows-1252
  fallback. For Markdown, BOM has priority over HTTP charset and UTF-8 is the
  default; there is no meta scan or legacy fallback. Unsupported declared HTTP
  charsets fail. Malformed decoded text is replaced with U+FFFD before HTML
  tokenization/parsing or Markdown indexing.

  A transform requests Accept-Encoding: identity. A caller-supplied value other
  than exactly identity is a usage error. If a server nevertheless returns one
  gzip or deflate encoding, webfetch decodes it under combined
  network/content-encoded and separate decompressed limits. Stacked, Brotli,
  zstd, and unknown encodings are rejected.

TOKENIZATION AND PARSING BOUNDARY
  Normalize processes HTML/XHTML as tolerant source-order HTML5 tokens, including
  deterministic lexical SVG/MathML integration contexts. It retains comments
  and markup and does not construct a tree.

  HTML strip mode instead uses the tolerant HTML5 tree builder with scripting
  disabled. It decodes entities, applies implied elements and end tags, filters
  the selected tree, and serializes the remaining HTML. XHTML follows the same
  tolerant HTML parsing rules rather than XML round-trip semantics.

  Markdown is never sent through the HTML parser. A small line scanner recognizes
  ATX h1-h6 headings for the index and excludes headings inside backtick or tilde
  fenced code; it is not a broad Markdown parser.

  No script or CSS is executed. No subresource is fetched. There is no browser,
  CSSOM, scripted DOM, rendered-layout extraction, or page emulation.

INDEX
  Strip mode always appends a compact text index after emitted content:

      [ez-agent-webfetch index]
      title: "Page title"
      content-root: main
      content-end: line 87
      headings:
      - line 3 h1 text="Account settings"
      - lines 10-11 h2 text="Security"
      landmarks:
      - lines 1-87 main text="Account settings"
      - lines 9-30 section text="Security"
      [/ez-agent-webfetch index]

  Output and index use LF and end with one LF. For filtered HTML, title is the
  first head/title text, compacted to one line and omitted when empty.
  content-root is main, article, body, or fragment. Heading entries cover h1-h6
  elements with visible text. Landmark entries cover only main, article,
  section, and table; a landmark label is its first visible descendant heading
  when present. Entries with no visible text are omitted.

  For Markdown, content-root is markdown and compact ATX heading entries describe
  the otherwise preserved Markdown. Markdown has no title or landmark entries.
  content-end is the final emitted content line, or line 0 when filtered HTML
  emitted no content. One-based ranges refer only to emitted content and never
  to the index. Labels are single-line, control-free, quoted Go strings limited
  to 120 Unicode code points.

EXAMPLES
  ez-agent-webfetch --normalize https://example.test/page

  ez-agent-webfetch --strip-and-index --html-width 100 \
    -o page.agent.txt https://example.test/page

  ez-agent-webfetch --normalize --strip-and-index https://example.test/page