Enhancements: Regex Extraction, Advanced Flags, and CLI Improvements#818
Open
mitchcapper wants to merge 6 commits into
Open
Enhancements: Regex Extraction, Advanced Flags, and CLI Improvements#818mitchcapper wants to merge 6 commits into
mitchcapper wants to merge 6 commits into
Conversation
P4X-ng
approved these changes
Jan 10, 2026
Owner
|
Hi @P4X-ng, your PR looks interesting but for some reason the tests haven't run. I suggest you edit your PR or add a commit like |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This code was AI assisted though I have reviewed and tested the code.
Overview
This PR introduces several enhancements to the extraction process and CLI functionality. Key changes include:
<pre>blocks as code.Commits
cli: Make keep-dirs work for local file input
determine_output_pathincli_utils.pyto correctly calculate relative paths when using--keep-dirswith--input-dir. This allows recursive processing of local files while preserving directory structure. It also fixes the issue where --keep-dirs would cause the output-dir to be ignored and output files to appear next to the original.test_keep_dirs_and_extensiontotests/cli_tests.pycovering both directory preservation and custom extensions.CLI: allow specifying the output extension
--output-extensionCLI argument to override the default extension derived from the output format. Personally I would assume markdown files defaulting to .md would be more standard but as that would be a pretty large breaking change I didn't do that.cli_utils.pyto respect the user-provided extension.extraction: Added AdvancedFlags option, added ALL_PRE_BLOCKS_ARE_CODEBLOCKS flag
AdvancedOptionsenum insettings.pyfor managing experimental flags.--advanced-flagsCLI argument.ALL_PRE_BLOCKS_ARE_CODEBLOCKSflag to force<pre>elements to be treated as code blocks.test_pre_conversion_flagtotests/unit_tests.py.extraction: Added regex option for pre-body extraction
The general idea here is to allow users to provide a regex that will be used to extract the main body of the document before any other extraction logic is applied. This is useful for documents with a consistent structure where a regex can reliably capture the desired content. I found while trafilatura largely worked great the webpages I was operating on had the body in a
and this caused some odd formatting. Using a regex to extract the body pre-processing allowed me to get a much cleaner result.Added
--regex-file-for-bodyCLI argument to allow users to provide a regex pattern for initial content extraction. We read the regex from a file rather than the CLI due to the fact regex's often have characters that can be escape or interpreted when used. Coming from a file not only avoids the requirement to get escaping correct but is shell agnostic, and allows to copy and paste the regex into a test engine easily.The API option is just "regex" and takes the actual regex pattern.
Updated
Extractorclass andbare_extractionincore.pyto apply the regex if provided.Added
test_regex_for_bodyto verify the functionality.Updated documentation to include the new option.
Testing
test_regex_for_body: Verifies regex-based body extraction.test_pre_conversion_flag: Checks the behavior ofALL_PRE_BLOCKS_ARE_CODEBLOCKS.test_keep_dirs_and_extension: Validates--keep-dirswith local inputs and--output-extension.test_code_blockexpectations for better formatting.Risks/Notes
Regex Extraction: To make things work correctly I wrap the captured groups in as otherwise shorter context may not be recognized as valid HTML. Allowing multiple capture groups should allow fairly advanced capture patterns.
Advanced Flags: The
ALL_PRE_BLOCKS_ARE_CODEBLOCKSflag changes default behavior for<pre>tags; it should be used when the target content consistently uses<pre>for code. In theory<pre>is always preformatted text so fixed blocks would seem to make sense, but again defaults to off. While it could be just another command line it would seem better to have potential flags rather than a growing list of command line options for experimental or edge case behaviors. While there is the settings file, this seems more static unlikely to change options that this didn't seem to fit into.