|
| 1 | +from datetime import datetime |
| 2 | + |
| 3 | +from docutils import nodes |
| 4 | +from docutils.parsers.rst import directives |
| 5 | +from sphinx.util.docutils import SphinxDirective |
| 6 | + |
| 7 | + |
| 8 | +class ReleaseDirective(SphinxDirective): |
| 9 | + has_content = True |
| 10 | + required_arguments = 1 |
| 11 | + optional_arguments = 0 |
| 12 | + option_spec = { |
| 13 | + "date": directives.unchanged_required |
| 14 | + } |
| 15 | + |
| 16 | + def run(self): |
| 17 | + """Creates a section for release notes with version and date.""" |
| 18 | + version = self.arguments[0] |
| 19 | + |
| 20 | + # Fetch today's date as default if no date is provided |
| 21 | + today_date_str = datetime.now().strftime("%Y-%m-%d") |
| 22 | + date_str = self.options.get("date", today_date_str) |
| 23 | + |
| 24 | + try: |
| 25 | + parsed_date = datetime.strptime(date_str, "%Y-%m-%d") |
| 26 | + release_date = parsed_date.strftime("%e %B %Y") |
| 27 | + except ValueError: |
| 28 | + raise ValueError(f"Invalid date format: {date_str}") |
| 29 | + |
| 30 | + # Create the version title node |
| 31 | + version_node = nodes.strong(text=version) |
| 32 | + section_title = nodes.title("", "", version_node) |
| 33 | + |
| 34 | + # Create the section node with a specific ID |
| 35 | + section_id = f"release-{version.replace(' ', '-')}" |
| 36 | + section = nodes.section( |
| 37 | + "", section_title, |
| 38 | + ids=[section_id], |
| 39 | + classes=["changelog-release"] |
| 40 | + ) |
| 41 | + |
| 42 | + # Append formatted date |
| 43 | + section.append( |
| 44 | + nodes.emphasis(text=release_date, classes=["release-date"]) |
| 45 | + ) |
| 46 | + |
| 47 | + # Parse content into a list of changes |
| 48 | + content_node = nodes.Element() |
| 49 | + self.state.nested_parse(self.content, self.content_offset, content_node) |
| 50 | + |
| 51 | + # Create a bullet list of changes |
| 52 | + changes_list = nodes.bullet_list("", classes=["changelog-change-list"]) |
| 53 | + for child in content_node: |
| 54 | + item = nodes.list_item("") |
| 55 | + item.append(child) |
| 56 | + changes_list.append(item) |
| 57 | + |
| 58 | + section.append(changes_list) |
| 59 | + |
| 60 | + return [section] |
| 61 | + |
| 62 | + |
| 63 | +class ChangeDirective(SphinxDirective): |
| 64 | + has_content = True |
| 65 | + required_arguments = 1 |
| 66 | + optional_arguments = 0 |
| 67 | + |
| 68 | + def run(self): |
| 69 | + """Generates a categorized list item for a changelog entry.""" |
| 70 | + category = self.arguments[0] |
| 71 | + |
| 72 | + # Create a paragraph for the category with specific styling |
| 73 | + class_name = f"changelog-category-{category.lower().replace(' ', '-')}" |
| 74 | + category_node = nodes.inline( |
| 75 | + "", category, |
| 76 | + classes=["changelog-category", class_name] |
| 77 | + ) |
| 78 | + paragraph_node = nodes.paragraph("", "", category_node) |
| 79 | + |
| 80 | + # Parse the detailed content under the category |
| 81 | + content_node = nodes.container() |
| 82 | + self.state.nested_parse(self.content, 0, content_node) |
| 83 | + paragraph_node += content_node |
| 84 | + |
| 85 | + return [paragraph_node] |
| 86 | + |
| 87 | + |
| 88 | +def setup(app): |
| 89 | + """Register extension with Sphinx.""" |
| 90 | + app.add_directive("release", ReleaseDirective) |
| 91 | + app.add_directive("change", ChangeDirective) |
0 commit comments