WIP Table
This commit is contained in:
parent
6654dcbb84
commit
d30d8f7ad5
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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"])]),
|
||||
]),
|
||||
]),
|
||||
|
|
|
|||
|
|
@ -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", "<header = '#,Text1 ' />"),
|
||||
("## Text2 ##", "one_header", "<header = '##,Text2 ' />"),
|
||||
("# Text1 #", "one_header", "<header = '#,Text1 ' />", __GL()),
|
||||
("## Text2 ##", "one_header", "<header = '##,Text2 ' />", __GL()),
|
||||
|
||||
# Text
|
||||
# il_link | et_strong | et_em | et_strikethrough | et_underline | et_custom_span
|
||||
("*Italic*", "et_em", "<text:em />"),
|
||||
("__Underline__", "et_underline", "<text:underline />"),
|
||||
("**Bold**", "et_strong", "<text:strong />"),
|
||||
("~~Strikethrough~~", "et_strikethrough", "<text:strikethrough />"),
|
||||
("*Italic*", "et_em", "<text:em />", __GL()),
|
||||
("__Underline__", "et_underline", "<text:underline />", __GL()),
|
||||
("**Bold**", "et_strong", "<text:strong />", __GL()),
|
||||
("~~Strikethrough~~", "et_strikethrough", "<text:strikethrough />", __GL()),
|
||||
("Text *bold __underline__ still bold*", "enhanced_text",
|
||||
"Text <text:em /> bold <text:underline /> underline <text:underline /> still bold <text:em />"),
|
||||
"Text <text:em /> bold <text:underline /> underline <text:underline /> still bold <text:em />", __GL()),
|
||||
# noqa E501 (line too long)
|
||||
("[link]('http://www.google.com')", "il_link", "<hyperlink = '[link]('http://www.google.com')' />"),
|
||||
("[link]('http://www.google.com')", "il_link", "<hyperlink = '[link]('http://www.google.com')' />", __GL()),
|
||||
("Reverse order:(#123) Span, __underline__ ~~strikethrough~~ **emphasis * Strong**", "enhanced_text",
|
||||
"Reverse order: <text:custom_span:123 /> Span, <text:underline /> underline <text:underline /> <text:strikethrough /> strikethrough <text:strikethrough /> <text:strong /> emphasis <text:em /> Strong <text:strong />"), # noqa E501 (line too long)
|
||||
"Reverse order: <text:custom_span:123 /> Span, <text:underline /> underline <text:underline /> <text:strikethrough /> strikethrough <text:strikethrough /> <text:strong /> emphasis <text:em /> Strong <text:strong />", __GL()), # noqa E501 (line too long)
|
||||
|
||||
# Structural elements
|
||||
("<<div", "se", "<se:start:div = 'div' />"),
|
||||
("div>>", "se", "<se:end:div = 'div' />"),
|
||||
("<<article", "se", "<se:start:article = 'article' />"),
|
||||
("article>>", "se", "<se:end:article = 'article' />"),
|
||||
("<<aside", "se", "<se:start:aside = 'aside' />"),
|
||||
("aside>>", "se", "<se:end:aside = 'aside' />"),
|
||||
("<<section", "se", "<se:start:section = 'section' />"),
|
||||
("section>>", "se", "<se:end:section = 'section' />"),
|
||||
("<<div", "se", "<se:start:div = 'div' />", __GL()),
|
||||
("div>>", "se", "<se:end:div = 'div' />", __GL()),
|
||||
("<<article", "se", "<se:start:article = 'article' />", __GL()),
|
||||
("article>>", "se", "<se:end:article = 'article' />", __GL()),
|
||||
("<<aside", "se", "<se:start:aside = 'aside' />", __GL()),
|
||||
("aside>>", "se", "<se:end:aside = 'aside' />", __GL()),
|
||||
("<<section", "se", "<se:start:section = 'section' />", __GL()),
|
||||
("section>>", "se", "<se:end:section = 'section' />", __GL()),
|
||||
|
||||
# Lists
|
||||
("- Text", "one_ulist", "<list:ulist = 'Text' />"),
|
||||
("#. Text", "one_olist", "<list:olist = 'Text' />"),
|
||||
("- Text", "one_ulist", "<list:ulist = 'Text' />", __GL()),
|
||||
("#. Text", "one_olist", "<list:olist = 'Text' />", __GL()),
|
||||
|
||||
# Tables
|
||||
("| Text1 | Text2 |", "table", "<table:row = '<table:cell = 'Text1 ' />,<table:cell = 'Text2 ' />' />"),
|
||||
("| Text1 |3 Text2 |", "table", "<table:row = '<table:cell = 'Text1 ' />,<table:cell = '<:cell_size = '3' "
|
||||
"/>,Text2 ' />' />"),
|
||||
("|---|---|", "table_separator", "<table:separator = '---,---' />"),
|
||||
("|:--|-:-|--:|--:|", "table_separator", "<table:separator = ':--,-:-,--:,--:' />"),
|
||||
("| Text1 | Text2 |", "table", "<table:row = '<table:cell = 'Text1 ' />,<table:cell = 'Text2 ' />' />", __GL()),
|
||||
("| Text1 |3 Text2 |", "table", "<table:row = '<table:cell = 'Text1 ' />,<table:cell = '<table:cell_size = '3' "
|
||||
"/>,Text2 ' />' />", __GL()),
|
||||
("|---|---|", "table_separator", "<table:separator = '---,---' />", __GL()),
|
||||
("|:--|-:-|--:|--:|", "table_separator", "<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.
|
||||
|
|
|
|||
Loading…
Reference in New Issue