diff --git a/src/bootstraparse/modules/export.py b/src/bootstraparse/modules/export.py index 12d0c01..ca14039 100644 --- a/src/bootstraparse/modules/export.py +++ b/src/bootstraparse/modules/export.py @@ -10,7 +10,7 @@ from bootstraparse.modules import config, pathresolver, error_mngr, context_mngr from collections import namedtuple -from bootstraparse.modules.syntax import split_optionals +from bootstraparse.modules.syntax import split_optionals, OptionalToken """ Named tuple containing all necessary information to select the appropriate @@ -29,7 +29,10 @@ def format_optionals(optionals): """ Function handling parser output optional object, splitting it between html_insert and class_insert on one hand and var on the other hand. - rtype: str + :param optionals: SplitToken namedtuple + :type optionals: OptionalToken + :return: The formatted optionals (With the pretty whitespace) + :rtype: str """ split = split_optionals(optionals) h = split.html_insert @@ -42,9 +45,9 @@ class ExportManager: """ Transforms ExportRequest tuples to ExportResponse tuples with the config-provided appropriate markup. """ - def __init__(self, cnoifg, templates): + def __init__(self, cnoifg, templates): # noqa # self.config = cnoifg TODO: actually import config - # self.templates = templates TODO: actually import templates + # self.templates = templates # noqa TODO: actually import templates self.config = config.ConfigLoader(pathresolver.b_path("configs/")) self.templates = config.ConfigLoader(pathresolver.b_path("templates/")) self.advanced_export = { @@ -127,6 +130,8 @@ class ExportManager: def header_transform(self, export_request): """ Specific template for headers. + :type export_request: ExportRequest + :rtype: ExportResponse """ start, end, optionals = self._get_template(export_request) try: @@ -145,6 +150,7 @@ class ExportManager: def display_transform(self, export_request): """ Specific template for display. + :type export_request: ExportRequest """ start, end, optionals = self._get_template(export_request) start = start.format(optionals=optionals, display_level=export_request.others["display_level"]) @@ -153,6 +159,7 @@ class ExportManager: def link_transform(self, export_request): """ Specific template for link. + :type export_request: ExportRequest """ start, end, _ = self._get_template(export_request) start = start.format(url=export_request.others["url"]) @@ -161,6 +168,7 @@ class ExportManager: def t_transform(self, export_request): """ Specific template for table content. + :type export_request: ExportRequest """ start, end, _ = self._get_template(export_request) start = start.format(col_span=export_request.others["col_span"]) @@ -169,6 +177,7 @@ class ExportManager: def image_transform(self, export_request): """ Specific template for images. + :type export_request: ExportRequest """ start, end, optionals = self._get_template(export_request) end = end.format(optionals=optionals) @@ -197,7 +206,7 @@ class ContextConverter: Main method processing the given pile. """ for container in self.pile: - print(container.export(self.exporter)) + print(container.export(self.exporter)) # TODO : Finish this def __str__(self): """ diff --git a/src/bootstraparse/modules/syntax.py b/src/bootstraparse/modules/syntax.py index 10fe83a..3a1922c 100644 --- a/src/bootstraparse/modules/syntax.py +++ b/src/bootstraparse/modules/syntax.py @@ -13,6 +13,8 @@ import os from itertools import zip_longest from collections import namedtuple +import rich + from bootstraparse.modules.error_mngr import MismatchedContainerError import pyparsing as pp @@ -330,6 +332,13 @@ SplitOptionals = namedtuple( def split_optionals(optionals): + """ + Splits the optional attributes into separate lists. + :param optionals: list of optional attributes + :type optionals: OptionalToken + :return: SplitOptionals object containing the split optionals + :rtype: SplitOptionals + """ var_list = [] var_dict = {} ci = '' @@ -342,11 +351,12 @@ def split_optionals(optionals): elif element.label == 'optional:insert': hi += " " + element.content[0] elif element.label == 'optional:var': - for e in element.content.var: + rich.inspect(optionals) + for e in element.content: if e.label == 'be:var': - var_list.append(e.content.value[0]) + var_list.append(e.content[0]) elif e.label == 'be:assign': - var_dict[e.content.assignation.var_name] = e.content.assignation.var_value[0] + var_dict[e.content[0][0]] = e.content[0][1] # Using the names makes it hard to debug return SplitOptionals(html_insert=hi[1:], class_insert=ci[1:], var_list=var_list, var_dict=var_dict) diff --git a/tests/modules/test_export.py b/tests/modules/test_export.py index 0bfdf65..40ef42c 100644 --- a/tests/modules/test_export.py +++ b/tests/modules/test_export.py @@ -40,6 +40,14 @@ zipped_templates = [ for item in sublist ] +_opts = syntax.OptionalToken([ + syntax.OptionalVarToken([ + syntax.BeAssignToken([["class", "blue"]]), + syntax.BeValueToken([123]), + ]), + syntax.OptionalInsertToken(["var='test', number=11"]), +]) + def test_export(): """ @@ -108,15 +116,13 @@ def test_get_template_error(): def test_with_optionnals(): em = export.ExportManager("test", "test") - em(export.ExportRequest("structural_elements", "div")) # noqa E741 + em(export.ExportRequest("structural_elements", "div", _opts)) # noqa E741 @pytest.mark.xfail(reason="Not implemented") def test_format_optionnals(): - import rich from bootstraparse.modules import syntax # noqa E402 - export.format_optionals("") - rich.inspect(export.format_optionals("")) + export.format_optionals(_opts) assert False @@ -141,9 +147,9 @@ def test_get_templates(export_type, export_subtype, line): "col_span": "1", "row_span": "1", "display_level": "1" - })) assert isinstance(r, export.ExportResponse) + # TODO : Add optional def test_context_cov(): diff --git a/tests/modules/test_syntax.py b/tests/modules/test_syntax.py index 5270066..cd5b87c 100644 --- a/tests/modules/test_syntax.py +++ b/tests/modules/test_syntax.py @@ -525,7 +525,7 @@ dict_advanced_syntax_input_and_expected_output = { ("|---|---|", (sy.TableSeparatorToken(["---", "---"]),), __GL()), ("- Text", (sy.EtUlistToken([sy.TextToken(["Text"])]),), __GL()), ("div>>", (sy.StructuralElementEndToken(["div"]),), __GL()), - ("#. Text with *em* and __underline__ and {arguments}{{super-arguments}}[a=1,'ui']", ( + ("#. Text with *em* and __underline__ and {arguments}{{super-arguments}}[a=1, c=2,'ui']", ( sy.EtOlistToken([ sy.TextToken(["Text with"]), sy.EtEmToken(["*"]), @@ -545,6 +545,7 @@ dict_advanced_syntax_input_and_expected_output = { ]), sy.OptionalVarToken([ sy.BeAssignToken([["a", 1]]), + sy.BeAssignToken([["c", 2]]), sy.BeValueToken(["ui"]), ]), ]),