Commit 8d624ec0 authored by Marco Cavalli's avatar Marco Cavalli
Browse files

feat: add checks to add empty lines in notes/examples if neeeded

parent dcf0bb13
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -84,6 +84,9 @@ function CustomTagsToLinks(el, prefix)
        id_prefix = prefix .. "_"
      end
      if filename then
        if filename_numbers_mapping[filename] == nil then
          error(string.format("Filename '%s' not found. Can't create a link for element %s", filename, pandoc.utils.stringify(child)))
        end
        local clause_number = filename_numbers_mapping[filename]
        -- check that number is not a letter
        if type(clause_number) ~= "number" then
+47 −0
Original line number Diff line number Diff line
@@ -248,6 +248,52 @@ def add_ids_to_headings(file_contents: str):
    return file_contents


def add_empty_lines_in_notes_and_examples(file_contents: str):
    """Ensure there is an empty line after the NOTE and EXAMPLE tags to ensure proper rendering. This is required because Pandoc would otherwise merge the note/example tag line with the first line of the note/example."""
    file_lines = file_contents.split("\n")
    new_file_lines = []
    i = 0
    while i < len(file_lines):
        line = file_lines[i]
        
        # opening of a note or example
        if line.startswith(">>> [!note]") or line.startswith(">>> [!tip]") or line.startswith("| >>> [!note]"):
            new_file_lines.append(line)
            # Check if the next line exists and is not empty
            if i + 1 < len(file_lines) and file_lines[i + 1].strip() != "":
                if not line.startswith("| >>> [!note]"):
                    new_file_lines.append("")  # Add an empty line only for notes/examples outside tables
                else:
                    if not line.startswith("+") and not line.endswith("+"):
                        line_length = len(line) - 2  # Subtract 2 for the "|" at the start and end
                        new_file_lines.append("|" + " " * line_length + "|")  # Add an empty line
        
        # closing of a note or example
        elif line.find(">>>") != -1 or line.find("| >>>") != -1:
            #check before the line
            line_before = file_lines[i - 1] if i > 0 else ""
            empty_line_regex = r"^\s*$"
            empty_table_row_regex = r"^\|\s*\|$"
            if not re.match(empty_line_regex, line_before) and not line_before.startswith("| "):
                new_file_lines.append("")  # Add an empty line before any other blockquote
            elif not re.match(empty_table_row_regex, line) and line_before.startswith("| "):
                line_length = len(line) - 2  # Subtract 2 for the "|" at the start and end
                new_file_lines.append("|" + " " * line_length + "|")  # Add an empty line before any other blockquote in a table

            new_file_lines.append(line)

            #check after the line
            if not line.startswith("| >>>"): # we are not in a table
                new_file_lines.append("")  # Add an empty line after any other blockquote
            elif line.startswith("| >>>"):
                if not line.startswith("+-") and not line.startswith("+="):
                    line_length = len(line) - 2  # Subtract 2 for the "|" at the start and end
                    new_file_lines.append("|" + " " * line_length + "|")  # Add an empty line
        else:
            new_file_lines.append(line)
        i += 1
    return "\n".join(new_file_lines) + "\n"

# Used to keep track of clause numbers across multiple levels when auto-numbering
clauses_counters = [0] * MAX_HEADING_LEVEL
clauses_counters[0] = 3  # first 3 clauses are taken by mandatory files
@@ -567,6 +613,7 @@ def preprocess(
                text = add_ids_to_references(text, filename)
                text = handle_less_than_greater_than_text(text)
                text = add_ids_to_headings(text)
                text = add_empty_lines_in_notes_and_examples(text)
                if filename_without_extension in REFS:
                    text = text.replace("::: REFS", "::: EX")