Skip to content

Commit 86eafa2

Browse files
committed
Refactor rendering of title via config.title_format (twisted#610)
* Refactor rendering of title via config.title_format * Add newsfragment * Config docs * Fix restructuredtext formatting error
1 parent be9bc2b commit 86eafa2

File tree

4 files changed

+71
-24
lines changed

4 files changed

+71
-24
lines changed

docs/configuration.rst

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ Top level keys
8080

8181
``""`` by default.
8282

83+
Formatted titles are appended a line of ``=`` on the following line (reStructuredText title format) unless the template has an ``.md`` suffix, in which case the title will instead be prefixed with ``#`` (markdown title format).
84+
8385
``issue_format``
8486
A format string for rendering the issue/ticket number in newsfiles.
8587

src/towncrier/build.py

+18-24
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,11 @@ def __main(
170170
.joinpath(config.template[1])
171171
.read_text(encoding="utf-8")
172172
)
173+
template_extension = os.path.splitext(config.template[1])[1]
173174
else:
174175
template = Path(config.template).read_text(encoding="utf-8")
176+
template_extension = os.path.splitext(config.template)[1]
177+
is_markdown = template_extension.lower() == ".md"
175178

176179
click.echo("Finding news fragments...", err=to_err)
177180

@@ -215,22 +218,10 @@ def __main(
215218
if project_date is None:
216219
project_date = _get_date().strip()
217220

218-
if config.title_format:
219-
top_line = config.title_format.format(
220-
name=project_name, version=project_version, project_date=project_date
221-
)
222-
render_title_with_fragments = False
223-
render_title_separately = True
224-
elif config.title_format is False:
225-
# This is an odd check but since we support both "" and False with
226-
# different effects we have to do something a bit abnormal here.
227-
top_line = ""
228-
render_title_separately = False
229-
render_title_with_fragments = False
230-
else:
231-
top_line = ""
232-
render_title_separately = False
233-
render_title_with_fragments = True
221+
# Render the title in the template if the title format is set to "". It can
222+
# alternatively be set to False or a string, in either case it shouldn't be rendered
223+
# in the template.
224+
render_title = config.title_format == ""
234225

235226
rendered = render_fragments(
236227
# The 0th underline is used for the top line
@@ -243,18 +234,21 @@ def __main(
243234
{"name": project_name, "version": project_version, "date": project_date},
244235
top_underline=config.underlines[0],
245236
all_bullets=config.all_bullets,
246-
render_title=render_title_with_fragments,
237+
render_title=render_title,
247238
)
248239

249-
if render_title_separately:
250-
content = "\n".join(
251-
[
252-
top_line,
253-
config.underlines[0] * len(top_line),
254-
rendered,
255-
]
240+
if config.title_format:
241+
top_line = config.title_format.format(
242+
name=project_name, version=project_version, project_date=project_date
256243
)
244+
if is_markdown:
245+
parts = [f"# {top_line}"]
246+
else:
247+
parts = [top_line, config.underlines[0] * len(top_line)]
248+
parts.append(rendered)
249+
content = "\n".join(parts)
257250
else:
251+
top_line = ""
258252
content = rendered
259253

260254
if draft:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The ``title_format`` configuration option now uses a markdown format for markdown templates.

src/towncrier/test/test_build.py

+50
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,56 @@ def test_title_format_custom(self, runner):
10321032
10331033
10341034
1035+
"""
1036+
)
1037+
1038+
self.assertEqual(0, result.exit_code)
1039+
self.assertEqual(expected_output, result.output)
1040+
1041+
@with_project(
1042+
config="""
1043+
[tool.towncrier]
1044+
package = "foo"
1045+
filename = "NEWS.md"
1046+
title_format = "[{project_date}] CUSTOM RELEASE for {name} version {version}"
1047+
"""
1048+
)
1049+
def test_title_format_custom_markdown(self, runner):
1050+
"""
1051+
A non-empty title format adds the specified title, and if the target filename is
1052+
markdown then the title is added as a markdown header.
1053+
"""
1054+
with open("foo/newsfragments/123.feature", "w") as f:
1055+
f.write("Adds levitation")
1056+
result = runner.invoke(
1057+
_main,
1058+
[
1059+
"--name",
1060+
"FooBarBaz",
1061+
"--version",
1062+
"7.8.9",
1063+
"--date",
1064+
"20-01-2001",
1065+
"--draft",
1066+
],
1067+
)
1068+
1069+
expected_output = dedent(
1070+
"""\
1071+
Loading template...
1072+
Finding news fragments...
1073+
Rendering news fragments...
1074+
Draft only -- nothing has been written.
1075+
What is seen below is what would be written.
1076+
1077+
# [20-01-2001] CUSTOM RELEASE for FooBarBaz version 7.8.9
1078+
1079+
### Features
1080+
1081+
- Adds levitation (#123)
1082+
1083+
1084+
10351085
"""
10361086
)
10371087

0 commit comments

Comments
 (0)