Skip to content

Commit dde5da2

Browse files
authored
Add $HFPR_VERSION substitution (#39)
1 parent 27c7a0e commit dde5da2

File tree

6 files changed

+29
-8
lines changed

6 files changed

+29
-8
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ The **third number** is for emergencies when we need to start branches for older
1313

1414
## [Unreleased](https://github.com/hynek/hatch-fancy-pypi-readme/compare/23.1.0...HEAD)
1515

16+
### Added
17+
18+
- `$HFPR_VERSION` is now replaced by the package version in the PyPI readme.
19+
The version is not available in CLI mode, therefore it's replaced by the dummy value of `42.0`.
20+
[#39](https://github.com/hynek/hatch-fancy-pypi-readme/pull/39)
21+
22+
1623
## [23.1.0](https://github.com/hynek/hatch-fancy-pypi-readme/compare/22.8.0...23.1.0) - 2023-05-22
1724

1825
### Added

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,14 @@ replacement = "[#\\1](https://github.com/hynek/hatch-fancy-pypi-readme/issues/\\
230230
Again, please check out our [example configuration][example-config] for a complete example.
231231

232232

233+
### Referencing Packaging Metadata
234+
235+
If the final contains the string `$HFPR_VERSION`, it is replaced by the current package version.
236+
237+
When running *hatch-fancy-pypi-readme* in CLI mode (as described in the next section), packaging metadata is not available.
238+
In that case `$HFPR_VERSION` is hardcoded to `42.0` so you can still test your readme.
239+
240+
233241
## CLI Interface
234242

235243
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/):
243251

244252

245253
```shell
246-
pipx run hatch-fancy-pypi-readme
254+
$ pipx run hatch-fancy-pypi-readme
247255
```
248256

249257
---

src/hatch_fancy_pypi_readme/_builder.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313

1414

1515
def build_text(
16-
fragments: list[Fragment], substitutions: list[Substituter]
16+
fragments: list[Fragment],
17+
substitutions: list[Substituter],
18+
version: str,
1719
) -> str:
1820
text = "".join(f.render() for f in fragments)
1921

2022
for sub in substitutions:
2123
text = sub.substitute(text)
2224

23-
return text
25+
return text.replace("$HFPR_VERSION", version)

src/hatch_fancy_pypi_readme/_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def cli_run(
6565
+ "\n".join(f"- {msg}" for msg in e.errors),
6666
)
6767

68-
print(build_text(config.fragments, config.substitutions), file=out)
68+
print(build_text(config.fragments, config.substitutions, "42.0"), file=out)
6969

7070

7171
def _fail(msg: str) -> NoReturn:

src/hatch_fancy_pypi_readme/hooks.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ def update(self, metadata: dict[str, Any]) -> None:
2020
"""
2121
Update the project table's metadata.
2222
"""
23-
2423
config = load_and_validate_config(self.config)
2524

2625
metadata["readme"] = {
2726
"content-type": config.content_type,
28-
"text": build_text(config.fragments, config.substitutions),
27+
"text": build_text(
28+
config.fragments,
29+
config.substitutions,
30+
version=metadata.get("version", ""),
31+
),
2932
}
3033

3134

tests/test_builder.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def test_single_text_fragment(self):
1111
"""
1212
A single text fragment becomes the readme.
1313
"""
14-
assert "This is the README!" == build_text(
15-
[TextFragment("This is the README!")], []
14+
assert "This is the README for 1.0!" == build_text(
15+
[TextFragment("This is the README for $HFPR_VERSION!")], [], "1.0"
1616
)
1717

1818
def test_multiple_text_fragment(self):
@@ -26,4 +26,5 @@ def test_multiple_text_fragment(self):
2626
TextFragment("This is the README!"),
2727
],
2828
[],
29+
"1.0",
2930
)

0 commit comments

Comments
 (0)