|
| 1 | +# ******************************************************************************* |
| 2 | +# Copyright (c) 2026 Contributors to the Eclipse Foundation |
| 3 | +# |
| 4 | +# See the NOTICE file(s) distributed with this work for additional |
| 5 | +# information regarding copyright ownership. |
| 6 | +# |
| 7 | +# This program and the accompanying materials are made available under the |
| 8 | +# terms of the Apache License Version 2.0 which is available at |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# SPDX-License-Identifier: Apache-2.0 |
| 12 | +# ******************************************************************************* |
| 13 | + |
| 14 | +"""Shared helper for generating RST wrapper pages for PlantUML diagram files.""" |
| 15 | + |
| 16 | +def make_puml_rst_wrappers(ctx, puml_files, output_dir, template, strip_prefix = "", filename_prefix = ""): |
| 17 | + """Generate a thin RST wrapper page for each PlantUML diagram file. |
| 18 | +
|
| 19 | + The wrapper embeds the diagram via ``.. uml::`` so it appears as a |
| 20 | + proper toctree entry while keeping the source ``.puml`` file separate. |
| 21 | +
|
| 22 | + Args: |
| 23 | + ctx: Rule context. |
| 24 | + puml_files: Iterable of File objects whose extension is ``puml`` or |
| 25 | + ``plantuml``. |
| 26 | + output_dir: String prefix for declared output files |
| 27 | + (e.g. ``ctx.label.name``). |
| 28 | + template: The ``puml_diagram.template.rst`` File (from |
| 29 | + ``ctx.file._puml_rst_template``). |
| 30 | + strip_prefix: Optional filename stem prefix to strip before deriving |
| 31 | + the human-readable title (e.g. ``"fta_"``). |
| 32 | + filename_prefix: Optional prefix prepended to the output RST filename |
| 33 | + stem (e.g. ``"detail_"``). |
| 34 | +
|
| 35 | + Returns: |
| 36 | + List of declared ``.rst`` output Files, one per input diagram. |
| 37 | + """ |
| 38 | + wrappers = [] |
| 39 | + for f in puml_files: |
| 40 | + if f.extension not in ("puml", "plantuml"): |
| 41 | + continue |
| 42 | + stem = f.basename[:-(len(f.extension) + 1)] |
| 43 | + if strip_prefix and stem.startswith(strip_prefix): |
| 44 | + stem = stem[len(strip_prefix):] |
| 45 | + title = stem.replace("_", " ").title() |
| 46 | + wrapper = ctx.actions.declare_file( |
| 47 | + "{}/{}{}.rst".format(output_dir, filename_prefix, stem), |
| 48 | + ) |
| 49 | + ctx.actions.expand_template( |
| 50 | + template = template, |
| 51 | + output = wrapper, |
| 52 | + substitutions = { |
| 53 | + "{title}": title, |
| 54 | + "{underline}": "=" * len(title), |
| 55 | + "{basename}": f.basename, |
| 56 | + }, |
| 57 | + ) |
| 58 | + wrappers.append(wrapper) |
| 59 | + return wrappers |
0 commit comments