-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_label_docs.py
More file actions
225 lines (180 loc) · 6.97 KB
/
Copy pathgenerate_label_docs.py
File metadata and controls
225 lines (180 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python3
"""Generate markdown documentation from a labels JSON definition file.
Usage:
python generate_label_docs.py
python generate_label_docs.py --output LABELS.md
python generate_label_docs.py --file my-labels.json
"""
import argparse
import json
import os
import sys
from urllib.parse import quote
DEFAULT_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "labels.json")
def load_labels(path):
with open(path, encoding="utf-8") as f:
return json.load(f)
def badge_url(label_text, color):
"""Generate a shields.io badge URL for a label."""
# Shields.io uses '-' as segment separator and '_' as space.
# Escape literal hyphens as '--' and underscores as '__' before URL-encoding.
escaped = label_text.replace("-", "--").replace("_", "__")
encoded = quote(escaped, safe="")
c = color.lstrip("#")
return f"https://img.shields.io/badge/{encoded}-{c}?style=flat&color={c}"
def badge_md(label_text, color, alt=None):
"""Generate a markdown image tag for a shields.io badge."""
if alt is None:
alt = label_text.replace(":", "").replace("/", "").strip()
url = badge_url(label_text, color)
return f""
def group_badge_url(group_name, color):
"""Generate a shields.io badge URL for a group overview row."""
name_lower = group_name.lower().split()[0]
c = color.lstrip("#")
return f"https://img.shields.io/badge/{quote(name_lower, safe='')}-blue?color=%23{c}&labelColor={c}"
def group_badge_md(group_name, color):
name_lower = group_name.lower().split()[0]
c = color.lstrip("#")
url = group_badge_url(group_name, color)
return f""
def group_labels(labels):
"""Group a list of labels by their 'group' field, preserving order."""
groups = {}
order = []
for label in labels:
g = label.get("group", "Other")
if g not in groups:
groups[g] = []
order.append(g)
groups[g].append(label)
return [(g, groups[g]) for g in order]
def generate_markdown(data):
lines = []
# Title
lines.append("# 3DCityDB Tagging system")
lines.append("")
# Overview table
lines.append("## Overview")
lines.append("")
lines.append("| Group | Base Color | Description | Example |")
lines.append("| :--- | :--- | :--- | :--- |")
groups = {g["name"]: g for g in data["groups"]}
for group in data["groups"]:
name = group["name"]
color = group["base_color"]
desc = group["description"]
badge = group_badge_md(name, color)
lines.append(f"| **{name}** | `{color}` | {desc} | {badge} |")
lines.append("")
# List of all tags (summary table)
all_labels = list(data["shared_labels"])
# Collect all repo-specific labels (deduplicated by name for the summary)
seen = {l["name"].lower() for l in all_labels}
for repo_labels in data.get("repo_labels", {}).values():
for label in repo_labels:
if label["name"].lower() not in seen:
all_labels.append(label)
seen.add(label["name"].lower())
lines.append("## List of all tags")
lines.append("")
lines.append("| Label | Group | Description | Preview |")
lines.append("| :--- | :--- | :--- | :--- |")
for label in all_labels:
name = label["name"]
group = label.get("group", "")
# Use a short description for the summary table
desc = label.get("description", "")
# Truncate long descriptions for the summary
if len(desc) > 60:
desc = desc[:57].rsplit(" ", 1)[0] + "..."
badge = badge_md(name, label["color"])
lines.append(f"| `{name}` | {group} | {desc} | {badge} |")
lines.append("")
lines.append("---")
lines.append("")
# Shared Tags section
lines.append("## Shared Tags (All Repositories)")
lines.append("")
lines.append("The following tags should be applied consistently across all "
"3DCityDB repositories with the same colors.")
lines.append("")
grouped = group_labels(data["shared_labels"])
group_heading_map = {
"Type": "Type Tags",
"Status": "Status Tags",
"Priority": "Priority Tags",
"Contribution": "Contribution Tags",
"Release": "Release Tags",
"Infrastructure and others": "Infrastructure and others tags",
}
for group_name, labels in grouped:
heading = group_heading_map.get(group_name, f"{group_name} Tags")
lines.append(f"### {heading}")
lines.append("")
lines.append("| Label | Color | Description | Preview |")
lines.append("| :--- | :--- | :--- | :--- |")
for label in labels:
name = label["name"]
color = label["color"].lstrip("#")
desc = label.get("description", "")
badge = badge_md(name, color)
lines.append(f"| `{name}` | `#{color}` | {desc} | {badge} |")
lines.append("")
lines.append("---")
lines.append("")
# Domain-specific tags per repo
lines.append("## Domain specific tags")
lines.append("")
lines.append("These tags are unique for a repo based on the repo content.")
lines.append("")
repo_display = {
"3dcitydb/3dcitydb": "3dcitydb",
"3dcitydb/citydb-tool": "citydb-tool",
"3dcitydb/3dcitydb-mkdocs": "3dcitydb-mkdocs",
}
for repo in data["repositories"]:
repo_labels = data.get("repo_labels", {}).get(repo, [])
if not repo_labels:
continue
display_name = repo_display.get(repo, repo.split("/")[-1])
lines.append(f"### Domain specific tags: {display_name}")
lines.append("")
lines.append(f"**Repository:** [github.com/{repo}](https://github.com/{repo})")
lines.append("")
lines.append("| Label | Color | Description | Preview |")
lines.append("| :--- | :--- | :--- | :--- |")
for label in repo_labels:
name = label["name"]
color = label["color"].lstrip("#")
desc = label.get("description", "")
badge = badge_md(name, color)
lines.append(f"| `{name}` | `#{color}` | {desc} | {badge} |")
lines.append("")
lines.append("---")
lines.append("")
return "\n".join(lines)
def main():
parser = argparse.ArgumentParser(
description="Generate markdown documentation from labels JSON."
)
parser.add_argument(
"--file",
default=DEFAULT_FILE,
help=f"Path to labels JSON file (default: {DEFAULT_FILE})",
)
parser.add_argument(
"--output", "-o",
help="Output file path (default: stdout)",
)
args = parser.parse_args()
data = load_labels(args.file)
markdown = generate_markdown(data)
if args.output:
with open(args.output, "w", encoding="utf-8") as f:
f.write(markdown)
print(f"Documentation written to {args.output}", file=sys.stderr)
else:
print(markdown)
if __name__ == "__main__":
main()