Skip to content

Commit

Permalink
New configuration option: underlines = ["=", "-", "~"]
Browse files Browse the repository at this point in the history
  • Loading branch information
njsmith committed Aug 3, 2017
1 parent e07a7b7 commit 6da8b83
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/towncrier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def __main(draft, directory, project_version, project_date):
click.echo("Rendering news fragments...", err=to_err)

fragments = split_fragments(fragments, definitions)
rendered = render_fragments(template, fragments, definitions)
rendered = render_fragments(
# The 0th underline is used for the top line
template, fragments, definitions, config['underlines'][1:])

if not project_version:
project_version = get_version(
Expand All @@ -90,7 +92,7 @@ def __main(draft, directory, project_version, project_date):
version=project_version,
project_date=project_date
)
top_line += u"\n" + (u"=" * len(top_line)) + u"\n"
top_line += u"\n" + (config['underlines'][0] * len(top_line)) + u"\n"

if draft:
click.echo(
Expand Down
5 changes: 3 additions & 2 deletions src/towncrier/_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def split_fragments(fragments, definitions):
return output


def render_fragments(template, fragments, definitions, major=u"-", minor=u"~"):
def render_fragments(template, fragments, definitions, underlines):
"""
Render the fragments into a news file.
"""
Expand Down Expand Up @@ -125,7 +125,8 @@ def render_fragments(template, fragments, definitions, major=u"-", minor=u"~"):

done = []

res = jinja_template.render(sections=data, definitions=definitions)
res = jinja_template.render(
sections=data, definitions=definitions, underlines=underlines)

for line in res.split(u"\n"):
done.append(textwrap.fill(line, width=79, subsequent_indent=u" "))
Expand Down
3 changes: 3 additions & 0 deletions src/towncrier/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"showcontent": True}),
(u"misc", {"name": u"Misc", "showcontent": False}),
])
_underlines = ["=", "-", "~"]


def load_config_ini(from_dir):
Expand Down Expand Up @@ -68,6 +69,7 @@ def load_config_ini(from_dir):
'template': template_fname,
'start_line': start_string,
'title_format': title_format,
'underlines': _underlines,
}


Expand Down Expand Up @@ -113,6 +115,7 @@ def load_config_toml(from_dir):
'template': config.get('template', _template_fname),
'start_line': config.get('start_string', _start_string),
'title_format': config.get('title_format', _title_format),
'underlines': config.get('underlines', _underlines)
}


Expand Down
1 change: 1 addition & 0 deletions src/towncrier/newsfragments/63.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
New option in ``pyproject.toml``: ``underlines = ["=", "-", "~"]`` to specify the ReST underline hierarchy in towncrier's generated text.
4 changes: 2 additions & 2 deletions src/towncrier/templates/template.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% for section, _ in sections|dictsort(by='key') %}
{% set underline = "-" %}{% if section %}{{section}}
{{ underline * section|length }}{% set underline = "~" %}
{% set underline = underlines[0] %}{% if section %}{{section}}
{{ underline * section|length }}{% set underline = underlines[1] %}

{% endif %}

Expand Down
36 changes: 35 additions & 1 deletion src/towncrier/test/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,39 @@ def test_basic(self):
"templates/template.rst").decode('utf8')

fragments = split_fragments(fragments, definitions)
output = render_fragments(template, fragments, definitions)
output = render_fragments(template, fragments, definitions, ["-", "~"])
self.assertEqual(output, expected_output)

# Check again with non-default underlines
expected_output_weird_underlines = (u"""
Features
********
- Foo added. (#2, #72)
- Stuff! (#4)
- Fun! (baz)
Misc
****
- #1, #142, bar
Names
*****
No significant changes.
Web
***
Bugfixes
^^^^^^^^
- Web fixed. (#3)
""")

output = render_fragments(template, fragments, definitions, ["*", "^"])
self.assertEqual(output, expected_output_weird_underlines)
2 changes: 2 additions & 0 deletions src/towncrier/test/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def test_base(self):
self.assertEqual(config['package'], "foobar")
self.assertEqual(config['package_dir'], ".")
self.assertEqual(config['filename'], "NEWS.rst")
self.assertEqual(config['underlines'], ["=", "-", "~"])


class TomlSettingsTests(TestCase):
Expand All @@ -46,3 +47,4 @@ def test_base(self):
self.assertEqual(config['package'], "foobar")
self.assertEqual(config['package_dir'], ".")
self.assertEqual(config['filename'], "NEWS.rst")
self.assertEqual(config['underlines'], ["=", "-", "~"])
6 changes: 4 additions & 2 deletions src/towncrier/test/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ def test_append_at_top(self):
"NEWS.rst",
".. towncrier release notes start\n",
"MyProject 1.0\n=============\n",
render_fragments(template, fragments, definitions))
render_fragments(
template, fragments, definitions, ["-", "~"]))

with open(os.path.join(tempdir, "NEWS.rst"), "r") as f:
output = f.read()
Expand Down Expand Up @@ -181,7 +182,8 @@ def test_append_at_top_with_hint(self):
"NEWS.rst",
".. towncrier release notes start\n",
"MyProject 1.0\n=============\n",
render_fragments(template, fragments, definitions))
render_fragments(
template, fragments, definitions, ["-", "~"]))

with open(os.path.join(tempdir, "NEWS.rst"), "r") as f:
output = f.read()
Expand Down

0 comments on commit 6da8b83

Please sign in to comment.