From d30d8f7ad5f32c2af9224a3d0bbc1ee40ea1ca02 Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Wed, 15 Jun 2022 16:58:40 +0200 Subject: [PATCH] WIP Table --- src/bootstraparse/modules/context_mngr.py | 71 ++++++++++++++++++++--- src/bootstraparse/modules/error_mngr.py | 6 +- tests/modules/test_context_mngr.py | 2 +- tests/modules/test_syntax.py | 56 +++++++++--------- 4 files changed, 96 insertions(+), 39 deletions(-) diff --git a/src/bootstraparse/modules/context_mngr.py b/src/bootstraparse/modules/context_mngr.py index 9f1080d..39951d8 100644 --- a/src/bootstraparse/modules/context_mngr.py +++ b/src/bootstraparse/modules/context_mngr.py @@ -401,6 +401,11 @@ class TableCellContainer(BaseContainer): pass +class TableCellSizeContainer(BaseContainer): + type = "table" + subtype = "t_cell" + + class LinebreakContainer(BaseContainer): def export(self, _): if len(self.content) == 1: @@ -428,7 +433,7 @@ _to_container = { "table:row": TableRowContainer, "table:cell": TableCellContainer, # Should be an error I beleive ? "table:separator": TableSeparatorContainer, # Should be an error I beleive ? - "table:cell_size": TableCellContainer, + "table:cell_size": TableCellSizeContainer, } @@ -452,19 +457,24 @@ class ContextManager: self.name = name self.ident = ident self.matched_elements = {} - self.dict_lookahead = { + self._dict_lookahead = { "list:ulist": ["list:ulist"], "list:olist": ["list:olist"], "table:row": ["table:separator", "table:row"], # future: Implement tables + "table:cell": ["table:separator", "table:cell"], "blockquotes": [], # future: Implement blockquotes "linebreak": ["linebreak"], } - self.dict_lookahead_reparse = [ + self._dict_custom_lookahead = { + "table:row": self.table_lookahead, # future: Implement tables + } + self._dict_lookahead_reparse = [ "list:ulist", "list:olist", "table:row", "table:cell", ] + self.contextualised = False def encapsulate(self, start, end): @@ -575,11 +585,16 @@ class ContextManager: self.pile[index] = None # Group together multiple one-lines - elif token.label in self.dict_lookahead: + elif token.label in self._dict_lookahead: lookahead_return = self.lookahead(token, index) index += lookahead_return[0] line_number += lookahead_return[1] + elif token.label in self._dict_custom_lookahead: + lookahead_return = self._dict_custom_lookahead[token.label](token, index) + index += lookahead_return[0] + line_number += lookahead_return[1] + # future: advanced lookahead for * logic # elif token.label in self.dict_advanced_lookahead: @@ -658,13 +673,13 @@ class ContextManager: """ range_to_encapsulate = 0 line_skipped = 0 - if self.parsed_list[index].label in self.dict_lookahead_reparse: + if self.parsed_list[index].label in self._dict_lookahead_reparse: self.recontext(self.parsed_list[index]) i = index + 1 while i < len(self.parsed_list): - if self.parsed_list[i].label in self.dict_lookahead[token.label]: - if self.parsed_list[i].label in self.dict_lookahead_reparse: + if self.parsed_list[i].label in self._dict_lookahead[token.label]: + if self.parsed_list[i].label in self._dict_lookahead_reparse: self.recontext(self.parsed_list[i]) self.pile.append(self.parsed_list[i]) range_to_encapsulate += 1 @@ -681,6 +696,37 @@ class ContextManager: self.encapsulate(index, index + range_to_encapsulate) return range_to_encapsulate, line_skipped + def table_lookahead(self, token, index): + """ + Method to lookahead for a table. + Parameters + ---------- + token : syntax.Table + Token to lookahead for. + index : int + Index of the token in the pile. + Returns + ------- + int : Index of the next token to lookahead for. + """ + range_to_encapsulate = 0 + i = index + 1 + while i < len(self.parsed_list): + if self.parsed_list[i].label == "linebreak": + self.pile.append(self.parsed_list[i]) + range_to_encapsulate += 1 + if self.parsed_list[i-1].label == "linebreak": + break + elif self.parsed_list[i].label in ["table:row", "table:separator"]: + self.pile.append(self.table_recontext(self.parsed_list[i])) + range_to_encapsulate += 1 + elif self.parsed_list[i].label == "table:cell_size": + if not isinstance(self.pile[i-1], TableRowContainer): + raise MismatchedContainerError(token, "A table cell size must be preceded by a table row.") + self.pile[i-1].cell_size = self.parsed_list[i] + self.pile.append(None) # Unsure if this is the best way to do it. + range_to_encapsulate += 1 + def recontext(self, token): """ Function to recontextualise the content of a token. @@ -691,6 +737,17 @@ class ContextManager: """ token.content = ContextManager(token.content, name=self.name)() + def table_recontext(self, token): + """ + Function to recontextualise the content of a table. + """ + for elt in token.content: + if isinstance(elt, syntax.TableCellToken): + elt.content = ContextManager(elt.content, name=self.name)() + elif isinstance(elt, syntax.TableCellSizeToken): + pass # Continue Here + elt.content = ContextManager(elt.content, name=self.name)() + def get_last_container_in_pile(self, index): """ Returns the last element in the pile if it is a Container, raises an error otherwise. diff --git a/src/bootstraparse/modules/error_mngr.py b/src/bootstraparse/modules/error_mngr.py index c460ee4..d12c8eb 100644 --- a/src/bootstraparse/modules/error_mngr.py +++ b/src/bootstraparse/modules/error_mngr.py @@ -17,7 +17,7 @@ from bootstraparse.modules.tools import __GLk # __GFi, __GFu, __GL _ERRORS = ["ParsingError", "MismatchedContainerError"] -def init_logging(filename=None, loglevel="ERROR", filemode='w', handler=None): +def init_logging(filename=None, loglevel="DEBUG", filemode='w', handler=None): """ Initializes logging :param filename: The path for the log file @@ -135,7 +135,7 @@ class MismatchedContainerError(BootstraparseError): """ The token is not final and cannot be contained. Indicative of a mismatched token. """ - def __init__(self, token): + def __init__(self, token, message=""): """ Initializes the MismatchedContainerError class with the token that was not final :param token: The token that is not final @@ -150,7 +150,7 @@ class MismatchedContainerError(BootstraparseError): self.line = None self.label = None self.name = None - super().__init__(f"Could not process {self.label} at line {self.line} in file {self.name}.") + super().__init__(f"Could not process {self.label} at line {self.line} in file {self.name}. {message}") class LonelyOptionalError(BootstraparseError): diff --git a/tests/modules/test_context_mngr.py b/tests/modules/test_context_mngr.py index f4b24d6..2d095df 100644 --- a/tests/modules/test_context_mngr.py +++ b/tests/modules/test_context_mngr.py @@ -329,7 +329,7 @@ _token_list_with_expected_result = [ sy.TableRowToken([ context_mngr.TableCellContainer([ sy.TableCellToken([ - "2", + sy.TableCellSizeToken(["2"]), context_mngr.TextContainer([sy.TextToken(["a"])]), ]), ]), diff --git a/tests/modules/test_syntax.py b/tests/modules/test_syntax.py index a40827b..c563df9 100644 --- a/tests/modules/test_syntax.py +++ b/tests/modules/test_syntax.py @@ -622,46 +622,46 @@ list_add_tag_input_and_expected_output = [ # test_readable_markup list_of_text_input_and_readable_output = [ # Header - ("# Text1 #", "one_header", "
"), - ("## Text2 ##", "one_header", "
"), + ("# Text1 #", "one_header", "
", __GL()), + ("## Text2 ##", "one_header", "
", __GL()), # Text # il_link | et_strong | et_em | et_strikethrough | et_underline | et_custom_span - ("*Italic*", "et_em", ""), - ("__Underline__", "et_underline", ""), - ("**Bold**", "et_strong", ""), - ("~~Strikethrough~~", "et_strikethrough", ""), + ("*Italic*", "et_em", "", __GL()), + ("__Underline__", "et_underline", "", __GL()), + ("**Bold**", "et_strong", "", __GL()), + ("~~Strikethrough~~", "et_strikethrough", "", __GL()), ("Text *bold __underline__ still bold*", "enhanced_text", - "Text bold underline still bold "), + "Text bold underline still bold ", __GL()), # noqa E501 (line too long) - ("[link]('http://www.google.com')", "il_link", ""), + ("[link]('http://www.google.com')", "il_link", "", __GL()), ("Reverse order:(#123) Span, __underline__ ~~strikethrough~~ **emphasis * Strong**", "enhanced_text", - "Reverse order: Span, underline strikethrough emphasis Strong "), # noqa E501 (line too long) + "Reverse order: Span, underline strikethrough emphasis Strong ", __GL()), # noqa E501 (line too long) # Structural elements - ("<"), - ("div>>", "se", ""), - ("<"), - ("article>>", "se", ""), - ("<"), - ("aside>>", "se", ""), - ("<"), - ("section>>", "se", ""), + ("<", __GL()), + ("div>>", "se", "", __GL()), + ("<", __GL()), + ("article>>", "se", "", __GL()), + ("<", __GL()), + ("aside>>", "se", "", __GL()), + ("<", __GL()), + ("section>>", "se", "", __GL()), # Lists - ("- Text", "one_ulist", ""), - ("#. Text", "one_olist", ""), + ("- Text", "one_ulist", "", __GL()), + ("#. Text", "one_olist", "", __GL()), # Tables - ("| Text1 | Text2 |", "table", ",' />"), - ("| Text1 |3 Text2 |", "table", ",,Text2 ' />' />"), - ("|---|---|", "table_separator", ""), - ("|:--|-:-|--:|--:|", "table_separator", ""), + ("| Text1 | Text2 |", "table", ",' />", __GL()), + ("| Text1 |3 Text2 |", "table", ",,Text2 ' />' />", __GL()), + ("|---|---|", "table_separator", "", __GL()), + ("|:--|-:-|--:|--:|", "table_separator", "", __GL()), # Other Mostly for testing and coverage - ("'", "quotes", "'"), - (" -", "line", "-"), + ("'", "quotes", "'", __GL()), + (" -", "line", "-", __GL()), ] @@ -852,8 +852,8 @@ def test__add_tag(token, expected_output): # Test text functions -@pytest.mark.parametrize("token_list, parsing_expression, expected", list_of_text_input_and_readable_output) -def test_readable_markup(token_list, parsing_expression, expected): +@pytest.mark.parametrize("token_list, parsing_expression, expected, line", list_of_text_input_and_readable_output) +def test_readable_markup(token_list, parsing_expression, expected, line): """ Test that the readable markup is correctly generated. :param token_list: The list of tokens.