merge
This commit is contained in:
commit
bcfb07e5c3
|
|
@ -1,7 +1,7 @@
|
||||||
pyyaml==6.0
|
pyyaml==6.0
|
||||||
rich~=11.0.0
|
rich==12.0.1
|
||||||
regex~=2022.1.18
|
regex==2022.3.15
|
||||||
pyparse~=1.1.7
|
pyparse~=1.1.7
|
||||||
pyparsing[diagrams]~=3.0.7
|
pyparsing[diagrams]~=3.0.7
|
||||||
|
|
||||||
pytest~=6.2.5
|
pytest==7.1.1
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
flake8==4.0.1
|
flake8==4.0.1
|
||||||
tox==3.24.3
|
tox==3.24.5
|
||||||
pytest==6.2.5
|
pytest==7.1.1
|
||||||
pytest-cov==2.12.1
|
pytest-cov==3.0.0
|
||||||
mypy===0.910
|
mypy==0.941
|
||||||
pyyaml==6.0
|
pyyaml==6.0
|
||||||
rich~=11.0.0
|
rich==12.0.1
|
||||||
regex~=2022.1.18
|
regex==2022.3.15
|
||||||
|
|
|
||||||
|
|
@ -1 +1,18 @@
|
||||||
# Interprets where the parser is located and resolves errors
|
# Interprets where the parser is located and resolves errors
|
||||||
|
class BaseContainer:
|
||||||
|
pass
|
||||||
|
# Must define __init__, __str__, __repr__ and a print_all method
|
||||||
|
# will contain all sub-tokens and/or sub-containers
|
||||||
|
# Defines a method to get all token elements (__iter__) and a method to get a specific element (__getitem__)
|
||||||
|
# Defines a method to get the number of elements (__len__)
|
||||||
|
# Defines a method to get the name of the container (__name__)
|
||||||
|
# Defines a method to the optionals inside the container ( ? .optional())
|
||||||
|
|
||||||
|
# Define containers all the Enhanced text elements, divs, headers, list and any element that can be a container
|
||||||
|
|
||||||
|
|
||||||
|
class ContextManager:
|
||||||
|
pass
|
||||||
|
# Defines all methods to manage the context of the parser as a file is being parsed
|
||||||
|
# Builds all containers as the parser is parsing the file
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ class SemanticType:
|
||||||
for elt in self.content:
|
for elt in self.content:
|
||||||
try:
|
try:
|
||||||
return_list.append(elt.to_markup())
|
return_list.append(elt.to_markup())
|
||||||
except Exception:
|
except Exception: # noqa E722 (This function is used for testing only)
|
||||||
return_list.append(str(elt))
|
return_list.append(str(elt))
|
||||||
return f"<{self.label} = '{','.join(return_list)}' />"
|
return f"<{self.label} = '{','.join(return_list)}' />"
|
||||||
return f'<[NOC] {self.label} />'
|
return f'<[NOC] {self.label} />'
|
||||||
|
|
@ -212,8 +212,8 @@ def reparse(parse_element):
|
||||||
:param parse_element: pp object defining desired match
|
:param parse_element: pp object defining desired match
|
||||||
:return: returns a function parsing given match with specified parsing element
|
:return: returns a function parsing given match with specified parsing element
|
||||||
"""
|
"""
|
||||||
def _reparse(original_string, _, __):
|
def _reparse(__, _, tokens):
|
||||||
parse_element.parse_string(original_string)
|
return parse_element.parseString(tokens[0]) # TODO: fix this with a proper solution
|
||||||
|
|
||||||
return _reparse
|
return _reparse
|
||||||
|
|
||||||
|
|
@ -232,7 +232,7 @@ def readable_markup(list_of_tokens):
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
readable_list.append(token.to_markup())
|
readable_list.append(token.to_markup())
|
||||||
except Exception:
|
except Exception: # noqa E722 (This function is used for testing and readability purposes)
|
||||||
readable_list.append(str(token))
|
readable_list.append(str(token))
|
||||||
return " ".join(readable_list)
|
return " ".join(readable_list)
|
||||||
|
|
||||||
|
|
@ -243,7 +243,7 @@ value = (pps(quotes) + pp.Word(pp.alphanums + r'.') + pps(pp.match_previous_lite
|
||||||
pp.common.fnumber)("value")
|
pp.common.fnumber)("value")
|
||||||
assignation = pp.Group(
|
assignation = pp.Group(
|
||||||
pp.common.identifier('var_name') + pps('=') + value('var_value')
|
pp.common.identifier('var_name') + pps('=') + value('var_value')
|
||||||
)("assignation") # TODO: More extensive testing
|
)("assignation")
|
||||||
text = pp.OneOrMore(pp.Word(pp.alphanums))('text').add_parse_action(of_type(TextToken))
|
text = pp.OneOrMore(pp.Word(pp.alphanums))('text').add_parse_action(of_type(TextToken))
|
||||||
url_characters = pp.common.url
|
url_characters = pp.common.url
|
||||||
|
|
||||||
|
|
@ -251,20 +251,20 @@ url_characters = pp.common.url
|
||||||
var = pps('[') + pp.delimitedList(
|
var = pps('[') + pp.delimitedList(
|
||||||
assignation.add_parse_action(of_type(BeAssignToken)) ^
|
assignation.add_parse_action(of_type(BeAssignToken)) ^
|
||||||
value.add_parse_action(of_type(BeValueToken))
|
value.add_parse_action(of_type(BeValueToken))
|
||||||
)("list_vars").set_name("list_vars") + pps(']') # TODO: More extensive testing
|
)("list_vars").set_name("list_vars") + pps(']')
|
||||||
|
|
||||||
# Specific elements
|
# Specific elements
|
||||||
image_element = ('@{' + pp.common.identifier('image_name') + '}')("image_element")
|
image_element = ('@{' + pp.common.identifier('image_name') + '}')("image_element")
|
||||||
alias_element = ('@[' + pp.common.identifier('alias_name') + ']')("alias_element")
|
alias_element = ('@[' + pp.common.identifier('alias_name') + ']')("alias_element")
|
||||||
expression = pp.Word(pp.alphanums + r'=+-_\'",;:!\/\\ ')
|
expression = pp.Word(pp.alphanums + r'=+-_\'",;:!\/\\. ')
|
||||||
html_insert = pps('{') + expression('html_insert') + pps('}') # TODO: More extensive testing
|
html_insert = pps('{') + expression('html_insert') + pps('}')
|
||||||
class_insert = pps('{{') + expression('class_insert') + pps('}}') # TODO: test dis
|
class_insert = pps('{{') + expression('class_insert') + pps('}}')
|
||||||
|
|
||||||
# Optional elements
|
# Optional elements
|
||||||
optional = (
|
optional = pp.OneOrMore(
|
||||||
pp.Opt(class_insert)("class_insert").add_parse_action(of_type(OptionalClassToken)) &
|
class_insert("class_insert").add_parse_action(of_type(OptionalClassToken)) ^
|
||||||
pp.Opt(html_insert)("html_insert").add_parse_action(of_type(OptionalInsertToken)) &
|
html_insert("html_insert").add_parse_action(of_type(OptionalInsertToken)) ^
|
||||||
pp.Opt(var)("var").add_parse_action(of_type(OptionalVarToken)) # noqa TODO: add class_insert to tests
|
var("var").add_parse_action(of_type(OptionalVarToken))
|
||||||
)("optional").add_parse_action(of_type(OptionalToken)) # Macro OptionalToken
|
)("optional").add_parse_action(of_type(OptionalToken)) # Macro OptionalToken
|
||||||
|
|
||||||
# Structural elements
|
# Structural elements
|
||||||
|
|
@ -279,7 +279,7 @@ display_element = pp.Word('!')
|
||||||
|
|
||||||
# Inline elements
|
# Inline elements
|
||||||
il_link = pp.Regex(
|
il_link = pp.Regex(
|
||||||
r"""\[(?P<text>.+)\]\(['"](?P<url>[a-zA-Z-_:\/=@#!%\?\d\(\)\.]+)['"]\)"""
|
r"""\[(?P<text>.+)\]\(['"]?(?P<url>[a-zA-Z-_:\/=@#!%\?\d\(\)\.]+)['"]?\)"""
|
||||||
).add_parse_action(of_type(HyperlinkToken))
|
).add_parse_action(of_type(HyperlinkToken))
|
||||||
|
|
||||||
# Enhanced text elements
|
# Enhanced text elements
|
||||||
|
|
@ -299,7 +299,7 @@ enhanced_text = pp.ZeroOrMore(
|
||||||
|
|
||||||
# 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)) + optional
|
se_end = (structural_elements + pps('>>')).add_parse_action(of_type(StructuralElementEndToken)) + pp.Opt(optional)
|
||||||
se = se_end | se_start # Structural element
|
se = se_end | se_start # Structural element
|
||||||
table_row = pp.OneOrMore(
|
table_row = pp.OneOrMore(
|
||||||
# pp.Regex(r'\|(\d)?')('table_colspan') +
|
# pp.Regex(r'\|(\d)?')('table_colspan') +
|
||||||
|
|
@ -322,24 +322,30 @@ multi_line = se | table | quotation
|
||||||
|
|
||||||
# Oneline elements
|
# Oneline elements
|
||||||
one_header = (
|
one_header = (
|
||||||
header_element + pp.SkipTo(pp.match_previous_literal(header_element))
|
header_element +
|
||||||
|
pp.SkipTo(pp.match_previous_literal(header_element)) +
|
||||||
|
pps(pp.match_previous_literal(header_element)) +
|
||||||
|
pp.Opt(optional)
|
||||||
).add_parse_action(of_type(HeaderToken))
|
).add_parse_action(of_type(HeaderToken))
|
||||||
one_display = (
|
one_display = (
|
||||||
display_element + pp.SkipTo(pp.match_previous_literal(display_element))
|
display_element +
|
||||||
|
pp.SkipTo(pp.match_previous_literal(display_element)) +
|
||||||
|
pps(pp.match_previous_literal(display_element)) +
|
||||||
|
pp.Opt(optional)
|
||||||
).add_parse_action(of_type(DisplayToken))
|
).add_parse_action(of_type(DisplayToken))
|
||||||
one_olist = pp.line_start + (
|
one_olist = pp.line_start + (
|
||||||
pps(pp.Literal('#.')) + (
|
pps(pp.Literal('#.')) + (
|
||||||
(pp.SkipTo(optional)('text').add_parse_action(of_type(TextToken)) + optional) ^ # TODO: add reparse
|
(pp.SkipTo(optional)('text').add_parse_action(reparse(enhanced_text)) + optional) |
|
||||||
enhanced_text)
|
enhanced_text)
|
||||||
).add_parse_action(of_type(EtOlistToken))
|
).add_parse_action(of_type(EtOlistToken))
|
||||||
one_ulist = pp.line_start + (
|
one_ulist = pp.line_start + (
|
||||||
pps(pp.Literal('-')) + (
|
pps(pp.Literal('-')) + (
|
||||||
(pp.SkipTo(optional)('text').add_parse_action(of_type(TextToken)) + optional) ^ # TODO: add reparse
|
(pp.SkipTo(optional)('text').add_parse_action(reparse(enhanced_text)) + optional) |
|
||||||
enhanced_text)
|
enhanced_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) + pp.Opt(optional)
|
one_line = (one_header | one_display | one_olist | one_ulist)
|
||||||
|
|
||||||
# Final elements
|
# Final elements
|
||||||
line = one_line | multi_line | enhanced_text
|
line = one_line | multi_line | enhanced_text
|
||||||
|
|
@ -350,8 +356,8 @@ line = one_line | multi_line | enhanced_text
|
||||||
##############################################################################
|
##############################################################################
|
||||||
|
|
||||||
# Composite elements
|
# Composite elements
|
||||||
image = image_element + optional
|
image = image_element + pp.Opt(optional)
|
||||||
alias = alias_element + optional
|
alias = alias_element + pp.Opt(optional)
|
||||||
|
|
||||||
# Syntax elements
|
# Syntax elements
|
||||||
line_to_replace = pp.OneOrMore(
|
line_to_replace = pp.OneOrMore(
|
||||||
|
|
@ -371,4 +377,4 @@ if __name__ == '__main__': # pragma: no cover
|
||||||
line.create_diagram("../../../dev_outputs/diagram_line.html")
|
line.create_diagram("../../../dev_outputs/diagram_line.html")
|
||||||
multi_line.create_diagram("../../../dev_outputs/diagram_multi_line.html")
|
multi_line.create_diagram("../../../dev_outputs/diagram_multi_line.html")
|
||||||
one_line.create_diagram("../../../dev_outputs/diagram_one_line.html")
|
one_line.create_diagram("../../../dev_outputs/diagram_one_line.html")
|
||||||
enhanced_text.create_diagram("../../../dev_outputs/diagram_enchanced_text.html")
|
enhanced_text.create_diagram("../../../dev_outputs/diagram_enhanced_text.html")
|
||||||
|
|
|
||||||
|
|
@ -2,19 +2,33 @@ from itertools import zip_longest
|
||||||
|
|
||||||
import pyparsing
|
import pyparsing
|
||||||
import pytest
|
import pytest
|
||||||
import inspect
|
|
||||||
|
|
||||||
|
|
||||||
import bootstraparse.modules.syntax as sy
|
import bootstraparse.modules.syntax as sy
|
||||||
|
from tools import __GL, __module_path, find_variables_in_file # , find_functions_in_file, find_classes_in_file
|
||||||
|
|
||||||
ptp = pytest.param
|
ptp = pytest.param
|
||||||
__XF = pytest.mark.xfail
|
__XF = pytest.mark.xfail
|
||||||
|
|
||||||
|
|
||||||
# Cursed frame inspection
|
##############################################################################################################
|
||||||
def __GL():
|
# Utility functions
|
||||||
return inspect.getframeinfo(inspect.currentframe().f_back).lineno
|
##############################################################################################################
|
||||||
|
def likely_definition(list_expression, file):
|
||||||
|
"""
|
||||||
|
Returns a dict for each expression and the associated line where this expression is likely to be defined.
|
||||||
|
:param list_expression: list of expressions
|
||||||
|
:param file: file to search in
|
||||||
|
:return: dict of expression and line
|
||||||
|
"""
|
||||||
|
likely_definition_dict = {}
|
||||||
|
loop_list = [e for e in list_expression]
|
||||||
|
with open(file) as f:
|
||||||
|
for line_number, line in enumerate(f):
|
||||||
|
for expression_id, expression in enumerate(loop_list):
|
||||||
|
if expression in line:
|
||||||
|
likely_definition_dict[expression] = line_number+1
|
||||||
|
loop_list.remove(expression)
|
||||||
|
return likely_definition_dict, file
|
||||||
|
|
||||||
|
|
||||||
##############################################################################################################
|
##############################################################################################################
|
||||||
|
|
@ -39,6 +53,7 @@ list_of_token_types = {
|
||||||
}
|
}
|
||||||
|
|
||||||
# Dictionary of all lexical elements and a list of matching strings for the Pre_parser
|
# Dictionary of all lexical elements and a list of matching strings for the Pre_parser
|
||||||
|
# test_expression_matching
|
||||||
expressions_to_match = {
|
expressions_to_match = {
|
||||||
# Base elements
|
# Base elements
|
||||||
"quotes": ["'hi, there'", "'hi, there'"],
|
"quotes": ["'hi, there'", "'hi, there'"],
|
||||||
|
|
@ -59,7 +74,16 @@ expressions_to_match = {
|
||||||
"html_insert": ['{test=12, 22=3, "=5}', "{testing=12, 22=3, 4=5, 6='7'}"],
|
"html_insert": ['{test=12, 22=3, "=5}', "{testing=12, 22=3, 4=5, 6='7'}"],
|
||||||
|
|
||||||
# Optional elements
|
# Optional elements
|
||||||
"optional": ["a=1", "a=1.33", "tr2='hu'"],
|
"optional": ["[a=1]", "[a=1, b=2]",
|
||||||
|
"{a=1'\";}",
|
||||||
|
"[a=1, b='2', 43, 'eee']",
|
||||||
|
"[a=1, b='2', 43, 'eee'] {azer=,;}",
|
||||||
|
"{{a=1, b='2', 43, 'eee'}}",
|
||||||
|
"{{a=1, b='2', 43, 'eee'}} {azer=,;}",
|
||||||
|
"{{a=1, b='2', 43, 'eee'}} {azer=,;} [azer=,;]",
|
||||||
|
"[a=1, b='2', 43, 'eee'] {azer=,;} {{azer=,;}}",
|
||||||
|
],
|
||||||
|
|
||||||
|
|
||||||
# Pre_parser elements
|
# Pre_parser elements
|
||||||
"image": ["@{image}{test=22}[a=12,22,c,d,ERE,r,3]", "@{image123_456}{a=12,22,c,d,ERE,r,3}",
|
"image": ["@{image}{test=22}[a=12,22,c,d,ERE,r,3]", "@{image123_456}{a=12,22,c,d,ERE,r,3}",
|
||||||
|
|
@ -110,8 +134,117 @@ dict_advanced_syntax_input_and_expected_output = {
|
||||||
("[text_link]('text://www.website.com/link.html')",
|
("[text_link]('text://www.website.com/link.html')",
|
||||||
[sy.HyperlinkToken(["[text_link]('text://www.website.com/link.html')"])], __GL())
|
[sy.HyperlinkToken(["[text_link]('text://www.website.com/link.html')"])], __GL())
|
||||||
],
|
],
|
||||||
|
"assignation": [
|
||||||
# il_link | et_strong | et_em | et_strikethrough | et_underline | et_custom_span
|
# Single assignation element, should only match the first "a=1"
|
||||||
|
("a=1", [sy.BeAssignToken([["a", 1]]), ], __GL()),
|
||||||
|
("a='str'", [sy.BeAssignToken([["a", 'str']]), ], __GL()),
|
||||||
|
],
|
||||||
|
"var": [
|
||||||
|
# List of variables or assignations, should match "[a=12, b=13, 'c']"
|
||||||
|
("[a=12, b=13, 'c']", [
|
||||||
|
sy.BeAssignToken([["a", 12]]),
|
||||||
|
sy.BeAssignToken([["b", 13]]),
|
||||||
|
sy.BeValueToken(["c"]),
|
||||||
|
], __GL()),
|
||||||
|
("[a=12, b=13, 'c', d=14]", [
|
||||||
|
sy.BeAssignToken([["a", 12]]),
|
||||||
|
sy.BeAssignToken([["b", 13]]),
|
||||||
|
sy.BeValueToken(["c"]),
|
||||||
|
sy.BeAssignToken([["d", 14]]),
|
||||||
|
], __GL()),
|
||||||
|
],
|
||||||
|
"optional": [
|
||||||
|
# Optional element, should match any optional element
|
||||||
|
("[a=12, b=13, 'c']", [
|
||||||
|
sy.OptionalToken([
|
||||||
|
sy.OptionalVarToken([
|
||||||
|
sy.BeAssignToken([["a", 12]]),
|
||||||
|
sy.BeAssignToken([["b", 13]]),
|
||||||
|
sy.BeValueToken(["c"]),
|
||||||
|
]),
|
||||||
|
]),
|
||||||
|
], __GL()),
|
||||||
|
("[a=12, b=13, 'c', d=14]", [
|
||||||
|
sy.OptionalToken([
|
||||||
|
sy.OptionalVarToken([
|
||||||
|
sy.BeAssignToken([["a", 12]]),
|
||||||
|
sy.BeAssignToken([["b", 13]]),
|
||||||
|
sy.BeValueToken(["c"]),
|
||||||
|
sy.BeAssignToken([["d", 14]]),
|
||||||
|
]),
|
||||||
|
]),
|
||||||
|
], __GL()),
|
||||||
|
("[a=12]{aze,/.}", (
|
||||||
|
sy.OptionalToken([
|
||||||
|
sy.OptionalVarToken([
|
||||||
|
sy.BeAssignToken([["a", 12]]),
|
||||||
|
]),
|
||||||
|
sy.OptionalInsertToken([
|
||||||
|
"aze,/.",
|
||||||
|
]),
|
||||||
|
]),
|
||||||
|
), __GL()),
|
||||||
|
("{aze,/.}[a=12]", (
|
||||||
|
sy.OptionalToken([
|
||||||
|
sy.OptionalInsertToken([
|
||||||
|
"aze,/.",
|
||||||
|
]),
|
||||||
|
sy.OptionalVarToken([
|
||||||
|
sy.BeAssignToken([["a", 12]]),
|
||||||
|
]),
|
||||||
|
]),
|
||||||
|
), __GL()),
|
||||||
|
("{aze!=}", (
|
||||||
|
sy.OptionalToken([
|
||||||
|
sy.OptionalInsertToken([
|
||||||
|
"aze!=",
|
||||||
|
]),
|
||||||
|
]),
|
||||||
|
), __GL()),
|
||||||
|
("{{aze!=}}", (
|
||||||
|
sy.OptionalToken([
|
||||||
|
sy.OptionalClassToken([
|
||||||
|
"aze!=",
|
||||||
|
]),
|
||||||
|
]),
|
||||||
|
), __GL()),
|
||||||
|
("{{aze!=}}{azett!=}", (
|
||||||
|
sy.OptionalToken([
|
||||||
|
sy.OptionalClassToken([
|
||||||
|
"aze!=",
|
||||||
|
]),
|
||||||
|
sy.OptionalInsertToken([
|
||||||
|
"azett!=",
|
||||||
|
]),
|
||||||
|
]),
|
||||||
|
), __GL()),
|
||||||
|
("{{aze!=}}{azett!=}[a=12]", (
|
||||||
|
sy.OptionalToken([
|
||||||
|
sy.OptionalClassToken([
|
||||||
|
"aze!=",
|
||||||
|
]),
|
||||||
|
sy.OptionalInsertToken([
|
||||||
|
"azett!=",
|
||||||
|
]),
|
||||||
|
sy.OptionalVarToken([
|
||||||
|
sy.BeAssignToken([["a", 12]]),
|
||||||
|
]),
|
||||||
|
]),
|
||||||
|
), __GL()),
|
||||||
|
("[a=12]{aze,/.}{{azer!=}}", (
|
||||||
|
sy.OptionalToken([
|
||||||
|
sy.OptionalVarToken([
|
||||||
|
sy.BeAssignToken([["a", 12]]),
|
||||||
|
]),
|
||||||
|
sy.OptionalInsertToken([
|
||||||
|
"aze,/.",
|
||||||
|
]),
|
||||||
|
sy.OptionalClassToken([
|
||||||
|
"azer!=",
|
||||||
|
]),
|
||||||
|
]),
|
||||||
|
), __GL()),
|
||||||
|
],
|
||||||
# Enhanced Text
|
# Enhanced Text
|
||||||
"enhanced_text": [
|
"enhanced_text": [
|
||||||
# Matches a line of text, with or without inline elements
|
# Matches a line of text, with or without inline elements
|
||||||
|
|
@ -155,7 +288,18 @@ dict_advanced_syntax_input_and_expected_output = {
|
||||||
sy.EtStrikethroughToken(["~~"]), sy.TextToken(["Strikethrough"]),
|
sy.EtStrikethroughToken(["~~"]), sy.TextToken(["Strikethrough"]),
|
||||||
), __GL()),
|
), __GL()),
|
||||||
],
|
],
|
||||||
|
"html_insert": [
|
||||||
|
# Match an HTML insert
|
||||||
|
("{azer='\"}", (
|
||||||
|
"azer='\"",
|
||||||
|
), __GL()),
|
||||||
|
],
|
||||||
|
"class_insert": [
|
||||||
|
# Match a class insert
|
||||||
|
("{{azer='\"}}", (
|
||||||
|
"azer='\"",
|
||||||
|
), __GL()),
|
||||||
|
],
|
||||||
# Structural Elements
|
# Structural Elements
|
||||||
"se_start": [
|
"se_start": [
|
||||||
# Matches a start of a structural element
|
# Matches a start of a structural element
|
||||||
|
|
@ -245,7 +389,7 @@ dict_advanced_syntax_input_and_expected_output = {
|
||||||
sy.OptionalInsertToken(["var='test', number=11"]),
|
sy.OptionalInsertToken(["var='test', number=11"]),
|
||||||
]),
|
]),
|
||||||
]),
|
]),
|
||||||
), __GL(), __XF),
|
), __GL()),
|
||||||
("#. Text8 [class='blue', 123]{var='test', number=11}", (
|
("#. Text8 [class='blue', 123]{var='test', number=11}", (
|
||||||
sy.EtOlistToken([
|
sy.EtOlistToken([
|
||||||
sy.TextToken(["Text8"]),
|
sy.TextToken(["Text8"]),
|
||||||
|
|
@ -257,11 +401,11 @@ dict_advanced_syntax_input_and_expected_output = {
|
||||||
sy.OptionalInsertToken(["var='test', number=11"]),
|
sy.OptionalInsertToken(["var='test', number=11"]),
|
||||||
]),
|
]),
|
||||||
]),
|
]),
|
||||||
), __GL(), __XF),
|
), __GL()),
|
||||||
("! Display9 ! [class='blue', 123]{var='test', number=11}", (
|
("! Display9 ! [class='blue', 123]{var='test', number=11}", (
|
||||||
sy.DisplayToken([
|
sy.DisplayToken([
|
||||||
"!",
|
"!",
|
||||||
"Display9",
|
"Display9 ",
|
||||||
sy.OptionalToken([
|
sy.OptionalToken([
|
||||||
sy.OptionalVarToken([
|
sy.OptionalVarToken([
|
||||||
sy.BeAssignToken([["class", "blue"]]),
|
sy.BeAssignToken([["class", "blue"]]),
|
||||||
|
|
@ -270,11 +414,11 @@ dict_advanced_syntax_input_and_expected_output = {
|
||||||
sy.OptionalInsertToken(["var='test', number=11"]),
|
sy.OptionalInsertToken(["var='test', number=11"]),
|
||||||
]),
|
]),
|
||||||
]),
|
]),
|
||||||
), __GL(), __XF), # Bad optional implementation
|
), __GL()), # Bad optional implementation
|
||||||
("## Text10 ## [class='blue', 123]{var='test', number=11}", (
|
("## Text10 ## [class='blue', 123]{var='test', number=11}", (
|
||||||
sy.HeaderToken([
|
sy.HeaderToken([
|
||||||
"##",
|
"##",
|
||||||
"Text10",
|
"Text10 ",
|
||||||
sy.OptionalToken([
|
sy.OptionalToken([
|
||||||
sy.OptionalVarToken([
|
sy.OptionalVarToken([
|
||||||
sy.BeAssignToken([["class", "blue"]]),
|
sy.BeAssignToken([["class", "blue"]]),
|
||||||
|
|
@ -283,7 +427,7 @@ dict_advanced_syntax_input_and_expected_output = {
|
||||||
sy.OptionalInsertToken(["var='test', number=11"]),
|
sy.OptionalInsertToken(["var='test', number=11"]),
|
||||||
]),
|
]),
|
||||||
]),
|
]),
|
||||||
), __GL(), __XF), # Bad optional implementation
|
), __GL()), # Bad optional implementation
|
||||||
],
|
],
|
||||||
|
|
||||||
# Tables
|
# Tables
|
||||||
|
|
@ -328,7 +472,7 @@ dict_advanced_syntax_input_and_expected_output = {
|
||||||
"var='test', number=11",
|
"var='test', number=11",
|
||||||
]),
|
]),
|
||||||
]),
|
]),
|
||||||
), __GL(), __XF), # Bad optional implementation
|
), __GL()),
|
||||||
],
|
],
|
||||||
|
|
||||||
"table_separator": [
|
"table_separator": [
|
||||||
|
|
@ -357,6 +501,31 @@ dict_advanced_syntax_input_and_expected_output = {
|
||||||
("|---|---|", (sy.TableSeparatorToken(["---", "---"]),), __GL()),
|
("|---|---|", (sy.TableSeparatorToken(["---", "---"]),), __GL()),
|
||||||
("- Text", (sy.EtUlistToken([sy.TextToken(["Text"])]),), __GL()),
|
("- Text", (sy.EtUlistToken([sy.TextToken(["Text"])]),), __GL()),
|
||||||
("div>>", (sy.StructuralElementEndToken(["div"]),), __GL()),
|
("div>>", (sy.StructuralElementEndToken(["div"]),), __GL()),
|
||||||
|
("#. Text with *em* and __underline__ and {arguments}{{super-arguments}}[a=1,'ui']", (
|
||||||
|
sy.EtOlistToken([
|
||||||
|
sy.TextToken(["Text with"]),
|
||||||
|
sy.EtEmToken(["*"]),
|
||||||
|
sy.TextToken(["em"]),
|
||||||
|
sy.EtEmToken(["*"]),
|
||||||
|
sy.TextToken(["and"]),
|
||||||
|
sy.EtUnderlineToken(["__"]),
|
||||||
|
sy.TextToken(["underline"]),
|
||||||
|
sy.EtUnderlineToken(["__"]),
|
||||||
|
sy.TextToken(["and"]),
|
||||||
|
sy.OptionalToken([
|
||||||
|
sy.OptionalInsertToken([
|
||||||
|
"arguments",
|
||||||
|
]),
|
||||||
|
sy.OptionalClassToken([
|
||||||
|
"super-arguments",
|
||||||
|
]),
|
||||||
|
sy.OptionalVarToken([
|
||||||
|
sy.BeAssignToken([["a", 1]]),
|
||||||
|
sy.BeValueToken(["ui"]),
|
||||||
|
]),
|
||||||
|
]),
|
||||||
|
]),
|
||||||
|
), __GL(),),
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -368,6 +537,9 @@ zipped_dict_advanced_syntax_input_and_expected_output = [
|
||||||
] for item in sublist
|
] for item in sublist
|
||||||
]
|
]
|
||||||
|
|
||||||
|
__syntax_file = __module_path("syntax.py")
|
||||||
|
__definition_of_syntax_elements = find_variables_in_file(__syntax_file, dict_advanced_syntax_input_and_expected_output.keys())
|
||||||
|
|
||||||
# test__add_tag
|
# test__add_tag
|
||||||
list_add_tag_input_and_expected_output = [
|
list_add_tag_input_and_expected_output = [
|
||||||
# Test cases for the add_tag function
|
# Test cases for the add_tag function
|
||||||
|
|
@ -421,6 +593,27 @@ list_of_text_input_and_readable_output = [
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
list_of_reparsing_input_and_expected_output = [
|
||||||
|
("reparse_test", "enhanced_text", [sy.TextToken(["reparse_test"])], __GL()),
|
||||||
|
("reparse text *bold*, __underlined__ (#123) Custom", "enhanced_text", [
|
||||||
|
sy.TextToken(["reparse text"]),
|
||||||
|
sy.EtEmToken(["*"]),
|
||||||
|
sy.TextToken(["bold"]),
|
||||||
|
sy.EtEmToken(["*"]),
|
||||||
|
sy.TextToken([","]),
|
||||||
|
sy.EtUnderlineToken(["__"]),
|
||||||
|
sy.TextToken(["underlined"]),
|
||||||
|
sy.EtUnderlineToken(["__"]),
|
||||||
|
sy.EtCustomSpanToken(["123"]),
|
||||||
|
sy.TextToken(["Custom"])
|
||||||
|
], __GL())
|
||||||
|
]
|
||||||
|
final_list_of_reparsing_input_and_expected_output = [
|
||||||
|
ptp(item[0], item[1], item[2], item[3], id=f'{item[0]} (line:{item[3]})', marks=item[4:])
|
||||||
|
for item in list_of_reparsing_input_and_expected_output
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("token_name,token_class", list_of_token_types.items())
|
@pytest.mark.parametrize("token_name,token_class", list_of_token_types.items())
|
||||||
def test_token_type_exists(token_name, token_class):
|
def test_token_type_exists(token_name, token_class):
|
||||||
"""
|
"""
|
||||||
|
|
@ -503,6 +696,9 @@ def test_semantic_type_eq():
|
||||||
st1 = sy.SemanticType([1, 2, 3])
|
st1 = sy.SemanticType([1, 2, 3])
|
||||||
st2 = sy.SemanticType([1, 2, 3, 4])
|
st2 = sy.SemanticType([1, 2, 3, 4])
|
||||||
assert st1 != st2
|
assert st1 != st2
|
||||||
|
st1 = sy.SemanticType([[1, 2, 4]])
|
||||||
|
st2 = sy.SemanticType([[1, 2, 3]])
|
||||||
|
assert st1 != st2
|
||||||
|
|
||||||
|
|
||||||
def find_expression_from_str(expression_str):
|
def find_expression_from_str(expression_str):
|
||||||
|
|
@ -551,8 +747,11 @@ def test_advanced_expression_and_token_creation(markup_element, to_parse, expect
|
||||||
assert isinstance(expr, pyparsing.ParserElement)
|
assert isinstance(expr, pyparsing.ParserElement)
|
||||||
result = expr.parse_string(to_parse)
|
result = expr.parse_string(to_parse)
|
||||||
print()
|
print()
|
||||||
print(f"Parsing expression: {to_parse} with {markup_element}")
|
print(f"Parsing string: '{to_parse}'")
|
||||||
print("Defined at %(filename)s:%(lineno)d" % {'filename': __file__, 'lineno': line_test})
|
print("Defined at %(filename)s:%(lineno)d" % {'filename': __file__, 'lineno': line_test})
|
||||||
|
print(f"With expression: '{markup_element}'")
|
||||||
|
print("Defined at %(filename)s:%(lineno)d" % {'filename': __syntax_file,
|
||||||
|
'lineno': __definition_of_syntax_elements[markup_element]})
|
||||||
print(f"Found: {result} (len:{len(result)}).")
|
print(f"Found: {result} (len:{len(result)}).")
|
||||||
print(f"Expected: {expected} (len:{len(expected)})")
|
print(f"Expected: {expected} (len:{len(expected)})")
|
||||||
assert result is not None
|
assert result is not None
|
||||||
|
|
@ -614,3 +813,33 @@ def test_weird_markup():
|
||||||
|
|
||||||
assert sy.readable_markup(
|
assert sy.readable_markup(
|
||||||
[sy.EmptySemanticType("weird"), empty_token, WeirdClass()]) == "weird <[NOC] weird /> weird"
|
[sy.EmptySemanticType("weird"), empty_token, WeirdClass()]) == "weird <[NOC] weird /> weird"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("original_string, reparse_with, expected_output, line_test",
|
||||||
|
final_list_of_reparsing_input_and_expected_output)
|
||||||
|
def test_reparse(original_string, reparse_with, expected_output, line_test):
|
||||||
|
"""
|
||||||
|
Test the reparse function
|
||||||
|
"""
|
||||||
|
print()
|
||||||
|
print(f"Reparsing string: '{original_string}'")
|
||||||
|
print("Defined at %(filename)s:%(lineno)d" % {'filename': __file__, 'lineno': int(line_test)})
|
||||||
|
print(f"With expression: '{reparse_with}'")
|
||||||
|
print("Defined at %(filename)s:%(lineno)d" % {'filename': __syntax_file,
|
||||||
|
'lineno': __definition_of_syntax_elements[reparse_with]})
|
||||||
|
print(f"Expected: {expected_output} (len:{len(expected_output)})")
|
||||||
|
|
||||||
|
expression = find_expression_from_str(str(reparse_with))
|
||||||
|
reparse_action = pyparsing.Word(pyparsing.printables+" ").add_parse_action(sy.reparse(expression))
|
||||||
|
reparse_result = reparse_action.parseString(str(original_string))
|
||||||
|
print(f"Found: {reparse_result} (len:{len(reparse_result)}).")
|
||||||
|
|
||||||
|
assert reparse_result is not None
|
||||||
|
for expected, result in zip_longest(expected_output, reparse_result):
|
||||||
|
assert result == expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_empty_token():
|
||||||
|
"""Test that empty tokens are correctly dropped."""
|
||||||
|
|
||||||
|
assert sy.of_type(sy.EmptySemanticType)("", [], []) is None
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,113 @@
|
||||||
|
import inspect
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
# List of tools for testing
|
||||||
|
###############################################################################
|
||||||
|
###############################################################################
|
||||||
|
# Paths
|
||||||
|
########################################
|
||||||
|
|
||||||
|
|
||||||
|
def __base():
|
||||||
|
"""
|
||||||
|
Return the path of the main module (relative to the tests)
|
||||||
|
:return: Path of the main module
|
||||||
|
:rtype: str
|
||||||
|
"""
|
||||||
|
return "../../src/bootstraparse/"
|
||||||
|
|
||||||
|
|
||||||
|
def __module_path(module_name):
|
||||||
|
"""
|
||||||
|
Return the path of the module module_name
|
||||||
|
:param module_name: Name of the module
|
||||||
|
:type module_name: str
|
||||||
|
:return: Path of the module
|
||||||
|
:rtype: str
|
||||||
|
"""
|
||||||
|
return os.path.normpath(os.path.join(os.path.dirname(__file__), __base(), "modules", module_name))
|
||||||
|
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
# Frame inspection
|
||||||
|
########################################
|
||||||
|
# Cursed frame inspection
|
||||||
|
def __GL():
|
||||||
|
return inspect.getframeinfo(inspect.currentframe().f_back).lineno
|
||||||
|
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
# Code inspection
|
||||||
|
########################################
|
||||||
|
def find_string_in_file(file_path, list_regex_to_find):
|
||||||
|
"""
|
||||||
|
Find the string in the file stream
|
||||||
|
:param file_path: Path of the file
|
||||||
|
:type file_path: str
|
||||||
|
:param list_regex_to_find: List of string to find
|
||||||
|
:type list_regex_to_find: list
|
||||||
|
:return: Dict of string found and line number
|
||||||
|
:rtype: dict
|
||||||
|
"""
|
||||||
|
dict_string_found = {}
|
||||||
|
list_regex_to_find_left = [string for string in list_regex_to_find]
|
||||||
|
with open(file_path, "r") as file:
|
||||||
|
for line_number, line in enumerate(file):
|
||||||
|
for string_number, regex_to_find in enumerate(list_regex_to_find_left):
|
||||||
|
match = regex_to_find.search(line)
|
||||||
|
if match:
|
||||||
|
dict_string_found[match.group(1)] = line_number + 1
|
||||||
|
list_regex_to_find_left.pop(string_number)
|
||||||
|
break
|
||||||
|
if not list_regex_to_find_left:
|
||||||
|
break
|
||||||
|
if list_regex_to_find_left:
|
||||||
|
print("String not found: " + str(list_regex_to_find_left))
|
||||||
|
return dict_string_found
|
||||||
|
|
||||||
|
|
||||||
|
def find_functions_in_file(file_path, list_function_name):
|
||||||
|
"""
|
||||||
|
Find the function in the file stream
|
||||||
|
:param file_path: Path of the file
|
||||||
|
:type file_path: str
|
||||||
|
:param list_function_name: List of function name to find
|
||||||
|
:type list_function_name: iterable
|
||||||
|
:return: Dict of function found and line number
|
||||||
|
:rtype: dict
|
||||||
|
"""
|
||||||
|
return find_string_in_file(file_path, [re.compile(f"$def ({function_name})") for function_name in list_function_name])
|
||||||
|
|
||||||
|
|
||||||
|
def find_classes_in_file(file_path, list_class_name):
|
||||||
|
"""
|
||||||
|
Find the class in the file stream
|
||||||
|
:param file_path: Path of the file
|
||||||
|
:type file_path: str
|
||||||
|
:param list_class_name: List of class name to find
|
||||||
|
:type list_class_name: iterable
|
||||||
|
:return: Dict of class found and line number
|
||||||
|
:rtype: dict
|
||||||
|
"""
|
||||||
|
return find_string_in_file(file_path, [re.compile(f"^class ({class_name})") for class_name in list_class_name])
|
||||||
|
|
||||||
|
|
||||||
|
def find_variables_in_file(file_path, list_variable_name):
|
||||||
|
"""
|
||||||
|
Find the variable in the file stream
|
||||||
|
:param file_path: Path of the file
|
||||||
|
:type file_path: str
|
||||||
|
:param list_variable_name: List of variable name to find
|
||||||
|
:type list_variable_name: iterable
|
||||||
|
:return: Dict of variable found and line number
|
||||||
|
:rtype: dict
|
||||||
|
"""
|
||||||
|
return find_string_in_file(file_path, [re.compile(f"^({variable_name}) ?=") for variable_name in list_variable_name])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# print(find_string_in_file(__module_path("syntax.py"), [re.compile(r"^(html_insert) ?=")]))
|
||||||
|
pass
|
||||||
|
|
@ -0,0 +1,161 @@
|
||||||
|
# Enhanced text
|
||||||
|
italics:
|
||||||
|
bpr: '*italics*'
|
||||||
|
markdown compatible: yes
|
||||||
|
additional unsupported markdown syntax: '_italics_'
|
||||||
|
accepts arguments: no
|
||||||
|
type: enhanced text
|
||||||
|
scope: first release
|
||||||
|
|
||||||
|
bold:
|
||||||
|
bpr: '**bold**'
|
||||||
|
markdown compatible: yes
|
||||||
|
accepts arguments: no
|
||||||
|
type: enhanced text
|
||||||
|
scope: first release
|
||||||
|
|
||||||
|
strikethrough:
|
||||||
|
bpr: '~~strikethrough~~'
|
||||||
|
markdown compatible: yes
|
||||||
|
accepts arguments: no
|
||||||
|
type: enhanced text
|
||||||
|
scope: first release
|
||||||
|
|
||||||
|
underline:
|
||||||
|
bpr: '__underline__'
|
||||||
|
markdown compatible: no
|
||||||
|
accepts arguments: no
|
||||||
|
type: enhanced text
|
||||||
|
scope: first release
|
||||||
|
|
||||||
|
|
||||||
|
# In-line elements
|
||||||
|
hyperlinks:
|
||||||
|
bpr: '[text]("hyperlink")'
|
||||||
|
markdown compatible: yes
|
||||||
|
accepts arguments: no
|
||||||
|
type: in-line
|
||||||
|
scope: first release
|
||||||
|
|
||||||
|
# Pre-parsing elements
|
||||||
|
aliases:
|
||||||
|
bpr: '@[alias]'
|
||||||
|
markdown compatible: no
|
||||||
|
accepts arguments: yes
|
||||||
|
type: pre-parsing
|
||||||
|
scope: first release
|
||||||
|
|
||||||
|
images:
|
||||||
|
bpr: '@{image}'
|
||||||
|
markdown compatible: no
|
||||||
|
accepts arguments: yes
|
||||||
|
type: pre-parsing
|
||||||
|
scope: first release
|
||||||
|
|
||||||
|
# One-line elements
|
||||||
|
headers:
|
||||||
|
bpr: '# header #'
|
||||||
|
markdown compatible: no
|
||||||
|
accepts arguments: yes
|
||||||
|
accepts enhanced text: no
|
||||||
|
type: one-line
|
||||||
|
scope: first release
|
||||||
|
|
||||||
|
display:
|
||||||
|
bpr: '! header !'
|
||||||
|
markdown compatible: no
|
||||||
|
accepts arguments: yes
|
||||||
|
accepts enhanced text: no
|
||||||
|
type: one-line
|
||||||
|
scope: first release
|
||||||
|
|
||||||
|
# Not sure about sublists for the moment, add that later
|
||||||
|
numbered lists:
|
||||||
|
bpr: '#. nbd list item'
|
||||||
|
markdown compatible: no
|
||||||
|
accepts arguments: no
|
||||||
|
accepts enhanced text: yes
|
||||||
|
additional unsupported features: sub-lists
|
||||||
|
type: one-line
|
||||||
|
scope: first release
|
||||||
|
|
||||||
|
bullet lists:
|
||||||
|
bpr: '- list item'
|
||||||
|
markdown compatible: yes
|
||||||
|
accepts arguments: no
|
||||||
|
accepts enhanced text: yes
|
||||||
|
type: one-line
|
||||||
|
scope: first release
|
||||||
|
|
||||||
|
|
||||||
|
# Multi-line elements
|
||||||
|
structural elements:
|
||||||
|
bpr: '<<start /// end>>'
|
||||||
|
accepted elements: div, article, aside, section
|
||||||
|
markdown compatible: no
|
||||||
|
accepts arguments: yes
|
||||||
|
type: multi-line
|
||||||
|
scope: first release
|
||||||
|
|
||||||
|
tables:
|
||||||
|
bpr: '| table cell |'
|
||||||
|
markdown compatible: no
|
||||||
|
accepts arguments: yes
|
||||||
|
accepts enhanced text: yes
|
||||||
|
type: multi-line
|
||||||
|
scope: first release
|
||||||
|
|
||||||
|
table-separator:
|
||||||
|
bpr: '|-----------|'
|
||||||
|
# accepts ':' either at the start, middle or end of the line to align text for the column
|
||||||
|
# putting a separator after a row will change said row to table-header
|
||||||
|
# putting a separator after a column will change said column to table-header
|
||||||
|
markdown compatible: no
|
||||||
|
accepts arguments: yes
|
||||||
|
accepts enhanced text: yes
|
||||||
|
type: multi-line
|
||||||
|
scope: first release
|
||||||
|
|
||||||
|
|
||||||
|
# # Things to add
|
||||||
|
blockquotes:
|
||||||
|
bpr: '> quote'
|
||||||
|
markdown compatible: yes
|
||||||
|
accepts arguments: yes
|
||||||
|
accepts enhanced text: yes
|
||||||
|
type: one-line
|
||||||
|
scope: first release
|
||||||
|
|
||||||
|
code:
|
||||||
|
bpr: '`quote`'
|
||||||
|
markdown compatible: yes
|
||||||
|
accepts arguments: no
|
||||||
|
accepts enhanced text: no
|
||||||
|
type: in-line
|
||||||
|
scope: post-release
|
||||||
|
|
||||||
|
code-blocks:
|
||||||
|
bpr: '```quote```'
|
||||||
|
markdown compatible: yes
|
||||||
|
accepts arguments: yes
|
||||||
|
accepts enhanced text: no
|
||||||
|
type: multi-line
|
||||||
|
scope: post-release
|
||||||
|
|
||||||
|
horizontal rule:
|
||||||
|
bpr: '---'
|
||||||
|
markdown compatible: yes
|
||||||
|
accepts arguments: yes
|
||||||
|
accepts enhanced text: no
|
||||||
|
type: one-line
|
||||||
|
scope: first release
|
||||||
|
|
||||||
|
reference-style link:
|
||||||
|
bpr: '[digit]("hyperlink")'
|
||||||
|
markdown syntax: '[link][digit]' # needs testing, unclear
|
||||||
|
accepts arguments: yes
|
||||||
|
accepts enhanced text: no
|
||||||
|
type: one-line
|
||||||
|
scope: first release
|
||||||
|
|
||||||
|
# Inline-style link with title
|
||||||
Loading…
Reference in New Issue