Commit dcf0bb13 authored by Marco Cavalli's avatar Marco Cavalli
Browse files

feat: flash an error if it finds a malformed note/example

parent 90868a22
Loading
Loading
Loading
Loading
+46 −2
Original line number Diff line number Diff line
@@ -112,7 +112,52 @@ def run_format_checks(filename: str, file_lines: list[str]):

            i += 1

    def check_notes_and_examples():
        """
        Display an error and exit when the [!note] or [!tip] tags are followed by some text that is not "NOTE" or "EXAMPLE" respectively.
        """
        note_regex = r"^>>> \[!note\]"
        note_in_table_regex = r"^\| >>> \[!note\]"
        example_regex = r"^>>> \[!tip\]"
        note_allowed_text = r"^>>> \[!note\]( NOTE(\s\d+)?:)?$"
        note_in_table_allowed_text = r"^\| >>> \[!note\](( NOTE(\s\d+)?:)?\s*\|)?$"
        example_allowed_text = r"^>>> \[!tip\]( EXAMPLE(\s\d+)?:)?$"

        for i, line in enumerate(file_lines):
            line_num = i + 1
            if re.match(note_regex, line):
                if re.match(note_allowed_text, line):
                    continue
                print(f"line: {line}")
                print(
                    p_error(
                        f"{p_label(filename)}:{p_label(line_num)}: NOTE with unexpected text found. Please ensure the NOTE tag is either empty or is followed by 'NOTE', an optional number and a colon."
                    )
                )
                raise Exception("NOTE_NUMBERING_ERROR")
            if re.match(note_in_table_regex, line):
                if re.match(note_in_table_allowed_text, line):
                    continue
                print(f"line: {line}")
                print(
                    p_error(
                        f"{p_label(filename)}:{p_label(line_num)}: NOTE with unexpected text found in table. Please ensure the NOTE tag is either empty or is followed by 'NOTE', an optional number and a colon. Also, since this is in a table, ensure the line ends with a pipe '|' character without any other text after it. Finally, NOTES in tables can only be in cells that span over all columns."
                    )
                )
                raise Exception("NOTE_NUMBERING_ERROR")
            if re.match(example_regex, line):
                if re.match(example_allowed_text, line):
                    continue
                print(f"line: {line}")
                print(
                    p_error(
                        f"{p_label(filename)}:{p_label(line_num)}: EXAMPLE with unexpected text found. Please ensure the EXAMPLE tag is either empty or is followed by 'EXAMPLE', an optional number and a colon."
                    )
                )
                raise Exception("EXAMPLE_NUMBERING_ERROR")

    check_divs()
    check_notes_and_examples()


def add_divs_to_images_tables(text : str) -> str:
@@ -424,7 +469,6 @@ def auto_number_content(
        lines[first_note_line_index] += f" NOTE{' 1' if note_counter > 1 else ''}:"

    if note_in_table_counter >= 1 and first_note_in_table_line_index != -1:
        print(f"note_in_table_counter: {note_in_table_counter}")
        note_string = f"| >>> [!note] NOTE{' 1' if note_in_table_counter > 1 else ''}:"
        note_string_length = len(note_string)
        text_to_be_replaced = "| >>> [!note]"
@@ -537,7 +581,7 @@ def preprocess(
                # print(
                #     f"Warning: Could not preprocess {input_path}. It may not be a valid UTF-8 text file or is missing."
                # )
                if e.args[0] == "DIV_DELINEATOR_ERROR":
                if e.args[0] == "DIV_DELINEATOR_ERROR" or e.args[0] == "NOTE_NUMBERING_ERROR" or e.args[0] == "EXAMPLE_NUMBERING_ERROR":
                    # delete all files that start with --preprocessed--
                    for f in os.listdir(src):
                        if f.startswith("--preprocessed--"):