Skip to content

fix(parser): align HTML Parser to Python (nested-table fix; alignment golden) - #7

Closed
xugangqiang wants to merge 1 commit into
fix/markdown-parser-alignfrom
fix/html-epub-parser-align
Closed

xugangqiang wants to merge 1 commit into
fix/markdown-parser-alignfrom
fix/html-epub-parser-align

Conversation

@xugangqiang

Copy link
Copy Markdown
Owner

Summary

Aligns the Go HTML parser (internal/parser/parser/html_parser.go) with the Python RAGFlowHtmlParser and adds a reusable alignment test harness so the two engines can be diffed content-wise, not just line-wise.

This fixes a real divergence — nested-table 塌方 — where a <table> nested inside other block content (or containing another table) was collapsed into a single concatenated string on the Go side while Python kept each cell/tag on its own line.

Root cause

walkHTMLLeaf recursed into <table> children as plain leaf text and concatenated every cell ("NameAge\nAlice30…"). Python keeps each cell/tag on its own line, so the engines diverged whenever a table appeared inside a list, blockquote, or another table.

Fix (surgical; preserves per-block granularity)

  • htmlLeafText / walkHTMLLeaf now take a *[]map[string]any sink for structured table items.
  • walkHTMLLeaf gained a table-aware branch: a <table> is rendered to its inline markup, written as text, and appended as a structured doc_type_kwd:"table" item — mirroring the existing top-level (non-leaf) table path.
  • renderTableHTML returns "" on render error instead of recursing through the leaf extractor (avoids a render loop).

Tests (TDD: RED before fix, GREEN after)

  • TestHTMLParser_NestedTableProducesStructuredItems — the original 塌方 case.
  • TestHTMLParser_NestedListItemsPreserved — nested <ul>/<ol> items stay separate, in document order; top-level list tagged ck_type:"list".
  • TestHTMLParser_PrePreservesVerbatim — <pre>/<textarea> keep indentation and internal newlines verbatim (one leading newline dropped per spec).
  • TestHTMLParser_AlignmentGolden — content-equivalence against the Python RAGFlowHtmlParser baseline via the shared align_test.go tool.

Alignment tooling

No chunker/wiring change was needed: the HTML parser already emits OutputFormat:"json", and the chunker's table path preserves ck_type, so the structured table item flows through unchanged.

Test plan

  • bash build.sh --test ./internal/parser/parser/... → ok (full package, ~20s), including the four new tests.

Base / stacking

This PR stacks on fix/markdown-parser-align (PR infiniflow#18014) because it reuses the align_test.go framework (HTMLAlignOptions, LoadGolden {meta, items} contract) introduced there. origin/main does not yet contain that framework, so this is not based on main.

Follow-up

The branch name includes epub; EPUB parser alignment is a separate follow-up and is intentionally not part of this PR.

… golden)

Root cause: walkHTMLLeaf recursed into <table> children as plain leaf text
and concatenated every cell into one string ("NameAge\nAlice30…"), i.e. the
nested-table 塌方. The Python RAGFlowHtmlParser keeps each cell/tag on its
own line, so the two engines diverged on any table containing a nested table
or a table inside other block content.

Fix (surgical, preserves per-block granularity):
- htmlLeafText / walkHTMLLeaf now take a *[]map[string]any tableItems sink.
- walkHTMLLeaf gained a table-aware branch: a <table> is rendered to its
  inline markup, written as text, and appended as a structured
  doc_type_kwd:"table" item — mirroring the top-level (non-leaf) table path.
- renderTableHTML returns "" on render error instead of recursing through the
  leaf extractor (avoids a render loop).

Tests (TDD: RED before fix, GREEN after):
- TestHTMLParser_NestedTableProducesStructuredItems: the original 塌方 case.
- TestHTMLParser_NestedListItemsPreserved: nested <ul>/<ol> items stay
  separate, in document order, top-level list tagged ck_type:"list".
- TestHTMLParser_PrePreservesVerbatim: <pre>/<textarea> keep indentation and
  internal newlines verbatim (one leading newline dropped per spec).
- TestHTMLParser_AlignmentGolden: content-equivalence against the Python
  RAGFlowHtmlParser baseline via the shared align_test.go tool.

Align tooling:
- align_test.go: add StripHTMLHeadingMarker + HTMLAlignOptions preset.
- testdata/html.python.golden.json: Python baseline as a {meta, items}
  document (meta records how to regenerate; no committed generator script,
  matching the markdown golden convention).
- testdata/html.sample.html: shared input sample.

No chunker/wiring change was needed: the HTML parser already emits
OutputFormat:"json", and the chunker's table path preserves ck_type, so the
structured table item flows through unchanged.
@xugangqiang

Copy link
Copy Markdown
Owner Author

Superseded: reopening against upstream infiniflow/ragflow (base main, stacks on merged infiniflow#18014).

xugangqiang added a commit that referenced this pull request Oct 30, 2026
- prefer worker-claim time (doc.ProcessBeginAt) for wait duration (#2)
- omit zero-value times from the JSON log line instead of emitting
  0001-01-01, and guard the logger with atomic.Pointer for a safe swap (#8, #7)
- populate DurationMS from ParseDurationS so consumers need not recompute (#11)
- drop the unused IngestLog.Code field (#Nit)
- thread the failure reason through MarkFailed into the emitted event's
  ErrorInfo, so failed runs are no longer logged with an empty reason (#4)
- persist process_end_at idempotently (WHERE process_end_at IS NULL) so
  overlapping terminal events cannot rewrite the recorded completion
  instant (#12)

All changes covered by unit tests in internal/common, internal/observability
and internal/service.
xugangqiang added a commit that referenced this pull request Nov 2, 2026
- prefer worker-claim time (doc.ProcessBeginAt) for wait duration (#2)
- omit zero-value times from the JSON log line instead of emitting
  0001-01-01, and guard the logger with atomic.Pointer for a safe swap (#8, #7)
- populate DurationMS from ParseDurationS so consumers need not recompute (#11)
- drop the unused IngestLog.Code field (#Nit)
- thread the failure reason through MarkFailed into the emitted event's
  ErrorInfo, so failed runs are no longer logged with an empty reason (#4)
- persist process_end_at idempotently (WHERE process_end_at IS NULL) so
  overlapping terminal events cannot rewrite the recorded completion
  instant (#12)
- emit the running lifecycle event from PrepareValidatedRun AFTER the
  worker-claim time is stamped, so BeginTime and wait_duration_s reflect
  this run instead of a zero or previous-run value (#A)
- only emit user_name when set, so the always-empty key is not logged and
  the JSON field uses omitempty (#C)
- correct the ingestor.log sink docs: it always writes to stdout;
  ingestor.log.path only adds an optional rotated file on top (#B)
- drop the redundant SetIngestLog/SyncIngestLog calls from admin and
  syncer, which never emit ingestion events (#D)

All changes covered by unit tests in internal/common, internal/observability
and internal/service.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant