|
1 | 1 | """
|
2 | 2 | Generates the release notes for the latest release, in Markdown.
|
3 | 3 |
|
4 |
| -Convert CHANGELOG.rst to Markdown, and extracts just the latest release. |
5 |
| -
|
6 |
| -Writes to ``scripts/latest-release-notes.md``, which can be |
7 |
| -used with https://github.com/softprops/action-gh-release. |
| 4 | +1. Extracts the latest release from the CHANGELOG.rst file. |
| 5 | +2. Converts it to Markdown using pypandoc. |
| 6 | +3. Writes to ``scripts/latest-release-notes.md``, which can be |
| 7 | + used with https://github.com/softprops/action-gh-release. |
8 | 8 | """
|
9 | 9 | from pathlib import Path
|
10 | 10 |
|
11 | 11 | import pypandoc
|
12 | 12 |
|
13 | 13 | this_dir = Path(__file__).parent
|
14 | 14 | rst_text = (this_dir.parent / "CHANGELOG.rst").read_text(encoding="UTF-8")
|
15 |
| -md_text = pypandoc.convert_text( |
16 |
| - rst_text, "md", format="rst", extra_args=["--wrap=preserve"] |
17 |
| -) |
18 | 15 |
|
19 | 16 | output_lines = []
|
20 |
| -first_heading_found = False |
21 |
| -for line in md_text.splitlines(): |
22 |
| - if line.startswith("# "): |
23 |
| - # Skip the first section (normally # Releases). |
24 |
| - pass |
25 |
| - elif line.startswith("## "): |
26 |
| - # First second-level section, note it and skip the text, |
27 |
| - # as we are only interested in the individual release items. |
28 |
| - if first_heading_found: |
| 17 | +capture = False |
| 18 | +for line in rst_text.splitlines(): |
| 19 | + # Only start capturing after the latest release section. |
| 20 | + if line.startswith("-------"): |
| 21 | + capture = not capture |
| 22 | + if not capture: |
| 23 | + # We only need to capture the latest release, so stop. |
29 | 24 | break
|
30 |
| - first_heading_found = True |
31 |
| - else: |
| 25 | + continue |
| 26 | + |
| 27 | + if capture: |
32 | 28 | output_lines.append(line)
|
33 | 29 |
|
| 30 | +# Drop last line, as it contains the previous release section title. |
| 31 | +del output_lines[-1] |
| 32 | + |
| 33 | +trimmed_rst = "\n".join(output_lines).strip() |
| 34 | +print(">>Trimmed RST follows:") |
| 35 | +print(trimmed_rst) |
| 36 | +print(">>Trimmed RST ends") |
| 37 | + |
| 38 | +md_text = pypandoc.convert_text( |
| 39 | + trimmed_rst, "md", format="rst", extra_args=["--wrap=preserve"] |
| 40 | +) |
| 41 | +print(">>Converted Markdown follows:") |
| 42 | +print(md_text) |
| 43 | +print(">>Converted Markdown ends") |
| 44 | + |
34 | 45 | output_fn = this_dir / "latest-release-notes.md"
|
35 |
| -output_fn.write_text("\n".join(output_lines), encoding="UTF-8") |
| 46 | +output_fn.write_text(md_text, encoding="UTF-8") |
36 | 47 | print(output_fn, "generated.")
|
0 commit comments