|
1 | | -import argparse |
2 | | -import json |
3 | | -import os |
4 | | -import re |
5 | | -import sys |
6 | | -from textwrap import dedent, indent |
7 | | - |
8 | | -import pypandoc |
9 | | - |
10 | | -scriptpath = "../" |
11 | | -script_dir = os.path.dirname(os.path.abspath(__file__)) |
12 | | -parent_dir = os.path.abspath(os.path.join(script_dir, "..")) |
13 | | -sys.path.append(parent_dir) |
14 | | - |
15 | | -from generate_guideline_templates import ( |
16 | | - guideline_rst_template, |
17 | | - issue_header_map, |
18 | | -) |
19 | | - |
20 | | - |
21 | | -def md_to_rst(markdown: str) -> str: |
22 | | - return pypandoc.convert_text( |
23 | | - markdown, |
24 | | - 'rst', |
25 | | - format='markdown', |
26 | | - extra_args=['--wrap=none'] |
27 | | - ) |
28 | | - |
29 | | - |
30 | | -def normalize_list_separation(text: str) -> str: |
31 | | - """ |
32 | | - Ensures every new list block is preceded by a blank line, |
33 | | - required for robust parsing by Pandoc when targeting RST |
34 | | - """ |
35 | | - # Regex to identify any line that starts a Markdown list item (* or -) |
36 | | - _list_item_re = re.compile(r"^[ \t]*[*-][ \t]+") |
37 | | - |
38 | | - output_buffer = [] |
39 | | - for line in text.splitlines(): |
40 | | - is_item = bool(_list_item_re.match(line)) |
41 | | - |
42 | | - # Get the last line appended to the output buffer |
43 | | - prev = output_buffer[-1] if output_buffer else "" |
44 | | - |
45 | | - # Check if a blank line needs to be inserted before list |
46 | | - # (Current is item) AND (Prev is not blank) AND (Prev is not an item) |
47 | | - if is_item and prev.strip() and not _list_item_re.match(prev): |
48 | | - # Insert a blank line to clearly separate the new list block |
49 | | - output_buffer.append("") |
50 | | - |
51 | | - output_buffer.append(line) |
52 | | - |
53 | | - return "\n".join(output_buffer) |
54 | | - |
55 | | - |
56 | | -def normalize_md(issue_body: str) -> str: |
57 | | - """ |
58 | | - Fix links and mixed bold/code that confuse Markdown parser |
59 | | - """ |
60 | | - # Fix links with inline-code: [`link`](url) => [link](url) |
61 | | - issue_body = re.sub( |
62 | | - r"\[\s*`([^`]+)`\s*\]\(([^)]+)\)", |
63 | | - r"[\1](\2)", |
64 | | - issue_body |
65 | | - ) |
66 | | - |
67 | | - # Fix mixed bold/code formatting |
68 | | - # **`code`** => `code` |
69 | | - issue_body = re.sub( |
70 | | - r"\*\*`([^`]+)`\*\*", |
71 | | - r"`\1`", |
72 | | - issue_body |
73 | | - ) |
74 | | - |
75 | | - # `**code**` => `code` |
76 | | - issue_body = re.sub( |
77 | | - r"`\*\*([^`]+)\*\*`", |
78 | | - r"`\1`", |
79 | | - issue_body |
80 | | - ) |
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-License-Identifier: MIT OR Apache-2.0 |
| 3 | +# SPDX-FileCopyrightText: The Coding Guidelines Subcommittee Contributors |
81 | 4 |
|
82 | | - return issue_body |
| 5 | +""" |
| 6 | +This script transforms a GitHub issue JSON into RST format for coding guidelines. |
83 | 7 |
|
| 8 | +It reads a GitHub issue's JSON data from standard input, parses its body |
| 9 | +(which is expected to follow a specific issue template), and converts it |
| 10 | +into a formatted reStructuredText (.rst) guideline. |
84 | 11 |
|
85 | | -def extract_form_fields(issue_body: str) -> dict: |
86 | | - """ |
87 | | - This function parses issues json into a dict of important fields |
88 | | - """ |
| 12 | +Usage: |
| 13 | + cat issue.json | uv run python scripts/auto-pr-helper.py |
| 14 | + cat issue.json | uv run python scripts/auto-pr-helper.py --save |
89 | 15 |
|
90 | | - fields = dict.fromkeys(issue_header_map.values(), "") |
| 16 | +Location: scripts/auto-pr-helper.py (replaces existing file) |
| 17 | +""" |
91 | 18 |
|
92 | | - lines = issue_body.splitlines() |
93 | | - current_key = None |
94 | | - current_value_lines = [] |
95 | | - |
96 | | - lines.append("### END") # Sentinel to process last field |
97 | | - |
98 | | - # Look for '###' in every line, ### represent a sections/field in a guideline |
99 | | - for line in lines: |
100 | | - header_match = re.match(r"^### (.+)$", line.strip()) |
101 | | - if header_match: |
102 | | - # Save previous field value if any |
103 | | - if current_key is not None: |
104 | | - value = "\n".join(current_value_lines).strip() |
105 | | - # `_No response_` represents an empty field |
106 | | - if value == "_No response_": |
107 | | - value = "" |
108 | | - if current_key in fields: |
109 | | - fields[current_key] = value |
110 | | - |
111 | | - header = header_match.group(1).strip() |
112 | | - current_key = issue_header_map.get( |
113 | | - header |
114 | | - ) # Map to dict key or None if unknown |
115 | | - current_value_lines = [] |
116 | | - else: |
117 | | - current_value_lines.append(line) |
118 | | - |
119 | | - return fields |
120 | | - |
121 | | - |
122 | | -def save_guideline_file(content: str, chapter: str): |
123 | | - """ |
124 | | - Appends a guideline to a chapter |
125 | | - """ |
126 | | - filename = f"src/coding-guidelines/{chapter.lower().replace(' ', '-')}.rst" |
127 | | - with open(filename, "a", encoding="utf-8") as f: |
128 | | - f.write(content) |
129 | | - print(f"Saved guideline to {filename}") |
130 | | - |
131 | | - |
132 | | -def guideline_template(fields: dict) -> str: |
133 | | - """ |
134 | | - This function turns a dictionary that contains the guideline fields |
135 | | - into a proper .rst guideline format |
136 | | - """ |
137 | | - |
138 | | - def get(key): |
139 | | - return fields.get(key, "").strip() |
140 | | - |
141 | | - def format_code_block(code: str, lang: str = "rust") -> str: |
142 | | - lines = code.strip().splitlines() |
143 | | - if lines and lines[0].strip().startswith("```"): |
144 | | - # Strip the ```rust and ``` lines |
145 | | - lines = lines[1:] |
146 | | - if lines and lines[-1].strip() == "```": |
147 | | - lines = lines[:-1] |
148 | | - |
149 | | - # Dedent before adding indentation |
150 | | - dedented_code = dedent("\n".join(lines)) |
151 | | - |
152 | | - # Add required indentation |
153 | | - indented_code = "\n".join( |
154 | | - f" {line}" for line in dedented_code.splitlines() |
155 | | - ) |
156 | | - |
157 | | - return f"\n\n{indented_code}\n" |
158 | | - |
159 | | - amplification_text = indent(md_to_rst(get("amplification")), " " * 12) |
160 | | - rationale_text = indent(md_to_rst(get("rationale")), " " * 16) |
161 | | - non_compliant_ex_prose_text = indent( |
162 | | - md_to_rst(get("non_compliant_ex_prose")), " " * 16 |
163 | | - ) |
164 | | - compliant_example_prose_text = indent( |
165 | | - md_to_rst(get("compliant_example_prose")), " " * 16 |
166 | | - ) |
167 | | - |
168 | | - guideline_text = guideline_rst_template( |
169 | | - guideline_title=get("guideline_title"), |
170 | | - category=get("category"), |
171 | | - status=get("status"), |
172 | | - release_begin=get("release_begin"), |
173 | | - release_end=get("release_end"), |
174 | | - fls_id=get("fls_id"), |
175 | | - decidability=get("decidability"), |
176 | | - scope=get("scope"), |
177 | | - tags=get("tags"), |
178 | | - amplification=amplification_text, |
179 | | - rationale=rationale_text, |
180 | | - non_compliant_ex_prose=non_compliant_ex_prose_text, |
181 | | - non_compliant_ex=format_code_block(get("non_compliant_ex")), |
182 | | - compliant_example_prose=compliant_example_prose_text, |
183 | | - compliant_example=format_code_block(get("compliant_example")), |
184 | | - ) |
185 | | - |
186 | | - return guideline_text |
| 19 | +import argparse |
| 20 | +import json |
| 21 | +import sys |
187 | 22 |
|
| 23 | +from guideline_utils import ( |
| 24 | + extract_form_fields, |
| 25 | + guideline_template, |
| 26 | + normalize_list_separation, |
| 27 | + normalize_md, |
| 28 | + save_guideline_file, |
| 29 | +) |
188 | 30 |
|
189 | 31 | if __name__ == "__main__": |
190 | 32 | # parse arguments |
|
0 commit comments