|
1 | 1 | import argparse as ap
|
| 2 | +import os |
2 | 3 | from pathlib import Path
|
3 |
| -from re import DOTALL, sub, match |
| 4 | +from re import DOTALL, MULTILINE, match, sub |
4 | 5 | from typing import Union
|
5 |
| -import os |
6 | 6 |
|
7 | 7 |
|
8 | 8 | def parse_args():
|
@@ -32,30 +32,35 @@ def remove_comments(text: str) -> str:
|
32 | 32 | """
|
33 | 33 | lines = text.splitlines()
|
34 | 34 | # remove inline comments with regex
|
35 |
| - for i, line in enumerate(lines): |
36 |
| - lines[i] = sub(r"(?: +)?//.*", "", line) |
| 35 | + lines = [sub(r"(?: +)?//.*", "", line) for line in lines] |
37 | 36 | # Remove block comments
|
38 | 37 | text = "\n".join(lines)
|
39 | 38 | text = sub(r"/\*.*?\*/", "", text, flags=DOTALL)
|
| 39 | + # Remove empty curlied-comment |
| 40 | + text = sub(r"^\{\s*\}", "", text, flags=DOTALL | MULTILINE) |
40 | 41 | return text
|
41 | 42 |
|
42 | 43 |
|
43 | 44 | def remove_blank_lines(text: str) -> str:
|
44 | 45 | """
|
45 |
| - Remove multiple blank lines from output file to only one |
| 46 | + Remove multiple blank lines from the input text, ensuring only single blank lines remain. |
46 | 47 |
|
47 | 48 | Args:
|
48 | 49 | text (str): Text to sanitize
|
49 | 50 |
|
50 | 51 | Returns:
|
51 | 52 | str: Sanitized text
|
52 | 53 | """
|
53 |
| - # if the line only contains whitespaces, remove it as well |
54 |
| - lines = text.splitlines() |
55 |
| - for i, line in enumerate(lines): |
56 |
| - lines[i] = sub(r"^\s+$", "", line) |
| 54 | + # Remove lines that contain only whitespace |
| 55 | + lines = [line for line in text.splitlines() if line.strip() != ""] |
| 56 | + |
| 57 | + # Join lines and replace multiple newlines with a single newline |
57 | 58 | text = "\n".join(lines)
|
58 |
| - text = sub(r"[\n]+", "\n", text) |
| 59 | + text = sub(r"\n{2,}", "\n", text) |
| 60 | + |
| 61 | + # Remove a leading newline if present |
| 62 | + text = text.lstrip("\n") |
| 63 | + |
59 | 64 | return text
|
60 | 65 |
|
61 | 66 |
|
@@ -83,6 +88,7 @@ def resolve_imports(script_path: Union[str, Path]) -> str:
|
83 | 88 |
|
84 | 89 | return "".join(sli)
|
85 | 90 |
|
| 91 | + |
86 | 92 | def main():
|
87 | 93 | args = parse_args()
|
88 | 94 | script = resolve_imports(args.input)
|
|
0 commit comments