created function for testing and readability

This commit is contained in:
vicky 2022-03-10 17:17:46 +01:00
parent 4adfadcf48
commit 11e4df26ac
1 changed files with 67 additions and 21 deletions

View File

@ -38,27 +38,27 @@ class TextToken(SemanticType):
class EnhancedToken(SemanticType):
label = "enhanced text"
label = "text:enhanced"
class EtEmToken(SemanticType):
label = "em text"
label = "text:em"
class EtStrongToken(SemanticType):
label = "strong text"
label = "text:strong"
class EtUnderlineToken(SemanticType):
label = "underline text"
label = "text:underline"
class EtStrikethroughToken(SemanticType):
label = "strikethrough text"
label = "text:strikethrough"
class EtCustomSpanToken(SemanticType):
label = "custom span text"
label = "text:custom_span"
def of_type(token_class):
@ -73,6 +73,40 @@ def of_type(token_class):
return _of_type
def readable_markup(list_of_tokens):
"""
Function used for testing and readability purposes. Replaces matched markup with html-like tags.
:param list_of_tokens: list of matched markup tokens in analysed text.
:return: returns readable string.
:rtype: string
"""
readable_string = ''
for token in list_of_tokens:
if type(token) == str:
readable_string += token
elif token.label == 'text:em':
readable_string += _add_tag('em', readable_markup(token.content))
elif token.label == 'text:strong':
readable_string += _add_tag('b', readable_markup(token.content))
elif token.label == 'text:underline':
readable_string += _add_tag('u', readable_markup(token.content))
elif token.label == 'text:strikethrough':
readable_string += _add_tag('st', readable_markup(token.content))
elif token.label == 'text:custom_span':
readable_string += _add_tag('#', readable_markup(token.content))
elif token.label == 'text':
readable_string += ' ' + ' '.join(token.content)
else:
raise Exception('Unknown token type' + token.label)
return readable_string[1:]
def _add_tag(tag, content):
if content:
return f' <{tag}>{content}</{tag}>'
return ''
# create_image_token = of_type(ImageToken)
# create_image_token()
# Base elements
@ -80,21 +114,22 @@ quotes = pp.Word(r""""'""")
value = (quotes + pp.Word(pp.alphanums + r'.') + pp.match_previous_literal(quotes) ^
pp.common.fnumber)("value")
assignation = pp.Group(pp.common.identifier('var_name') + '=' + value('var_value'))("assignation")
text = pp.OneOrMore(pp.Word(pp.alphanums))
text = pp.OneOrMore(pp.Word(pp.alphanums))('text').add_parse_action(of_type(TextToken))
# Composite elements
var = '[' + pp.delimitedList(assignation ^ value)("list_vars").set_name("list_vars") + ']'
# Enhanced text
enhanced_text = pp.Forward()
et_em = ('*' + enhanced_text + '*')('em')
et_strong = ('**' + enhanced_text + '**')('strong')
et_underline = ('__' + enhanced_text + '__')('underline')
et_strikethrough = ('~~' + enhanced_text + '~~')('strikethrough')
custom_span = '(#' + pp.Word(pp.nums) + ')'
et_custom_span = (custom_span + enhanced_text + pp.match_previous_literal(custom_span))('custom_span')
# enhanced_text <<=
# (et_strong ^ et_em ^ et_underline ^ et_strikethrough ^ et_custom_span ^ text) + pp.Optional(enhanced_text)
et_em = (pps('*') + enhanced_text + pps('*'))('em').add_parse_action(of_type(EtEmToken))
et_strong = (pps('**') + enhanced_text + pps('**'))('strong').add_parse_action(of_type(EtStrongToken))
et_underline = (pps('__') + enhanced_text + pps('__'))('underline').add_parse_action(of_type(EtUnderlineToken))
et_strikethrough = (pps('~~') + enhanced_text + pps('~~'))('strikethrough').add_parse_action(of_type(EtStrikethroughToken))
custom_span = ('(#' + pp.Word(pp.nums)('span_id') + ')').set_name('span_tag')
et_custom_span = \
(custom_span + enhanced_text + pp.match_previous_literal(custom_span))('custom_span')\
.add_parse_action(of_type(EtCustomSpanToken))
enhanced_text <<= (text | et_strong | et_em | et_underline | et_strikethrough | et_custom_span) + pp.Opt(enhanced_text)
# Specific elements
@ -120,9 +155,20 @@ line_to_replace = pp.OneOrMore(pp.SkipTo(image ^ alias)('text').add_parse_action
# Temporary tests
if __name__ == '__main__': # pragma: no cover
pp.autoname_elements()
test_string = r"""
text ***str_text* em_text** text
"""
saucisse = enhanced_text.parse_string(test_string)
rich.inspect(saucisse)
list_strings = [
"Normal text",
"Normal text *em text*",
"Normal text **strong text**",
"Normal text __underline text__",
"Normal text ~~strikethrough text~~",
"Normal text (#123) custom span text(#123)",
"**Strong text __underline__** normal text",
"Normal text **strong text *em text*** *em text **strong text*** normal text",
]
enhanced_text.create_diagram("../../../dev_outputs/diagram.html")
for string in list_strings:
print('Input string:', string)
output = enhanced_text.parseString(string)
# rich.inspect(output)
print('Output string:', readable_markup(output))
print()