Skip to content

feat: add xhtml_to_plain_text helper - #240

Open
Eliot5566 wants to merge 1 commit into
strictdoc-project:mainfrom
Eliot5566:feat/235-xhtml-to-plain-text
Open

Eliot5566 wants to merge 1 commit into
strictdoc-project:mainfrom
Eliot5566:feat/235-xhtml-to-plain-text

Conversation

@Eliot5566

Copy link
Copy Markdown
Contributor

Closes #235.

This adds reqif.helpers.xhtml.xhtml_to_plain_text(xhtml, keep_paragraphs=False), based on the sketch in the issue:

  • It parses with fragment_fromstring(..., create_parent="div"), so several top-level elements, plain text and an empty value all work.
  • Block elements (the BLOCK_TAGS set from the issue) and <br/> mark a break before and after their text. Comments are skipped.
  • By default the result is one line, as in the sketch: First. Second., Line one Line two, and the compact list from the issue reads the same as the indented one.

For the open question about StrictDoc, keep_paragraphs=True separates the blocks with a blank line and starts a new line at each <br/>, for example The system shall lock after:\n\n5 attempts, or\n\nadmin request. Nested blocks do not add extra blank lines. Happy to change the shape of that output if StrictDoc needs something different.

Following the follow-up list in the issue, the README recipe and examples/05_extract_requirement_text/script.py now use the helper, and input.reqif has a third requirement whose XHTML has no whitespace between the tags. The old recipe would print ...end a session after:15 minutes without activity, ora logout. for it.

The unit tests cover the values from the table in the issue. Locally, the unit tests pass and all integration tests pass except commands/dump/01_basic_example and examples/04_convert_reqif_to_json, which fail the same way on main in my environment.

I used Claude to help write and test this change.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Hq1ACTsK85bRrY28fUQ7b3

lxml's text_content() adds no separator after block elements, so words
from adjacent blocks run together when the XHTML has no whitespace
between the tags. Add reqif.helpers.xhtml.xhtml_to_plain_text, which
breaks the text after block elements and <br/> and collapses the
remaining whitespace. keep_paragraphs=True keeps the paragraph and
line breaks for callers such as StrictDoc.

Use the helper in the README and in the requirement text example, and
add a requirement with compact XHTML to the example input.

Closes strictdoc-project#235

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hq1ACTsK85bRrY28fUQ7b3
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.

Convert XHTML attribute values to plain text without joining words

1 participant