fixed a lot of the testing and a little of the syntax

This commit is contained in:
vky 2022-03-19 01:15:30 +01:00
parent 69a962dac9
commit 8e694a87da
2 changed files with 65 additions and 62 deletions

View File

@ -216,7 +216,7 @@ display_element = pp.Word('!')
# Optional elements # Optional elements
optional = ( optional = pp.Opt(
pp.Opt(html_insert)("html_insert") + pp.Opt(var)("var") pp.Opt(html_insert)("html_insert") + pp.Opt(var)("var")
)("optional").add_parse_action(of_type(OptionalToken)) )("optional").add_parse_action(of_type(OptionalToken))
@ -235,20 +235,23 @@ et_custom_span = (
).set_name('custom_span').add_parse_action(of_type(EtCustomSpanToken)) ).set_name('custom_span').add_parse_action(of_type(EtCustomSpanToken))
# markup sums up all in-line elements # markup sums up all in-line elements
markup = il_link | et_strong | et_em | et_strikethrough | et_underline | et_custom_span markup = il_link | et_strong | et_em | et_strikethrough | et_underline | et_custom_span
enhanced_text = pp.ZeroOrMore(
markup | pp.SkipTo(markup)('text').add_parse_action(of_type(TextToken)) + markup
) + pp.Opt(pp.Regex(r'.+')("text").add_parse_action(of_type(TextToken)))
# Multiline elements # Multiline elements
se_start = (pps('<<') + structural_elements).add_parse_action(of_type(StructuralElementStartToken)) se_start = (pps('<<') + structural_elements).add_parse_action(of_type(StructuralElementStartToken))
se_end = (structural_elements + pps('>>')).add_parse_action(of_type(StructuralElementEndToken)) se_end = (structural_elements + pps('>>')).add_parse_action(of_type(StructuralElementEndToken)) # TODO: + pp.Opt(optional)
se = se_end | se_start # Structural element se = se_end | se_start # Structural element
table_row = ( table_row = pp.OneOrMore(
pp.Literal('|') + # pp.Regex(r'\|(\d)?')('table_colspan') +
pp.Opt(pp.nums)('table_colspan') + (pp.Combine(pps('|') + pp.Word(pp.nums)('table_colspan')) | pps('|')) +
pp.SkipTo('|')('table_cell').add_parse_action(of_type(TableCellToken)) + '|' pp.SkipTo('|')('table_cell').add_parse_action(of_type(TableCellToken)) # TODO: account for enhanced text
).add_parse_action(of_type(TableRowToken)) ).add_parse_action(of_type(TableRowToken)) + pps('|') # TODO: + pp.Opt(optional)
table_separator = pp.OneOrMore( table_separator = pp.OneOrMore(
pps('|') + pp.Word(':-') pps('|') + pp.Word(':-')
)('table_separator').add_parse_action(of_type(TableSeparatorToken)) + pps('|') )('table_separator').add_parse_action(of_type(TableSeparatorToken)) + pps('|')
table = table_row + pp.Opt(table_separator) + pp.OneOrMore(table_row) + optional table = table_separator | table_row
# multi_line sums up all multi-line elements # multi_line sums up all multi-line elements
multi_line = se | table multi_line = se | table
@ -261,19 +264,16 @@ one_display = (
display_element + pp.SkipTo(pp.match_previous_literal(display_element)) display_element + pp.SkipTo(pp.match_previous_literal(display_element))
).add_parse_action(of_type(DisplayToken)) ).add_parse_action(of_type(DisplayToken))
one_olist = pp.line_start + ( one_olist = pp.line_start + (
pp.Literal('#') + pps('.') + pp.SkipTo(pp.line_end)('olist_text') pps(pp.Literal('#')) + pps('.') + enhanced_text('olist_text')
).add_parse_action(of_type(EtOlistToken)) ).add_parse_action(of_type(EtOlistToken))
one_ulist = pp.line_start + ( one_ulist = pp.line_start + (
pp.Literal('-') + pp.SkipTo(pp.line_end)('ulist_text') pps(pp.Literal('-')) + enhanced_text('ulist_text')
).add_parse_action(of_type(EtUlistToken)) ).add_parse_action(of_type(EtUlistToken))
# one_line sums up all one-line elements # one_line sums up all one-line elements
one_line = one_header | one_display | one_olist | one_ulist one_line = one_header | one_display | one_olist | one_ulist # TODO: + pp.Opt(optional)
# Final elements # Final elements
enhanced_text = pp.ZeroOrMore( line = one_line | multi_line | enhanced_text
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)))
line = enhanced_text | one_line | multi_line
############################################################################## ##############################################################################

View File

@ -134,9 +134,11 @@ dict_advanced_syntax_input_and_expected_output = {
sy.HyperlinkToken(['[link_name]("link")']), sy.HyperlinkToken(['[link_name]("link")']),
sy.TextToken(["and a"]), sy.EtEmToken(["*"]), sy.TextToken(["and a"]), sy.EtEmToken(["*"]),
sy.TextToken(["bold"]), sy.TextToken(["bold"]),
sy.EtEmToken(["*"]),)), sy.EtEmToken(["*"]),
sy.TextToken(["text"]),
)),
("Reverse order (#123) Span __ Underline ~~ Strikethrough", ("Reverse order (#123) Span __ Underline ~~ Strikethrough",
(sy.EtCustomSpanToken(["123"]), sy.TextToken(["Reverse order"]), (sy.TextToken(["Reverse order"]),
sy.EtCustomSpanToken(["123"]), sy.TextToken(["Span"]), sy.EtCustomSpanToken(["123"]), sy.TextToken(["Span"]),
sy.EtUnderlineToken(["__"]), sy.TextToken(["Underline"]), sy.EtUnderlineToken(["__"]), sy.TextToken(["Underline"]),
sy.EtStrikethroughToken(["~~"]), sy.TextToken(["Strikethrough"]),)), # noqa E501 (line too long) sy.EtStrikethroughToken(["~~"]), sy.TextToken(["Strikethrough"]),)), # noqa E501 (line too long)
@ -150,29 +152,28 @@ dict_advanced_syntax_input_and_expected_output = {
"se_end": [ "se_end": [
("div>>", (sy.StructuralElementEndToken(["div"]),)), ("div>>", (sy.StructuralElementEndToken(["div"]),)),
], ],
"se": [ # "se": [
# Matches a div element, must be at the beginning of the line, the closing div can be with arguments # Matches a div element, must be at the beginning of the line, the closing div can be with arguments
("div>> [class='blue', 123#]{var='test', number=11}", [sy.StructuralElementEndToken(["div", [ # TODO : Re-implement this
sy.OptionalToken(["[", "class='blue'", ",", "123#", "]", "{", "var='test'", ",", "number=11", "}"])]])]), # ("div>> [class='blue', 123#]{var='test', number=11}", [sy.StructuralElementEndToken(["div", [
# noqa E501 (line too long) # sy.OptionalToken(["[", "class='blue'", ",", "123#", "]", "{", "var='test'", ",", "number=11", "}"])]])]),
("div>> [class='blue', 123#]", # ("div>> [class='blue', 123#]",
[sy.StructuralElementEndToken(["div", [sy.OptionalToken(["[", "class='blue'", ",", "123#", "]"])]])]), # [sy.StructuralElementEndToken(["div", [sy.OptionalToken(["[", "class='blue'", ",", "123#", "]"])]])]),
# noqa E501 (line too long) # ("div>> {var='test', number=11}",
("div>> {var='test', number=11}", # [sy.StructuralElementEndToken(["div", [sy.OptionalToken(["{", "var='test'", ",", "number=11", "}"])]])]),
[sy.StructuralElementEndToken(["div", [sy.OptionalToken(["{", "var='test'", ",", "number=11", "}"])]])]), # ],
# noqa E501 (line too long)
],
# List Elements # List Elements
# TODO: Test markup in lists
"one_olist": [ "one_olist": [
# Matches a one-level ordered list element, must be at the beginning of the line # Matches a one-level ordered list element, must be at the beginning of the line
# Drop the "." from the match # Drop the "." from the match
("#. Text", (sy.EtOlistToken(["#", "Text"]),)), ("#. Text", (sy.EtOlistToken([sy.TextToken(["Text"])]),)),
], ],
"one_ulist": [ "one_ulist": [
# Matches a one-level unordered list element, must be at the beginning of the line # Matches a one-level unordered list element, must be at the beginning of the line
# Drop the "-" from the match # Drop the "-" from the match
("- Text", [sy.EtUlistToken(["-", "Text"])]), ("- Text", (sy.EtUlistToken([sy.TextToken(["Text"])]),)),
], ],
# Headers # Headers
@ -184,44 +185,46 @@ dict_advanced_syntax_input_and_expected_output = {
("#### Text4 ####", (sy.HeaderToken(["####", "Text4 "]),)), ("#### Text4 ####", (sy.HeaderToken(["####", "Text4 "]),)),
("##### Text5 #####", (sy.HeaderToken(["#####", "Text5 "]),)), ("##### Text5 #####", (sy.HeaderToken(["#####", "Text5 "]),)),
("###### Text6 ######", (sy.HeaderToken(["######", "Text6 "]),)), ("###### Text6 ######", (sy.HeaderToken(["######", "Text6 "]),)),
("## Text2 ## {var='test', number=11}", (sy.HeaderToken(["##", "Text2 "]), # TODO : Re-implement that
sy.OptionalToken(["{", "var='test'", ",", "number=11", "}"]))), # ("## Text2 ## {var='test', number=11}", (sy.HeaderToken(["##", "Text2 "]),
("### Text3 ### [class='blue', 123#]{var='test', number=11}", (sy.HeaderToken(["###", "Text3 "]), # sy.OptionalToken(["{", "var='test'", ",", "number=11", "}"]))),
sy.OptionalToken( # ("### Text3 ### [class='blue', 123#]{var='test', number=11}", (sy.HeaderToken(["###", "Text3 "]),
["[", "class='blue'", ",", "123#", "]"]), # sy.OptionalToken(
# noqa E501 (line too long) # ["[", "class='blue'", ",", "123#", "]"]),
sy.OptionalToken( # # noqa E501 (line too long)
["{", "var='test'", ",", "number=11", # sy.OptionalToken(
"}"]))), # noqa E501 (line too long) # ["{", "var='test'", ",", "number=11",
# "}"]))), # noqa E501 (line too long)
], ],
# Tables # Tables
"table_row": [ "table_row": [
# Matches a table element, must be at the beginning of the line # Matches a table element, must be at the beginning of the line
("| Text1 | Text2 |", (sy.TableRowToken(["|", "Text1", "|", "Text2", "|"]),)), ("| Text1 | Text2 |", (sy.TableRowToken([sy.TableCellToken(["Text1 "]), sy.TableCellToken(["Text2 "]),]),)),
("| Text1 | Text2 | Text3 |", (sy.TableRowToken(["|", "Text1", "|", "Text2", "|", "Text3", "|"]),)), ("| Text1 | Text2 | Text3 |", (sy.TableRowToken([sy.TableCellToken(["Text1 "]), sy.TableCellToken(["Text2 "]), sy.TableCellToken(["Text3 "])]),)),
("|2 Text1 | Text2 |", (sy.TableRowToken(["|2", "Text1", "|", "Text2", "|"]),)), ("| 2 Text1 | Text2 |", (sy.TableRowToken([sy.TableCellToken(["2 Text1 "]), sy.TableCellToken(["Text2 "])]),)),
("|2 Text1 |3 Text2 |", (sy.TableRowToken(["|2", "Text1", "|3", "Text2", "|"]),)), ("|2 Text1 | Text2 |", (sy.TableRowToken(['2', sy.TableCellToken(["Text1 "]), sy.TableCellToken(["Text2 "])]),)),
("|2 Text1 |3 Text2 |4 Text3 |", (sy.TableRowToken(["|2", "Text1", "|3", "Text2", "|4", "Text3", "|"]),)), ("|2 Text1 |3 Text2 |", (sy.TableRowToken(['2', sy.TableCellToken(["Text1 "]), '3', sy.TableCellToken(["Text2 "])]),)),
("|3 Text1 | Text2 | {var='test', number=11}", (sy.TableRowToken(["|3", "Text1", "|", "Text2", "|"]), # ("|2 Text1 |3 Text2 |4 Text3 |", (sy.TableRowToken(["|2", "Text1", "|3", "Text2", "|4", "Text3", "|"]),)),
sy.UnimplementedToken( # ("|3 Text1 | Text2 | {var='test', number=11}", (sy.TableRowToken(["|3", "Text1", "|", "Text2", "|"]),
["{", "var='test'", ",", "number=11", "}"]))), # sy.UnimplementedToken(
# noqa E501 (line too long) # ["{", "var='test'", ",", "number=11", "}"]))),
], ],
"table_separator": [ "table_separator": [
# Matches a table element, must be at the beginning of the line # TODO: Tokens ? # Matches a table element, must be at the beginning of the line # TODO: Tokens ?
("|---|---|", (sy.TableSeparatorToken(["|", "---", "|", "---", "|"]),)), ("|---|---|", (sy.TableSeparatorToken(["---", "---"]),)),
("|---|---|---|", (sy.TableSeparatorToken(["|", "---", "|", "---", "|", "---", "|"]),)), ("|---|---|---|", (sy.TableSeparatorToken(["---", "---", "---"]),)),
("|:--|-:-|--:|--:|", (sy.TableSeparatorToken(["|", ":--", "|", "-:-", "|", "--:", "|", "--:", "|"]),)), ("|:--|-:-|--:|--:|", (sy.TableSeparatorToken([":--", "-:-", "--:", "--:"]),)),
], ],
"line": [ "line": [
# Match any line parsed by the parser (can match header, list table etc...) this is the main syntax element # Match any line parsed by the parser (can match header, list table etc...) this is the main syntax element
("# Text1 #", (sy.HeaderToken(["#", "Text1 "]),)), ("# Text1 #", (sy.HeaderToken(["#", "Text1 "]),)),
("Text *bold __underline__ still bold*", ( # TODO: old testing, needs freshening up
sy.TextToken(["Text ", sy.EtStrongToken(["bold", " ", sy.EtUnderlineToken(["underline"]), " still bold"])]),)), # noqa E501 (line too long) # ("Text *bold __underline__ still bold*", (
("|2 Text1 | Text2 |", (sy.TableRowToken(["|2", "Text1", "|", "Text2", "|"]),)), # sy.TextToken(["Text ", sy.EtStrongToken(["bold", " ", sy.EtUnderlineToken(["underline"]), " still bold"])]),)), # noqa E501 (line too long)
("|---|---|", (sy.TableSeparatorToken(["|", "---", "|", "---", "|"]),)), ("|2 Text1 | Text2 |", (sy.TableRowToken(["2", sy.TableCellToken(["Text1 "]), sy.TableCellToken(["Text2 "])]),)),
("|---|---|", (sy.TableSeparatorToken(["---", "---"]),)),
("- Text", (sy.EtUlistToken([sy.TextToken(["Text"])]),)), ("- Text", (sy.EtUlistToken([sy.TextToken(["Text"])]),)),
("div>>", (sy.StructuralElementEndToken(["div", ">>"]),)), ("div>>", (sy.StructuralElementEndToken(["div"]),)),
], ],
} }
@ -261,23 +264,23 @@ list_of_text_input_and_readable_output = [
("**Bold**", "et_strong", "<text:strong />"), ("**Bold**", "et_strong", "<text:strong />"),
("~~Strikethrough~~", "et_strikethrough", "<text:strikethrough />"), ("~~Strikethrough~~", "et_strikethrough", "<text:strikethrough />"),
("Text *bold __underline__ still bold*", "enhanced_text", ("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 />"),
# noqa E501 (line too long) # 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')' />"),
("Reverse order: (#123) Span, __underline__ ~~strikethrough~~ **emphasis * Strong**", "enhanced_text", ("Reverse order: (#123) Span, __underline__ ~~strikethrough~~ **emphasis * Strong**", "enhanced_text",
"Reverse order: (<text:custom_span = '(#123)' /> Span, <text:underline /> ~~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 />"), # noqa E501 (line too long)
# Structural elements # Structural elements
("<<div", "se", "<se:start = 'div' />"), ("<<div", "se", "<se:start = 'div' />"),
("div>>", "se", "<se:end = 'div' />"), ("div>>", "se", "<se:end = 'div' />"),
# Lists # Lists
("- Text", "one_ulist", "<list:ulist = '-,Text' />"), ("- Text", "one_ulist", "<list:ulist = 'Text' />"),
("#. Text", "one_olist", "<list:olist = '#,Text' />"), ("#. Text", "one_olist", "<list:olist = 'Text' />"),
# Tables # Tables
("| Text1 | Text2 |", "table", "<table: | Text1 | Text2 |>"), ("| Text1 | Text2 |", "table", "<table:row = '<table:cell = 'Text1 ' />,<table:cell = 'Text2 ' />' />"),
("| Text1 |3 Text2 |", "table", "<table: | Text1 |3 Text2 |>"), ("| Text1 |3 Text2 |", "table", "<table:row = '<table:cell = 'Text1 ' />,3,<table:cell = 'Text2 ' />' />"),
("|---|---|", "table_separator", "<table:separator = '---,---' />"), ("|---|---|", "table_separator", "<table:separator = '---,---' />"),
("|:--|-:-|--:|--:|", "table_separator", "<table:separator = ':--,-:-,--:,--:' />"), ("|:--|-:-|--:|--:|", "table_separator", "<table:separator = ':--,-:-,--:,--:' />"),