StrictDoc Documentation
strictdoc/backend/sdoc_source_code/helpers/comment_preprocessor.py
Source file coverage
Path:
strictdoc/backend/sdoc_source_code/helpers/comment_preprocessor.py
Lines:
30
Non-empty lines:
23
Non-empty lines covered with requirements:
23 / 23 (100.0%)
Functions:
2
Functions covered by requirements:
2 / 2 (100.0%)
1
"""
2
@relation(SDOC-SRS-142, scope=file)
3
"""
4
 
5
import re
6
from typing import Match
7
 
8
WS = "[ \t]"
9
 
10
 
11
def preprocess_source_code_comment(comment: str) -> str:
12
    """
13
    Replace all Doxygen/Python/etc comment markers with spaces for easier processing.
14
 
15
    Note that the replacement does not change the size of the input string. This is
16
    important for the use case when StrictDoc writes the mutated code comments
17
    back to source files.
18
    """
19
 
20
    def replace_with_spaces(match: Match[str]) -> str:
21
        # Return a string of spaces with the same length as the matched text.
22
        return " " * len(match.group(0))
23
 
24
    replacement = re.sub(
25
        rf"(^/\*\*)|^{WS}*\*/?|(^///)|(^//)|(^#+)",
26
        replace_with_spaces,
27
        comment,
28
        flags=re.MULTILINE,
29
    )
30
    return replacement