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

Rework Files to support generated files #3451

Merged
merged 8 commits into from Jan 26, 2024
Merged

Rework Files to support generated files #3451

merged 8 commits into from Jan 26, 2024

Conversation

oprypin
Copy link
Contributor

@oprypin oprypin commented Oct 30, 2023

@oprypin oprypin force-pushed the genfile branch 3 times, most recently from 15e06eb to 03f7e9e Compare December 15, 2023 22:11
@oprypin oprypin marked this pull request as ready for review December 15, 2023 22:12
oprypin and others added 8 commits December 15, 2023 23:42
The previous attempt always computed these on the fly #3017
but now they will be cached after first access, so the performance penalty is avoided.

The fields can still be overwritten manually.
And one can ask the fields to be recomputed by using the `del` operation.
Co-authored-by: Tim Vink <vinktim@gmail.com>
Add a new field `content_bytes`/`content_string` to read the content regardless of whether it's backed by the new style or still by `abs_src_path` (which is now not always present)
-it populates `generated_by` automatically as well, based on the currently running plugin event.
This means plugins don't have to carefully find the sort order to put files in.
@oprypin
Copy link
Contributor Author

oprypin commented Dec 16, 2023

Ready for review

@oprypin
Copy link
Contributor Author

oprypin commented Dec 16, 2023

The File.generated constructor lets you replace this:

Before:

with open(os.path.join(temp_dir, 'new_file.md'), 'w', encoding='utf-8') as f:
    f.write("content")
f = File('new_file.md', temp_dir, config.site_dir, config.use_directory_urls)
f.generated_by = 'my-plugin'
files.append(f)

After, option 1:

with open(any_tmp_path, 'w', encoding='utf-8') as f:
    f.write("content")
f = File.generated(config, 'new_file.md', abs_src_path=any_tmp_path)
files.append(f)

After, option 2:

f = File.generated(config, 'new_file.md', content="content")
files.append(f)

@squidfunk
Copy link
Sponsor Contributor

Awesome, that looks like a huge quality of life improvement for plugin authors 👏

@oprypin oprypin merged commit 5dc51b7 into master Jan 26, 2024
32 checks passed
@oprypin oprypin deleted the genfile branch January 26, 2024 20:01
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.

None yet

2 participants