diff --git a/toMkdocs/gridTableTools.py b/toMkdocs/gridTableTools.py index 95fde45e8869b56f17b39cd8d853b8ff58251d69..c690c2c9de53aa419a55a20ae8b3ae311d516420 100644 --- a/toMkdocs/gridTableTools.py +++ b/toMkdocs/gridTableTools.py @@ -27,7 +27,7 @@ class GridCell: self.auxiliarIndex:int = 0 - def calculateAndSetAlignment(self) -> None: + def calculateAndSetAlignment(self, headerDelimiterPositions:list[int], delimiterPositions:list[int], defaultAlignments:list[str], hasHeader:bool) -> None: """ Set the alignment of the cell based on the position of the delimiter. """ if self.position is None: @@ -90,50 +90,7 @@ class GridRow(): def __repr__(self): return self.__str__() - - def check_delimiter_alignment(line: str, delimiters: str = "|+") -> bool: - """ - Check if delimiters in a row align with expected positions. - - Args: - line: The line of text to check - delimiter_positions: List of expected positions (based on + characters) - delimiters: String containing valid delimiter characters (default: "|+") - - Returns: - bool: True if delimiters align correctly, False otherwise - """ - if not line or not delimiterPositions: - return False - - print(f"\nChecking line: '{line}'") - print(f"Expected delimiter positions: {delimiterPositions}") - - # For full separator lines (only +) - if '+' in line and '|' not in line: - currentPositions = [i for i, char in enumerate(line) if (char == '+' and i != 0)] - print(f"Full separator line - Found + at positions: {currentPositions}") - return all(delimiterPositions[-1] in currentPositions and - line.startswith("+") and - pos in delimiterPositions for pos in currentPositions) - - # For data lines (only |) - if '|' in line and '+' not in line: - currentPositions = [i for i, char in enumerate(line) if (char == '|' and i != 0)] - print(f"Data line - Found | at positions: {current_positions}") - return all(delimiterPositions[-1] in currentPositions and - line.startswith("|") and - pos in delimiterPositions for pos in currentPositions) - - # For partial separators (mix of + and |) - currentPositions = [i for i, char in enumerate(line) if (char in delimiters and i != 0)] - print(f"Partial separator - Found delimiters at positions: {currentPositions}") - print(f"Characters at those positions: {[line[pos] for pos in currentPositions]}") - return all(delimiterPositions[-1] in currentPositions and - (line.startswith("+") or line.startswith("|")) and - pos in delimiterPositions for pos in currentPositions) - - + class GridRowsTracker(): """ Represents the document object. """ def __init__(self, size:int) -> None: @@ -167,7 +124,7 @@ def parseGridTableWithSpans(gridTable:str) -> tuple[GridTableRowList, GridTableR :param pandoc_table: String of the Pandoc-style grid table. :return: List of lists representing the table with metadata for spans. """ - global hasHeader, defaultAlignments, headerDelimiterPositions, delimiterPositions, nextListElementMark + #global hasHeader, defaultAlignments, headerDelimiterPositions, delimiterPositions, nextListElementMark # Initialize globals hasHeader = False @@ -248,6 +205,48 @@ def parseGridTableWithSpans(gridTable:str) -> tuple[GridTableRowList, GridTableR row[columnIndex].colspanAdjusted = True # Mark cell as adjusted + def check_delimiter_alignment(line: str, delimiterPositions:list[int], delimiters: str = "|+") -> bool: + """ + Check if delimiters in a row align with expected positions. + + Args: + line: The line of text to check + delimiter_positions: List of expected positions (based on + characters) + delimiters: String containing valid delimiter characters (default: "|+") + + Returns: + bool: True if delimiters align correctly, False otherwise + """ + if not line or not delimiterPositions: + return False + + print(f"\nChecking line: '{line}'") + print(f"Expected delimiter positions: {delimiterPositions}") + + # For full separator lines (only +) + if '+' in line and '|' not in line: + currentPositions = [i for i, char in enumerate(line) if (char == '+' and i != 0)] + print(f"Full separator line - Found + at positions: {currentPositions}") + return all(delimiterPositions[-1] in currentPositions and + line.startswith("+") and + pos in delimiterPositions for pos in currentPositions) + + # For data lines (only |) + if '|' in line and '+' not in line: + currentPositions = [i for i, char in enumerate(line) if (char == '|' and i != 0)] + print(f"Data line - Found | at positions: {currentPositions}") + return all(delimiterPositions[-1] in currentPositions and + line.startswith("|") and + pos in delimiterPositions for pos in currentPositions) + + # For partial separators (mix of + and |) + currentPositions = [i for i, char in enumerate(line) if (char in delimiters and i != 0)] + print(f"Partial separator - Found delimiters at positions: {currentPositions}") + print(f"Characters at those positions: {[line[pos] for pos in currentPositions]}") + return all(delimiterPositions[-1] in currentPositions and + (line.startswith("+") or line.startswith("|")) and + pos in delimiterPositions for pos in currentPositions) + separatorIndices = [i for i, line in enumerate(lines) if isSeparator(line)] @@ -334,7 +333,7 @@ def parseGridTableWithSpans(gridTable:str) -> tuple[GridTableRowList, GridTableR cell.position = delimiterIndex # Position of cell delimiter + # Set alignment as defined by header separator line - cell.calculateAndSetAlignment() + cell.calculateAndSetAlignment(headerDelimiterPositions, delimiterPositions, defaultAlignments, hasHeader) while delimiterIndex > delimiterPositions[columnIndex]: columnIndex += 1 @@ -358,7 +357,7 @@ def parseGridTableWithSpans(gridTable:str) -> tuple[GridTableRowList, GridTableR auxDelimiterIndex += len(content) + 1 cell = rows[-1][auxiliarCellIndex] cell.position = auxDelimiterIndex # Position of cell delimiter + - cell.calculateAndSetAlignment() + cell.calculateAndSetAlignment(headerDelimiterPositions, delimiterPositions, defaultAlignments, hasHeader) while auxDelimiterIndex > delimiterPositions[auxiliarCellIndex]: auxiliarCellIndex += 1 auxiliarCellIndex += 1