8000 feat(autoindex): extract man pages (roff man(7)), one record per .SH section by thomas-villani · Pull Request #1134 · xerj-org/xerj · GitHub
[go: up one dir, main page]

Skip to content

feat(autoindex): extract man pages (roff man(7)), one record per .SH section - #1134

Merged
xerj-org merged 1 commit into
xerj-org:mainfrom
thomas-villani:feat/man-pages
Oct 5, 2026
Merged

xerj-org merged 1 commit into
xerj-org:mainfrom
thomas-villani:feat/man-pages

Conversation

@thomas-villani
Copy link
Copy Markdown
Contributor

What this changes, and why

Most of a Unix system's own documentation is man pages, and autoindex had no reader for them:

  • A page was detected as txt-lines (or txt-prose), so /usr/share/man was indexed as 40-line windows of raw roff:
    • every line is a macro (.TP, .B \-a);
    • words are wrapped in font escapes (\fBls\fR);
    • pod2man pages open with about 100 lines of preamble.
  • Nothing recorded that the page was ls(1), or that it lists directory contents.
  • Pages whose examples contain CREATE TABLE …; (sqlite3, psql) risked being sent to the SQL dump extractor.

This adds Family::Man.

Detection is by content, never by extension. It runs in step 2b, before the text heuristics. A file counts as a man page when its first request is .TH, skipping over:

  • comments;
  • a generator's preamble requests (.de, .ds, .if, …);
  • calls to macros the page defines itself (grep.1 sets its date with its own .dT).

mdoc pages (.Dd, BSD/macOS), .so redirect stubs and files that start with text don't match, so they keep their current family. Gzipped pages (ls.1.gz) use the existing magic-byte detection.

Extraction: one record per .SH section. Fields:

field contents
title .TH NAME(SECTION), e.g. LS(1); otherwise the logical file name minus .gz
man_section the section from .TH (1, 7SSL, …)
heading the .SH heading
description the summary from the NAME line, e.g. "list directory contents", on every record
body the section text
section the split index, only when a long section is split

Locators are sh{k}-s{j}. The parser reads the macros instead of formatting them:

  • .SS stays inside its section as a heading line.
  • Option lists (.TP/.IP/.TQ) keep each term on its own line above its description.
  • No-fill blocks keep their lines and indentation.
  • tbl tables become one | line per row.
  • Escapes, special characters, strings (.ds) and conditionals are evaluated as nroff running under groff. That covers pod2man and DocBook preambles without special cases.

Wiring:

Origin: a port of the man parser in all2md (parsers/man.py, MIT, © 2025 Tom Villani, who is the author of this PR), reduced to plain text. Deliberate departures from it:

  • \} is no longer stripped before a conditional is evaluated. Stripping it made a one-line false .if t \{ … \} skip the rest of the page.
  • .ig XX ends at .XX, as roff does.
  • \N, \[u…] and charNNN above U+10FFFF produce nothing instead of an error.
  • A page-defined macro (bash's .FN, grep's .dT) can't be run. After the first .SH, its arguments are kept as text; before it, the call is treated as preamble.
  • A forward \h motion prints a space. Without that, DocBook's 1.\h'+01'\c list numbers ran into the item's text.
  • Limits:
    • 16 MiB of decompressed input;
    • 32 MiB of produced text;
    • string interpolation depth 8;
    • 200k interpolations per page. A test covers a string that doubles at each level, 2^30 copies if unbounded.

Reference-code note (AGENTS.md): groff (GPL) and mandoc (ISC) were not consulted. The port is from the author's own MIT code.

Evidence

cargo test -p xerj-autoindex --lib     1211 passed; 0 failed; 2 ignored   (22 new, in extract/man.rs)
cargo clippy -p xerj-autoindex --all-targets -- -D warnings   clean
cargo build --release -j 32 -p xerj-autoindex                 ok

Mutation check: with the sniff rule disabled, the gzipped end-to-end test fails.

Real pages: 15 man pages from Debian (the rust:latest image), run through sniffing and extraction in a temporary probe test that was removed before commit. Release build: 2.5–12 ms per page.

page generator family records title description
ls.1 help2man man 8 LS(1) list directory contents
bash.1 hand-written man 210 BASH(1) GNU Bourne-Again SHell
grep.1 hand-written man 24 GREP(1) print lines that match patterns
find.1 hand-written man 55 FIND(1) search for files in a directory hierarchy
tar.1 hand-written man 26 TAR(1) an archiving utility
make.1 hand-written man 13 MAKE(1) GNU Make utility to maintain groups of programs
curl.1 hand-written man 147 curl(1) transfer a URL
git.1 DocBook man 47 GIT(1) the stupid content tracker
git-commit.1 DocBook man 22 GIT-COMMIT(1) Record changes to the repository
apt.conf.5 DocBook man 35 APT.CONF(5) Configuration file for APT
perl.1 pod2man man 19 PERL(1) The Perl 5 language interpreter
cpan.1 pod2man man 13 CPAN(1) easily interact with CPAN from the command line
Git.3pm pod2man man 16 Git(3) Perl interface to the Git version control system
EVP_CIPHER-AES.7ssl pod2man man 6 EVP_CIPHER-AES(7SSL) The AES EVP_CIPHER implementations
ssh.1 mdoc txt-lines (unchanged)

An excerpt of ls(1)'s DESCRIPTION record as extracted:

List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.

-a, --all
do not ignore entries starting with .

-A, --almost-all
do not list implied . and ..

Checks

  • cargo fmt --all (--check clean)
  • Scoped release build: cargo build --release -j 32 -p xerj-autoindex
  • cargo test -p xerj-autoindex --lib passes
  • ES-YAML conformance suite: not run. The change is confined to the autoindex crate (a new extractor and its sniff rule), and no engine or wire behavior changes.
  • New ES-compatible YAML case: N/A
  • Docs: not in this PR. docs: list .pptx/.xlsx as extracted formats; retire "no XLSX/PPTX extractor" #1130 is the open PR editing landing/llms.txt, and only one PR may edit it at a time (docs/research/llms-txt-2026-09). Once docs: list .pptx/.xlsx as extracted formats; retire "no XLSX/PPTX extractor" #1130 lands, I'll add man pages to the format lists in README, llms.txt and llms-full.txt, plus a fact-check matrix row.
  • CONTRIBUTION_REVIEW audit: not run in full. Failure behavior: input over the cap, or a page with no text, is counted as junk (the same as txt-prose). A page that fails to parse cannot fail the run, because the parser has no error path; unknown macros are ignored.

Not run: no end-to-end xerj autoindex /usr/share/man run against a live node. The checks were the crate tests and the extractor probe described above.

Provenance

  • Written by: AI agent (Claude Code, Claude Opus 5.5), run by @thomas-villani, who reviewed it. It ports @thomas-villani's own MIT-licensed parser.
  • Verified: every command and number above. The real-page check covered pod2man 5.x, DocBook XSL, help2man 1.50 and hand-written pages from Debian.
  • Assumed (not tested):
    • Pages from other distributions and older generators (pod2man 2.x/4.x preambles, docutils rst2man, ronn, pandoc) parse equally well.
    • Detection may fail for a page whose .TH falls beyond the 8 KiB sniff prefix. None of the 15 did. Such a page keeps its current family and is not lost.
    • mdoc pages, most of macOS and BSD, are not extracted as man pages.

🤖 Generated with Claude Code

…section

Motivation: most of a Unix system's own documentation is man pages, and
autoindex had no reader for them. A page sniffed as txt-lines (or
txt-prose), so /usr/share/man indexed as 40-line windows of raw roff:
every line a macro (`.TP`, `.B \-a`), words wrapped in font escapes
(`\fBls\fR`), and pod2man pages opening with ~100 lines of preamble.
Nothing knew the page was ls(1) or that it lists directory contents.
sqlite3(1)/psql(1)-style pages, whose examples hold `CREATE TABLE ...;`,
were at risk of being routed to the SQL dump extractor.

Change:
- New `Family::Man` ("man"), sniffed by content in step 2b, before the
  text heuristics: after comments, a generator's preamble requests
  (`.de`/`.ds`/`.if`/...), and calls of macros the page defines itself,
  the first request is `.TH`. mdoc pages (`.Dd`, BSD/macOS), `.so` stubs
  and text-first files do not match and keep their current family.
  Gzip is the existing magic-byte detection, so `ls.1.gz` works.
- extract/man.rs: reads the macros rather than formatting them. Records
  carry `title` (`.TH` NAME(SECTION), else the logical file name minus
  `.gz`), `man_section`, `heading` (the `.SH` text), `description` (the
  NAME line's summary: "list directory contents"), `body`, and `section`
  when a long section is split. Locator `sh{k}-s{j}`. `.SS` stays in its
  section as a heading line; `.TP`/`.IP`/`.TQ` keep each term on its own
  line above its description; no-fill blocks keep lines and indentation;
  tbl tables become one ` | ` line per row. Escapes, special characters,
  strings and conditionals are evaluated (nroff answers, groff present),
  which handles pod2man and DocBook preambles with no special case.
- Wiring: dispatch (with the logical name, xerj-org#722 class), SourceAndDocs
  band in both order.rs functions, whole-file arms in estimate.rs and
  lib.rs's size cap, and the two family lists in estimate.rs tests.

Provenance: a port of the man parser in all2md (parsers/man.py, MIT,
(c) 2025 Tom Villani, the author of this PR), reduced to plain text.
Deliberate departures from it:
- `\}` is not stripped before a conditional is evaluated; doing so made
  a one-line false `.if t \{ ... \}` skip the rest of the page.
- `.ig XX` ends at `.XX`, as in roff (all2md ended it at `..`).
- `\N`/`\[u...]`/`charNNN` above U+10FFFF yield nothing instead of an
  error.
- A macro the page defines (bash's `.FN`, grep's `.dT`) cannot be run,
  so after the first `.SH` its arguments are kept as text (all2md drops
  them; bash.1's file names were lost).
- A forward `\h` motion prints a space (DocBook's `1.\h'+01'\c` list
  numbers ran into the item text).
- Bounds: 16 MiB decompressed input (read_whole), 32 MiB produced text,
  string interpolation depth 8 and 200k interpolations per page.

Evidence:
- 22 tests in extract/man.rs; cargo test -p xerj-autoindex --lib: 1211
  passed, 0 failed, 2 ignored. clippy -D warnings, fmt and the release
  build clean.
- Mutation: disabling the sniff rule fails the gzipped end-to-end test.
- 15 real pages from Debian (rust:latest image), extracted in a
  temporary probe test removed before commit: pod2man (perl, cpan,
  Git.3pm, EVP_CIPHER-AES.7ssl), DocBook (git, git-commit, apt.conf),
  help2man (ls), hand-written (bash, find, grep, make, tar, curl) all
  sniff as man with correct titles and NAME summaries, e.g. BASH(1)
  "GNU Bourne-Again SHell" in 210 records over 38 sections; ssh (mdoc)
  stays txt-lines. Release build: 2.5-12 ms per page (bash.1, 350 KB,
  is the 12 ms).
@cla-bot cla-bot Bot added the cla-signed label Oct 4, 2026
thomas-villani added a commit to thomas-villani/xerj that referenced this pull request Oct 4, 2026
…org#1132-xerj-org#1134)

Motivation: xerj-org#1132 fills vertical merges down, xerj-org#1133 names columns from a
two-row grouped header (Q1_Jan) and xerj-org#1134 adds a roff man(7) extractor.
The format lists and the fact-check matrix should say so. This commit
must merge AFTER those three code PRs, because it describes them as shipped.

- README, landing/llms.txt, landing/llms-full.txt: man pages added to the
  format lists (llms-full: plain or gzipped, one record per section, titled
  NAME(SECT), mdoc(7) pages stay plain text). The XLSX entry in llms-full
  now says vertical merges are filled down, a two-row grouped header is
  combined, and merges across columns are not expanded. It replaces
  "merged cells not expanded".
- scripts/seo/claims_rules.py: new GREEN THING row "Man pages (roff man(7))"
  citing extract/man.rs:1, and its gate says mdoc is not parsed. The .xlsx
  gate gets the same merged-cell wording as llms-full.
- testdata/factcheck: new good_man_answer.md fixture (git check-ignore
  confirms the !scripts/seo/testdata/**/*.md re-include applies), and
  good_xlsx_answer.md loses the old "merged cells are not expanded" line.

Verified: factcheck --self-test OK (35 THING rows); --fail-on error 0 ERROR;
build_articles --check current; landing-constants-guard passes.
--fixture-check reports 1 false positive (FC-EV-DANGLING) on this branch
alone, because good_man_answer.md cites extract/man.rs, which only exists
once xerj-org#1134 merges. With man.rs from xerj-org#1134 checked out it reports 0 false
positives and 13/13 good fixtures clean.
@xerj-org
xerj-org merged commit f3cc029 into xerj-org:main Oct 5, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0