From f6e88975224198265196f8ef233cb6c7bf963753 Mon Sep 17 00:00:00 2001 From: vicky Date: Thu, 17 Mar 2022 17:58:59 +0100 Subject: [PATCH] made things test-compliant, or tests thing-compliant --- src/bootstraparse/modules/syntax.py | 32 ++++++++++------------------- tests/modules/test_syntax.py | 16 +++++++-------- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/bootstraparse/modules/syntax.py b/src/bootstraparse/modules/syntax.py index cd9e342..1932c50 100644 --- a/src/bootstraparse/modules/syntax.py +++ b/src/bootstraparse/modules/syntax.py @@ -194,6 +194,10 @@ optional = ( pp.Opt(html_insert)("html_insert") + pp.Opt(var)("var") )("optional").add_parse_action(of_type(OptionalToken)) +# Inline elements +il_link = pp.Regex( + r"""\[(?P.+)\]\(['"](?P[a-zA-Z-_:\/=@#!%\?\d\(\)\.]+)['"]\)""" +).add_parse_action(of_type(HyperlinkToken)) # Enhanced text elements et_em = pp.Literal('*')('em').add_parse_action(of_type(EtEmToken)) @@ -203,22 +207,15 @@ et_strikethrough = pp.Literal('~~')('strikethrough').add_parse_action(of_type(Et et_custom_span = ( pps('(#') + pp.Word(pp.nums)('span_id') + pps(')') ).set_name('custom_span').add_parse_action(of_type(EtCustomSpanToken)) -markup = et_strong | et_em | et_strikethrough | et_underline | et_custom_span + +# markup sums up all in-line elements +markup = il_link | et_strong | et_em | et_strikethrough | et_underline | et_custom_span # Multiline elements se_start = (pps('<<') + structural_elements).add_parse_action(of_type(StructuralElementStartToken)) se_end = (structural_elements + pps('>>')).add_parse_action(of_type(StructuralElementEndToken)) se = se_end | se_start # Structural element -# Inline elements -# [link text]('http://test.icule') -il_link = pp.common.url -# il_link = ( -# pp.Literal('[') + pp.SkipTo('](')('text') + '](' + -# quotes + url_characters('url') + pp.match_previous_literal(quotes) -# + ')' -# ).add_parse_action(of_type(HyperlinkToken)) - # Oneline elements one_header = ( header_element + pp.SkipTo(pp.match_previous_literal(header_element)) @@ -234,10 +231,9 @@ one_ulist = pp.line_start + ( ).add_parse_action(of_type(EtUlistToken)) # Final elements -enhanced_text = pp.OneOrMore(markup + text) -# pp.SkipTo(markup)('text').add_parse_action(of_type(TextToken)) + markup | -# pp.SkipTo(pp.line_end)("text").add_parse_action(of_type(TextToken)) -# ) +enhanced_text = pp.ZeroOrMore( + markup | pp.SkipTo(markup)('text').add_parse_action(of_type(TextToken)) + markup +) + pp.Opt(pp.rest_of_line("text").add_parse_action(of_type(TextToken))) ############################################################################## @@ -253,7 +249,7 @@ line_to_replace = pp.OneOrMore( pp.SkipTo(image ^ alias)('text').add_parse_action(of_type(TextToken)) ^ image.add_parse_action(of_type(ImageToken)) ^ alias.add_parse_action(of_type(AliasToken)) -) ^ pp.SkipTo(pp.lineEnd)('text').add_parse_action(of_type(TextToken)) +) ^ pp.rest_of_line('text').add_parse_action(of_type(TextToken)) ############################################################################## # Temporary tests @@ -261,12 +257,6 @@ line_to_replace = pp.OneOrMore( if __name__ == '__main__': # pragma: no cover pp.autoname_elements() - list_strings = [ - "'http://test.icule'", - ] - - rich.inspect(il_link.parse_string(list_strings[0])) - if not os.path.exists('../../../dev_outputs/'): os.mkdir('../../../dev_outputs/') enhanced_text.create_diagram("../../../dev_outputs/diagram.html") \ No newline at end of file diff --git a/tests/modules/test_syntax.py b/tests/modules/test_syntax.py index 14d3dc0..c7af8e2 100644 --- a/tests/modules/test_syntax.py +++ b/tests/modules/test_syntax.py @@ -80,9 +80,9 @@ dict_advanced_syntax_input_and_expected_output = { ("(#12345)", (sy.EtCustomSpanToken(["12345"]), )), ], "il_link": [ - # Single link element, should only match the first "[link_name](link)" + # Single link element, should only match the first "[link_name]('link')" ("[text_link]('text://www.website.com/link.html')", - sy.HyperlinkToken("[text_link]('text://www.website.com/link.html')")) + [sy.HyperlinkToken(["[text_link]('text://www.website.com/link.html')"])]) ], # Enhanced Text @@ -107,11 +107,11 @@ dict_advanced_syntax_input_and_expected_output = { sy.EtCustomSpanToken(["12"]), sy.TextToken(["test"]), sy.EtStrongToken(["**"]), sy.TextToken(["test"]), sy.EtUnderlineToken(["__"]), sy.TextToken(["test"]), sy.EtStrikethroughToken(["~~"]), )), - ("Test text with a link [link_name](link)", (sy.TextToken(["Test text with a link "]), - sy.HyperlinkToken(["link_name", "link"]), )), - ("Test text with a link [link_name](link) and a *bold* text", (sy.TextToken(["Test text with a link "]), - sy.HyperlinkToken(["link_name", "link"]), - sy.TextToken([" and a "]), sy.EtEmToken(["*"]), + ("Test text with a link [link_name]('link')", (sy.TextToken(["Test text with a link"]), + sy.HyperlinkToken(["[link_name]('link')"]), )), + ("Test text with a link [link_name](\"link\") and a *bold* text", (sy.TextToken(["Test text with a link"]), + sy.HyperlinkToken(['[link_name]("link")']), + sy.TextToken(["and a"]), sy.EtEmToken(["*"]), sy.TextToken(["bold"]), sy.EtEmToken(["*"]), )), ], @@ -212,7 +212,7 @@ list_of_text_input_and_readable_output = [ ("**Bold**", "et_strong", ""), ("~~Strikethrough~~", "et_strikethrough", ""), # ("Text *bold __underline__ still bold*", "enhanced_text", "Text bold underline still bold"), # noqa E501 (line too long) - ("[link]('http://www.google.com')", "il_link", ""), + ("[link]('http://www.google.com')", "il_link", ""), # Structural elements ("<"),