From e7f6b9f4f76142ecd554106984a8520a0ada9d03 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Wed, 23 Mar 2022 14:18:23 -0500 Subject: [PATCH] tools/gen-cpydiff: Skip Black fmt comments. Since cpydiff is code used as documentation, there are cases where we may want to use Black's `fmt: on/off/skip` comments to avoid automatic formatting. However, we don't want these comments to be distracting in the generated documentation. This rewrites the code to omit these comments when generating the docs. Signed-off-by: David Lechner --- tools/gen-cpydiff.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tools/gen-cpydiff.py b/tools/gen-cpydiff.py index 1d458d3dc..d4c8a5736 100644 --- a/tools/gen-cpydiff.py +++ b/tools/gen-cpydiff.py @@ -85,6 +85,16 @@ def readfiles(): class_, desc, cause, workaround, code = [ x.rstrip() for x in list(filter(None, re.split(SPLIT, text))) ] + + # remove black `fmt: on/off/skip` comments + code = "".join( + # skip comments are inline, so we replace just the comment + re.sub(r"\s*# fmt: skip", "", x) + for x in code.splitlines(keepends=True) + # on/off comments are on their own line, so we omit the entire line + if not re.match(r"\s*# fmt: (on|off)\s*", x) + ) + output = Output(test, class_, desc, cause, workaround, code, "", "", "") files.append(output) except IndexError: