Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions scripts/auto-pr-helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import re
import sys
from textwrap import indent
from textwrap import dedent, indent

from m2r import convert

Expand All @@ -21,6 +21,34 @@
def md_to_rst(markdown: str) -> str:
return convert(markdown)

def normalize_md(issue_body: str) -> str:
"""
Fix links and mixed bold/code that confuse Markdown parser
"""
# Fix links with inline-code: [`link`](url) => [link](url)
issue_body = re.sub(
r"\[\s*`([^`]+)`\s*\]\(([^)]+)\)",
r"[\1](\2)",
issue_body
)

# Fix mixed bold/code formatting
# **`code`** => `code`
issue_body = re.sub(
r"\*\*`([^`]+)`\*\*",
r"`\1`",
issue_body
)

# `**code**` => `code`
issue_body = re.sub(
r"`\*\*([^`]+)\*\*`",
r"`\1`",
issue_body
)

return issue_body


def extract_form_fields(issue_body: str) -> dict:
"""
Expand Down Expand Up @@ -85,9 +113,15 @@ def format_code_block(code: str, lang: str = "rust") -> str:
lines = lines[1:]
if lines and lines[-1].strip() == "```":
lines = lines[:-1]

# Dedent before adding indentation
dedented_code = dedent("\n".join(lines))

# Add required indentation
indented_code = "\n".join(
f" {line}" for line in lines
) # Adds the required indentation
f" {line}" for line in dedented_code.splitlines()
)

return f"\n\n{indented_code}\n"

amplification_text = indent(md_to_rst(get("amplification")), " " * 12)
Expand Down Expand Up @@ -139,7 +173,10 @@ def format_code_block(code: str, lang: str = "rust") -> str:

issue_number = json_issue["number"]
issue_title = json_issue["title"]

issue_body = json_issue["body"]
issue_body = normalize_md(issue_body)

fields = extract_form_fields(issue_body)
chapter = fields["chapter"]
content = guideline_template(fields)
Expand Down