Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/24.1.0...HEAD)

### Added

- `$HFPR_PACKAGE_NAME` is now replaced by the package name in the PyPI readme.
The version is not available in CLI mode, therefore it's replaced by the dummy value of `your-package`.
[#64](https://github.com/hynek/hatch-fancy-pypi-readme/pull/64)

### Removed

- Support for Python 3.7.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ Again, please check out our [example configuration][example-config] for a comple

### Referencing Packaging Metadata

If the final readme contains the string `$HFPR_VERSION`, it is replaced by the current package version.
If the final readme contains the strings `$HFPR_PACKAGE_NAME` or `$HFPR_VERSION`, they will be replaced by the current package name or 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.
In that case `$HFPR_PACKAGE_NAME` is hardcoded to `your-package`, and `$HFPR_VERSION` to `42.0`, so you can still test your readme.


## CLI Interface
Expand Down
5 changes: 4 additions & 1 deletion src/hatch_fancy_pypi_readme/_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
def build_text(
fragments: list[Fragment],
substitutions: list[Substituter],
package_name: str = "",
version: str = "",
) -> str:
"""
Expand All @@ -26,4 +27,6 @@ def build_text(
for sub in substitutions:
text = sub.substitute(text)

return text.replace("$HFPR_VERSION", version)
return text.replace("$HFPR_PACKAGE_NAME", package_name).replace(
"$HFPR_VERSION", version
)
7 changes: 6 additions & 1 deletion src/hatch_fancy_pypi_readme/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ def cli_run(
+ "\n".join(f"- {msg}" for msg in e.errors),
)

print(build_text(config.fragments, config.substitutions, "42.0"), file=out)
print(
build_text(
config.fragments, config.substitutions, "your-package", "42.0"
),
file=out,
)


def _fail(msg: str) -> NoReturn:
Expand Down
1 change: 1 addition & 0 deletions src/hatch_fancy_pypi_readme/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def update(self, metadata: dict[str, Any]) -> None:
"text": build_text(
config.fragments,
config.substitutions,
package_name=metadata.get("name", ""),
version=metadata.get("version", ""),
),
}
Expand Down
12 changes: 10 additions & 2 deletions tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ def test_single_text_fragment(self):
"""
A single text fragment becomes the readme.
"""
assert "This is the README for 1.0!" == build_text(
[TextFragment("This is the README for $HFPR_VERSION!")], [], "1.0"
assert "This is the README for your-package 1.0!" == build_text(
[
TextFragment(
"This is the README for $HFPR_PACKAGE_NAME $HFPR_VERSION!"
)
],
[],
"your-package",
"1.0",
)

def test_multiple_text_fragment(self):
Expand All @@ -26,5 +33,6 @@ def test_multiple_text_fragment(self):
TextFragment("This is the README!"),
],
[],
"your-package",
"1.0",
)