Repository navigation
New Feature: Add configurable HTML hook before PDF generation #128
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
Open
jdillard
wants to merge
13
commits into
useblocks:main
Choose a base branch
from
jdillard:html-hook
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
30e14d8
Add hook to manipulate HTML
jdillard 0ff5424
use a set function name
joby-jareddillard a297fc7
Merge branch 'main' into html-hook
jdillard 234b9ee
Merge branch 'main' into html-hook
joby-jareddillard dfbc73f
Drop the version bump from this PR
joby-jareddillard efb5519
run ruff formatting
joby-jareddillard 6a8e2c8
Make the None return an error, not a warning
joby-jareddillard 5110ecb
Load the hook once in __init__, not on every call to _execute_html_hook
joby-jareddillard 80c7ee5
Guard against spec_from_file_location returning None and use virtual …
joby-jareddillard b229cc0
fix mypy and ruff now that i have pre-commit installed
joby-jareddillard eaf2a07
Add tests
joby-jareddillard 6ba694d
Add a note that it's intentionally single-hook
joby-jareddillard 5711ecb
Merge branch 'main' into html-hook
joby-jareddillard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| """Test project for simplepdf_html_hook.""" | ||
|
|
||
| project = "HtmlHookTest" | ||
| extensions = ["sphinx_simplepdf"] | ||
| master_doc = "index" | ||
| exclude_patterns = ["_build"] | ||
|
|
||
| simplepdf_theme = "simplepdf_theme" | ||
| simplepdf_html_hook = "hooks/ok.py" |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| def not_html_hook() -> None: | ||
| pass |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from bs4 import BeautifulSoup | ||
|
|
||
|
|
||
| def html_hook(soup: BeautifulSoup, app: object) -> BeautifulSoup: | ||
| return soup |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from bs4 import BeautifulSoup | ||
|
|
||
|
|
||
| def html_hook(soup: BeautifulSoup, app: object) -> None: | ||
| return None |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| HTML hook test document | ||
| ======================= | ||
|
|
||
| Minimal content for SimplePDF hook tests. | ||
|
|
||
| Section | ||
| ------- | ||
|
|
||
| Body. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| """Tests for simplepdf_html_hook configuration.""" | ||
|
|
||
| import pytest | ||
| from sphinx.errors import ConfigError, ExtensionError | ||
|
|
||
| from .utils import build_and_capture_stdout | ||
|
|
||
|
|
||
| def test_html_hook_valid_builds(sphinx_build, capsys): | ||
| """Configured hook script loads and build completes.""" | ||
| result = build_and_capture_stdout(sphinx_build, capsys, srcdir="html_hook") | ||
| assert result.pdf_exists() | ||
| assert not result.has_warnings("ERROR:") | ||
|
|
||
|
|
||
| def test_html_hook_missing_file_raises(sphinx_build): | ||
| with pytest.raises(ConfigError, match="not found"): | ||
| sphinx_build( | ||
| srcdir="html_hook", | ||
| confoverrides={"simplepdf_html_hook": "./does_not_exist_hook.py"}, | ||
| ).build() | ||
|
|
||
|
|
||
| def test_html_hook_missing_function_raises(sphinx_build): | ||
| with pytest.raises(ConfigError, match="html_hook"): | ||
| sphinx_build( | ||
| srcdir="html_hook", | ||
| confoverrides={"simplepdf_html_hook": "hooks/missing_def.py"}, | ||
| ).build() | ||
|
|
||
|
|
||
| def test_html_hook_returns_none_raises(sphinx_build): | ||
| with pytest.raises(ExtensionError, match="returned None"): | ||
| sphinx_build( | ||
| srcdir="html_hook", | ||
| confoverrides={"simplepdf_html_hook": "hooks/returns_none.py"}, | ||
| ).build() |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No tests. The project has no test harness, I know. But
_load_html_hookis testable in isolation — give it a temp file with a function, assert it loads. Give it a file withouthtml_hook, assert it raises. Give it a nonexistent path, assert it raises. Four lines each. Even without a formal test framework, aif __name__ == "__main__"smoke test at the bottom of the module would be better than nothing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I merged
main, since the test framework was added, and added tests in eaf2a07