Skip to content

Commit

Permalink
docs: generate changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
sethfischer committed Feb 15, 2024
1 parent 39540d7 commit b4acccc
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
/_build
/dist
/docs/_build
CHANGELOG.md
CHANGELOG.*
__pycache__
1 change: 1 addition & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ build:
python: "3.9"
jobs:
post_checkout:
- git fetch --unshallow || true
- >
git_lfs_ver="3.3.0";
wget -qO- https://github.com/git-lfs/git-lfs/releases/download/v${git_lfs_ver}/git-lfs-linux-amd64-v${git_lfs_ver}.tar.gz
Expand Down
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
=========
Changelog
=========

.. osr:cz-changelog::
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,6 @@ Specifications
console
related-projects
about
changelog
copyright
indices
50 changes: 50 additions & 0 deletions src/osr_sphinx/commitizen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Commitizen Sphinx directives."""

from __future__ import annotations

import subprocess

from docutils import nodes
from docutils.frontend import OptionParser
from docutils.utils import new_document
from sphinx.parsers import RSTParser
from sphinx.util.docutils import SphinxDirective


class CzChangelog(SphinxDirective):
"""Commitizen changelog directive."""

has_content = False
required_arguments = 0
optional_arguments = 0

def run(self) -> list[nodes.Node]:
"""Run Commitizen changelog."""
changelog = self.get_changelog()

return self.parse_rst(changelog)

@staticmethod
def get_changelog() -> str:
"""Execute cz changelog and capture output."""
result = subprocess.run(
["cz", "changelog", "--dry-run", "--file-name", "CHANGELOG.rst"],
stdout=subprocess.PIPE,
)

return result.stdout.decode("utf-8")

def parse_rst(self, text: str) -> list[nodes.Node]:
"""Parse reStructuredText string."""
parser = RSTParser()
parser.set_application(self.env.app)
settings = OptionParser(
defaults=self.env.settings,
components=(RSTParser,),
read_config_files=True,
).get_default_values()

document = new_document("<rst-doc>", settings=settings)
parser.parse(text, document)

return document.children
2 changes: 2 additions & 0 deletions src/osr_sphinx/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from sphinx.environment import BuildEnvironment

from osr_sphinx.bom import BomTable
from osr_sphinx.commitizen import CzChangelog
from osr_sphinx.dimensions import DimensionRole
from osr_sphinx.print_settings import PrintSettings

Expand All @@ -23,6 +24,7 @@ class OsrDomain(Domain):

directives = {
"bom": BomTable,
"cz-changelog": CzChangelog,
"print-settings": PrintSettings,
}
roles = {
Expand Down

0 comments on commit b4acccc

Please sign in to comment.