Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add preserve_space option to keep original format in code blocks #772

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

unsleepy22
Copy link
Contributor

Currently preserve_space is forced False in sanitize method when converting xml to text, while for code blocks esp. python code we wish to preserve spaces to keep the original format since space matters in python.

Copy link

codecov bot commented Dec 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.27%. Comparing base (91c567c) to head (bc83c56).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #772   +/-   ##
=======================================
  Coverage   99.27%   99.27%           
=======================================
  Files          21       21           
  Lines        3587     3608   +21     
=======================================
+ Hits         3561     3582   +21     
  Misses         26       26           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@adbar adbar linked an issue Dec 24, 2024 that may be closed by this pull request
@adbar
Copy link
Owner

adbar commented Dec 24, 2024

Hi @unsleepy22, a few comments on the PR:

Last, if you have several PRs in store about unrelated parts of the code it would be best to send them all at once, I don't plan to maintain the software on a day-to-day basis, we could both save time by grouping requests.

@unsleepy22
Copy link
Contributor Author

Hi @unsleepy22, a few comments on the PR:

Last, if you have several PRs in store about unrelated parts of the code it would be best to send them all at once, I don't plan to maintain the software on a day-to-day basis, we could both save time by grouping requests.

Sorry for late response as I have been on vacation for a few days.

Yes, this fixes Preserve horizontal space in code blocks #553.
And also I added code to fix #769.

The preserve_spaces option does not work globally in [htmlprocessing/handle_textnode], and to keep backward-compatibility, I leave preserve_spaces to False by default.

Another change I'd like to make but needs discussion is adding another option like include_metadata_in_text to preserve/remove metadata dict string from text, but this may cause a bit confusion since we have 3 options regarding metadata, and, again, just for backward-compatibility. Another way to do this is change the default behavior of with_metadata option, which might introduce breaking changes. What do you think?

@adbar adbar linked an issue Jan 3, 2025 that may be closed by this pull request
@adbar
Copy link
Owner

adbar commented Jan 3, 2025

  • If preserve_spaces is nearly always set to false, wouldn't it be easier to just remove the parameter and consider that space should be preserved in certain elements only like code blocks?
  • I'm not sure about with_metadata, what do you want to achieve by doing this? Metadata is already added in TXT/MD format when with_metadata=True or is there a bug?

@unsleepy22
Copy link
Contributor Author

unsleepy22 commented Jan 4, 2025

  • If preserve_spaces is nearly always set to false, wouldn't it be easier to just remove the parameter and consider that space should be preserved in certain elements only like code blocks?
  • I'm not sure about with_metadata, what do you want to achieve by doing this? Metadata is already added in TXT/MD format when with_metadata=True or is there a bug?

It's ok with me to make preserve_spaces default to True, I'll change my code a bit and fix a few UTs.

Currently if with_metadata is set to True, the metadata dict string will be prepended before text, what I want is to exclude metadata dict string from text and return within the Document.

@adbar
Copy link
Owner

adbar commented Jan 5, 2025

OK then, you can change the code of preserve_spaces to avoid adding another parameter.

Concerning with_metadata this is expected result when people use extract(). To get metadata separately from the text you could just use bare_extraction(). The text is then in .raw_text, the title in .title etc.
If this is not enough please open an issue with an example of the output you want to get.

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.

Loss format or data when li contains p Preserve horizontal space in code blocks
2 participants