#!/usr/bin/env python3 """Write references/components.md, the list of the kit's components, from their docstrings. python3 skills/atlassian-design/render_kit/index.py [--check] With --check it writes nothing, and fails when components.md is out of date, when a function the package exports has no docstring, or when the icon names in references/data-format.md don't match the icon set. Run it after changing the kit. """ import ast import importlib import inspect import re import sys from pathlib import Path sys.dont_write_bytecode = True ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT)) import render_kit # noqa: E402 OUT = ROOT / 'references' / 'components.md' DATA_FORMAT = ROOT / 'references' / 'data-format.md' INTRO = '''# Kit components Generated by `render_kit/index.py` from the kit's docstrings. Don't edit it by hand: change the docstring and run `python3 skills/atlassian-design/render_kit/index.py`. Every function here is exported by the package, so a renderer reaches it as `k.` after `import render_kit as k`. Each family's CSS sits beside its module and is added to a page only when the page uses one of its classes. `tokens.css`, `ads.css`, `links.css` and `layout.css` are on every page. Don't add CSS for new visual pieces to a renderer: add them to the kit as components, with a docstring, and run this script. ''' def doc(f): """A function's docstring as written, less the 4 spaces of its body, so its example stays an indented code block. Python 3.13 and later strip that indentation from __doc__, so this reads the source.""" node = ast.parse(inspect.getsource(f)).body[0] raw = ast.get_docstring(node, clean=False) if not raw: return '' first, *rest = raw.expandtabs().splitlines() return '\n'.join([first.strip()] + [l[4:] if l.startswith(' ') else l.strip() for l in rest]).strip() def exported(mod): """The functions the package exports from a module, in source order.""" fs = [getattr(render_kit, n) for n in dir(render_kit) if not n.startswith('_')] fs = [f for f in fs if inspect.isfunction(f) and f.__module__ == mod.__name__] return sorted(fs, key=lambda f: f.__code__.co_firstlineno) def build(): page = importlib.import_module('render_kit.page') out, missing = [INTRO], [] for name in render_kit.MODULES: mod = importlib.import_module(f'render_kit.{name}') out.append(f'\n## {name}\n\n{inspect.getdoc(mod)}\n') css = ROOT / 'render_kit' / f'{name}.css' if css.is_file(): classes = ', '.join(f'`.{c}`' for c in sorted(page.css_classes(css.read_text()))) out.append(f'\nCSS: `{css.name}`, styling {classes}.\n') if name == 'icons': out.append('\nIcons: ' + ', '.join(f'`{i}`' for i in render_kit.ICONS) + '.\n') for f in exported(mod): text = doc(f) if not text: missing.append(f'{name}.{f.__name__}') out.append(f'\n### `{f.__name__}{inspect.signature(f)}`\n\n{text}\n') return ''.join(out), missing def icon_mismatch(): """Icon names listed in data-format.md's "Icon names" section that the set lacks, and the reverse.""" md = DATA_FORMAT.read_text() section = re.search(r'^## Icon names\n(.*?)(?=^## |\Z)', md, re.M | re.S) listed = set(re.findall(r'`([a-z][a-z-]*)`', section.group(1))) if section else set() return sorted(listed - set(render_kit.ICONS)), sorted(set(render_kit.ICONS) - listed) def main(): text, missing = build() problems = [f'no docstring: {m}' for m in missing] extra, unlisted = icon_mismatch() problems += [f'data-format.md lists an icon the kit lacks: {i}' for i in extra] problems += [f'data-format.md is missing the icon: {i}' for i in unlisted] if '--check' in sys.argv[1:]: if not OUT.is_file() or OUT.read_text() != text: problems.append(f'{OUT.relative_to(ROOT.parent.parent)} is out of date: run render_kit/index.py') elif not missing: OUT.write_text(text) print(f'wrote {OUT.relative_to(ROOT.parent.parent)}') if problems: sys.exit('\n'.join(problems)) if __name__ == '__main__': main()