From 2afbbb4246d6957360bcb3f885d4fbe91de119b9 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Sat, 30 Dec 2023 12:52:43 +0100 Subject: [PATCH 1/2] Add $HFPR_VERSION substitution --- CHANGELOG.md | 6 ++++++ README.md | 10 +++++++++- src/hatch_fancy_pypi_readme/_builder.py | 6 ++++-- src/hatch_fancy_pypi_readme/_cli.py | 2 +- src/hatch_fancy_pypi_readme/hooks.py | 7 +++++-- tests/test_builder.py | 5 +++-- 6 files changed, 28 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 896180d..3f9b261 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,12 @@ The **third number** is for emergencies when we need to start branches for older ## [Unreleased](https://github.com/hynek/hatch-fancy-pypi-readme/compare/23.1.0...HEAD) +### Added + +- `$HFPR_VERSION` is now replaced by the package version in the PyPI readme. + The version is not available in CLI mode, therefore it's replaced by the dummy value of `42.0`. + + ## [23.1.0](https://github.com/hynek/hatch-fancy-pypi-readme/compare/22.8.0...23.1.0) - 2023-05-22 ### Added diff --git a/README.md b/README.md index 186a7b9..5f57243 100644 --- a/README.md +++ b/README.md @@ -230,6 +230,14 @@ replacement = "[#\\1](https://github.com/hynek/hatch-fancy-pypi-readme/issues/\\ Again, please check out our [example configuration][example-config] for a complete example. +### Referencing Packaging Metadata + +If the final contains the string `$HFPR_VERSION`, it is replaced by the current package version. + +When running *hatch-fancy-pypi-readme* in CLI mode (as described in the next section), packaging metadata is not available. +In that case `$HFPR_VERSION` is hardcoded to `42.0` so you can still test your readme. + + ## CLI Interface For faster feedback loops, *hatch-fancy-pypi-readme* comes with a CLI interface that takes a `pyproject.toml` file as an argument and renders out the readme that would go into respective package. @@ -243,7 +251,7 @@ Therefore we recommend running it using [*pipx*](https://pypa.github.io/pipx/): ```shell -pipx run hatch-fancy-pypi-readme +$ pipx run hatch-fancy-pypi-readme ``` --- diff --git a/src/hatch_fancy_pypi_readme/_builder.py b/src/hatch_fancy_pypi_readme/_builder.py index 6be1560..b17e2ee 100644 --- a/src/hatch_fancy_pypi_readme/_builder.py +++ b/src/hatch_fancy_pypi_readme/_builder.py @@ -13,11 +13,13 @@ def build_text( - fragments: list[Fragment], substitutions: list[Substituter] + fragments: list[Fragment], + substitutions: list[Substituter], + version: str, ) -> str: text = "".join(f.render() for f in fragments) for sub in substitutions: text = sub.substitute(text) - return text + return text.replace("$HFPR_VERSION", version) diff --git a/src/hatch_fancy_pypi_readme/_cli.py b/src/hatch_fancy_pypi_readme/_cli.py index 33fb0f2..e4c3320 100644 --- a/src/hatch_fancy_pypi_readme/_cli.py +++ b/src/hatch_fancy_pypi_readme/_cli.py @@ -65,7 +65,7 @@ def cli_run( + "\n".join(f"- {msg}" for msg in e.errors), ) - print(build_text(config.fragments, config.substitutions), file=out) + print(build_text(config.fragments, config.substitutions, "42.0"), file=out) def _fail(msg: str) -> NoReturn: diff --git a/src/hatch_fancy_pypi_readme/hooks.py b/src/hatch_fancy_pypi_readme/hooks.py index 0f7a773..97c4ef2 100644 --- a/src/hatch_fancy_pypi_readme/hooks.py +++ b/src/hatch_fancy_pypi_readme/hooks.py @@ -20,12 +20,15 @@ def update(self, metadata: dict[str, Any]) -> None: """ Update the project table's metadata. """ - config = load_and_validate_config(self.config) metadata["readme"] = { "content-type": config.content_type, - "text": build_text(config.fragments, config.substitutions), + "text": build_text( + config.fragments, + config.substitutions, + version=metadata.get("version", ""), + ), } diff --git a/tests/test_builder.py b/tests/test_builder.py index e821469..205de53 100644 --- a/tests/test_builder.py +++ b/tests/test_builder.py @@ -11,8 +11,8 @@ def test_single_text_fragment(self): """ A single text fragment becomes the readme. """ - assert "This is the README!" == build_text( - [TextFragment("This is the README!")], [] + assert "This is the README for 1.0!" == build_text( + [TextFragment("This is the README for $HFPR_VERSION!")], [], "1.0" ) def test_multiple_text_fragment(self): @@ -26,4 +26,5 @@ def test_multiple_text_fragment(self): TextFragment("This is the README!"), ], [], + "1.0", ) From c22faf5966fa095c13a4f8b3b59870248c25ea61 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Sat, 30 Dec 2023 13:03:51 +0100 Subject: [PATCH 2/2] Add PR# --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f9b261..7c3920c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ The **third number** is for emergencies when we need to start branches for older - `$HFPR_VERSION` is now replaced by the package version in the PyPI readme. The version is not available in CLI mode, therefore it's replaced by the dummy value of `42.0`. + [#39](https://github.com/hynek/hatch-fancy-pypi-readme/pull/39) ## [23.1.0](https://github.com/hynek/hatch-fancy-pypi-readme/compare/22.8.0...23.1.0) - 2023-05-22