|
| 1 | +""" |
| 2 | +A directive for organization/areas.rst to render the area tables. |
| 3 | +""" |
| 4 | + |
| 5 | +from docutils import nodes |
| 6 | +from docutils.parsers.rst import Directive |
| 7 | +import re |
| 8 | + |
| 9 | +area_table_rows = ["Communication", "GitHub reviews", "GitHub labels", "Triage project"] |
| 10 | + |
| 11 | +channel_re = re.compile(r'#([\w-]+)') |
| 12 | +godot_team_re = re.compile(r'@godotengine/([\w-]+)') |
| 13 | +labels_re = re.compile(r'<gh-label>(.+?)</gh-label>') |
| 14 | +triage_re = re.compile(r'<gh-triage project=(\d+)>(.*?)</gh-triage>') |
| 15 | + |
| 16 | + |
| 17 | +def transform_channels(match: re.Match): |
| 18 | + old_value = match.group(1) |
| 19 | + transformed = f'<a href="https://chat.godotengine.org/channel/{old_value}">#{old_value}</a>' |
| 20 | + return transformed |
| 21 | + |
| 22 | + |
| 23 | +def transform_github_teams(match: re.Match): |
| 24 | + old_value = match.group(1) |
| 25 | + transformed = f'<a href="https://github.com/godotengine/godot/pulls?q=is%3Apr+is%3Aopen+team-review-requested%3Agodotengine%2F{old_value}">@godotengine/{old_value}</a>' |
| 26 | + return transformed |
| 27 | + |
| 28 | + |
| 29 | +def transform_github_labels(match: re.Match): |
| 30 | + old_value = match.group(1) |
| 31 | + transformed = f'<code class="docutils literal notranslate"><span class="pre">{old_value}</span></code> (<a href="https://github.com/godotengine/godot/issues?q=is%3Aissue%20state%3Aopen%20label%3A{old_value}">issues</a>, <a href="https://github.com/godotengine/godot/pulls?q=is%3Apr+is%3Aopen+label%3A{old_value}">PRs</a>)' |
| 32 | + return transformed |
| 33 | + |
| 34 | + |
| 35 | +def transform_github_triage(match: re.Match): |
| 36 | + number = match.group(1) |
| 37 | + name = match.group(2) |
| 38 | + transformed = f'<a href="https://github.com/orgs/godotengine/projects/{number}">{name}</a>' |
| 39 | + return transformed |
| 40 | + |
| 41 | + |
| 42 | +class TableDirective(Directive): |
| 43 | + has_content = False |
| 44 | + required_arguments = 0 |
| 45 | + optional_arguments = 0 |
| 46 | + final_argument_whitespace = False |
| 47 | + option_spec = {column.lower().replace(" ", "_"): str for column in area_table_rows} |
| 48 | + |
| 49 | + def run(self): |
| 50 | + # Create a table node with the parsed header and data |
| 51 | + table = nodes.table() |
| 52 | + table.setdefault('classes', []).append("gdarea-table") |
| 53 | + tgroup = nodes.tgroup(cols=2) |
| 54 | + table += tgroup |
| 55 | + |
| 56 | + # Set column specifications |
| 57 | + tgroup += nodes.colspec(colwidth=2) |
| 58 | + tgroup += nodes.colspec(colwidth=5) |
| 59 | + |
| 60 | + # Create and add the table body |
| 61 | + tbody = nodes.tbody() |
| 62 | + tgroup += tbody |
| 63 | + for column_title in area_table_rows: |
| 64 | + row_text = self.options.get(column_title.lower().replace(" ", "_"), '') |
| 65 | + if not row_text: |
| 66 | + continue |
| 67 | + |
| 68 | + body_row = nodes.row() |
| 69 | + |
| 70 | + entry = nodes.entry() |
| 71 | + entry.setdefault('classes', []).append("gdarea-table-header") |
| 72 | + entry += nodes.paragraph(text=column_title) |
| 73 | + body_row += entry |
| 74 | + |
| 75 | + row_text = channel_re.sub(transform_channels, row_text) |
| 76 | + row_text = godot_team_re.sub(transform_github_teams, row_text) |
| 77 | + row_text = labels_re.sub(transform_github_labels, row_text) |
| 78 | + row_text = triage_re.sub(transform_github_triage, row_text) |
| 79 | + |
| 80 | + entry = nodes.entry() |
| 81 | + paragraph = nodes.paragraph() |
| 82 | + paragraph += nodes.raw('', row_text, format='html') |
| 83 | + entry += paragraph |
| 84 | + body_row += entry |
| 85 | + |
| 86 | + tbody += body_row |
| 87 | + |
| 88 | + return [table] |
| 89 | + |
| 90 | + |
| 91 | +def setup(app): |
| 92 | + app.add_directive('gdareatable', TableDirective) |
0 commit comments