From 476df81ef0248c4f0e8c4011badf2e540b745e39 Mon Sep 17 00:00:00 2001 From: Alex Bucknall Date: Tue, 26 May 2026 16:34:54 +0100 Subject: [PATCH] fix: protect backtick spans before stripping markdown italic in docstring generator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The _([^_]+)_ regex intended to strip _italic_ markdown was also matching underscores inside backtick-quoted identifiers (e.g. SERIAL_RX_BUFFER_SIZE → SERIALRXBUFFER_SIZE). Backtick spans are now saved and restored around the emphasis-stripping pass. Co-Authored-By: Claude Sonnet 4.6 --- scripts/generate_apis.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/scripts/generate_apis.py b/scripts/generate_apis.py index 4fca246..ef2be4c 100644 --- a/scripts/generate_apis.py +++ b/scripts/generate_apis.py @@ -126,13 +126,23 @@ def clean_docstring_text(self, text: str) -> str: import re text = re.sub(r'\[([^\]]+)\]\([^\)]+\)', r'\1', text) - # Remove markdown emphasis formatting (* and _) - # Handle both single and double emphasis markers + # Remove markdown emphasis formatting (* and _), but protect backtick-quoted + # spans first so identifiers like SERIAL_RX_BUFFER_SIZE are not corrupted. + placeholders = {} + def save_backtick(m): + key = f"\x00BACKTICK{len(placeholders)}\x00" + placeholders[key] = m.group(0) + return key + text = re.sub(r'`[^`]*`', save_backtick, text) + text = re.sub(r'\*\*([^*]+)\*\*', r'\1', text) # **bold** -> bold text = re.sub(r'\*([^*]+)\*', r'\1', text) # *italic* -> italic text = re.sub(r'__([^_]+)__', r'\1', text) # __bold__ -> bold text = re.sub(r'_([^_]+)_', r'\1', text) # _italic_ -> italic + for key, val in placeholders.items(): + text = text.replace(key, val) + # Collapse multiple spaces into single spaces text = re.sub(r'\s+', ' ', text)