summaryrefslogtreecommitdiff
path: root/lib/python/qmk/comment_remover.py
diff options
context:
space:
mode:
authorZach White <skullydazed@users.noreply.github.com>2020-05-26 13:05:41 -0700
committerDrashna Jael're <drashna@live.com>2020-08-08 20:31:13 -0700
commitec8c10ac1bd939bd210d1047f4d9d7c4dfca456d (patch)
tree33e494ba22ada101ed0b630453fbfb50792d4333 /lib/python/qmk/comment_remover.py
parent5ea1cd3526ff49ef61cbe728409782994b83bf1b (diff)
[CLI] Add a subcommand for getting information about a keyboard (#8666)
You can now use `qmk info` to get information about keyboards and keymaps. Co-authored-by: Erovia <Erovia@users.noreply.github.com>
Diffstat (limited to 'lib/python/qmk/comment_remover.py')
-rw-r--r--lib/python/qmk/comment_remover.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/python/qmk/comment_remover.py b/lib/python/qmk/comment_remover.py
new file mode 100644
index 0000000000..45a25257f8
--- /dev/null
+++ b/lib/python/qmk/comment_remover.py
@@ -0,0 +1,20 @@
+"""Removes C/C++ style comments from text.
+
+Gratefully adapted from https://stackoverflow.com/a/241506
+"""
+import re
+
+comment_pattern = re.compile(r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"', re.DOTALL | re.MULTILINE)
+
+
+def _comment_stripper(match):
+ """Removes C/C++ style comments from a regex match.
+ """
+ s = match.group(0)
+ return ' ' if s.startswith('/') else s
+
+
+def comment_remover(text):
+ """Remove C/C++ style comments from text.
+ """
+ return re.sub(comment_pattern, _comment_stripper, text)